在PDF视图中显示修改的PDF页面而无需在本地保存
我正在使用以下代码在PDF上编写文本。我需要使用pdfview.i无法找到修改后的PDF页面。我找不到在PDF视图上显示修改的PDF页面的方法,而无需在本地保存修改的PDF并从光盘中加载它。有没有办法在不先保存的情况下在PDF中显示PDF文件?
let input = getSelectedFilename()
let dstURL = URL(fileURLWithPath: "/Users/me/Downloads/out.pdf")
var pdffile=PDFDocument(url: input)
let page: PDFPage = pdffile!.page(at: 0)!
let bounds = page.bounds(for: PDFDisplayBox.mediaBox)
let size = bounds.size
var mediaBox: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let context = CGContext(dstURL as CFURL, mediaBox: &mediaBox, nil)
let graphicsContext = NSGraphicsContext(cgContext: context!, flipped: false)
NSGraphicsContext.current = graphicsContext
context!.beginPDFPage(nil)
text.draw(in:drawrect,withAttributes:textFontAttributes);
context!.saveGState()
context!.restoreGState()
context!.endPDFPage()
NSGraphicsContext.current = nil
context?.closePDF()
var pdffile2=PDFDocument(url: dstURL)
pdfview.document=pdffile2
I am using the following code to write a text on a PDF. I need to display the modified PDF page using PDFView.I cannot find a way to display the modified PDF page on the PDF View without saving the modified PDF locally and loading it from disc. Is there a way to display the PDF file in the PDF you without saving it first?
let input = getSelectedFilename()
let dstURL = URL(fileURLWithPath: "/Users/me/Downloads/out.pdf")
var pdffile=PDFDocument(url: input)
let page: PDFPage = pdffile!.page(at: 0)!
let bounds = page.bounds(for: PDFDisplayBox.mediaBox)
let size = bounds.size
var mediaBox: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let context = CGContext(dstURL as CFURL, mediaBox: &mediaBox, nil)
let graphicsContext = NSGraphicsContext(cgContext: context!, flipped: false)
NSGraphicsContext.current = graphicsContext
context!.beginPDFPage(nil)
text.draw(in:drawrect,withAttributes:textFontAttributes);
context!.saveGState()
context!.restoreGState()
context!.endPDFPage()
NSGraphicsContext.current = nil
context?.closePDF()
var pdffile2=PDFDocument(url: dstURL)
pdfview.document=pdffile2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从
数据
创建pdfdocument
。在您的pdfdocument上使用.datarepresentation()
获取数据,然后pdfdocument(data:)
从数据创建pdfdocument。You can create a
PDFDocument
fromData
. Use.dataRepresentation()
on your PDFDocument to get the data, andPDFDocument(data: )
to create the PDFDocument from the data.