[英]Internal Hyperlinks within PDF getting removed after processing using PDFKit
我正在使用以下代碼在 PDF 頁面上繪制文本。一切正常,但如果您處理帶有內部鏈接的 PDF,例如:帶有內容鏈接到頁面的書,生成的 ZBCD1B68617759B1DFCFF0403A8D1Z 文件似乎已剝離鏈接。為什么會這樣?
let data = NSMutableData()
let consumer = CGDataConsumer(data: data as CFMutableData)!
let context = CGContext(consumer: consumer, mediaBox: nil, nil)!
let pdffile=PDFDocument(url: input)
for y in stride(from: 0, to: pagecount, by: 1)
{
let page: PDFPage = pdffile!.page(at: y)!
var mediaBox = page.bounds(for: PDFDisplayBox.mediaBox)
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
let bounds = page.bounds(for: PDFDisplayBox.mediaBox)
let size = bounds.size
page.draw(with: .mediaBox, to: context)
text.draw(in:drawrect,withAttributes:textFontAttributes);
context.endPDFPage()
}
context.closePDF()
try! data!.write(to: GetOutputFileName(inputfile: x), options[.atomicWrite])
鏈接只是一種注釋,所以我們只需要將原始頁面的注釋重新添加到新頁面,例如
...
}
context.closePDF()
let anotherDocument = PDFDocument(data:data as Data)
for y in stride(from: 0, to: pdffile!.pageCount, by: 1)
{
let page: PDFPage = pdffile!.page(at: y)!
let newPage: PDFPage = anotherDocument!.page(at: y)!
for anotatation in page.annotations {
newPage.addAnnotation(anotatation) // << here !!
}
}
使用 Xcode 13.4 / macOS 12.5 測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.