在PDF视图中显示修改的PDF页面而无需在本地保存

发布于 2025-01-29 02:21:21 字数 1073 浏览 5 评论 0原文

我正在使用以下代码在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2025-02-05 02:21:21

您可以从数据创建pdfdocument。在您的pdfdocument上使用.datarepresentation()获取数据,然后pdfdocument(data:)从数据创建pdfdocument。

let data = myPDFDocument.dataRepresentation()
let anotherDocument = PDFDocument(data: data)

You can create a PDFDocument from Data. Use .dataRepresentation() on your PDFDocument to get the data, and PDFDocument(data: ) to create the PDFDocument from the data.

let data = myPDFDocument.dataRepresentation()
let anotherDocument = PDFDocument(data: data)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文