我正在使用以下代碼在 PDF 上撰寫文本。我需要使用 PDFView 顯示修改后的 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
uj5u.com熱心網友回復:
您可以PDFDocument從Data. 在您的 PDFDocument 上使用.dataRepresentation()以獲取資料,并PDFDocument(data: )根據資料創建 PDFDocument。
let data = myPDFDocument.dataRepresentation()
let anotherDocument = PDFDocument(data: data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483532.html
上一篇:如何宣告稍后可用的Cocoa類?
