问题描述
我一直在尝试向我的PDFPage添加链接注释,以便单击后将其带到下一页的特定位置。这是我尝试的方式,但是我的代码无法正常工作:
- 光标不会更改为指针样式。
- 单击该区域后,不会进行导航。
- .freetext注释正在工作并显示。
在此方面,我将不胜感激。最近几天我一直在尝试这种方法。我正在使用最新版本的xcode(12和Swift 5.3)
let docDirectoryPath = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)[0]
let pdfDocument = PDFDocument()
let page1 = PDFPage()
let page2 = PDFPage()
pdfDocument.insert(page1,at: 0)
pdfDocument.insert(page2,at: 1)
let textAnnotation = PDFAnnotation(bounds: NSRect(x: 200,y: 1000,width: 600,height: 25),forType: .freeText,withProperties: nil)
textAnnotation.contents = "Click me to go to the next page"
let linkAnnotation = PDFAnnotation(bounds: NSRect(x: 200,forType: .link,withProperties: nil)
linkAnnotation.action = PDFActionGoTo(destination: PDFDestination(page: page2,at: NSPoint(x: 200,y: 1000)))
linkAnnotation.shouldDisplay = true
linkAnnotation.shouldPrint = true
let someText = PDFAnnotation(bounds: NSRect(x: 200,withProperties: nil)
someText.contents = "This is the desitnation of link annotation"
page1.addAnnotation(textAnnotation)
page1.addAnnotation(linkAnnotation)
page2.addAnnotation(someText)
let printOpts: [NSPrintInfo.AttributeKey: Any] = [NSPrintInfo.AttributeKey.jobDisposition: NSPrintInfo.JobDisposition.preview,NSPrintInfo.AttributeKey.jobSavingURL: docDirectoryPath]
let printInfo = NSPrintInfo(dictionary: printOpts)
printInfo.horizontalPagination = NSPrintInfo.PaginationMode.automatic
printInfo.verticalPagination = NSPrintInfo.PaginationMode.automatic
printInfo.topMargin = 0.0
printInfo.leftMargin = 0.0
printInfo.rightMargin = 0.0
printInfo.bottomMargin = 0.0
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = false
printInfo.paperSize = NSSize(width: 8.5*96,height: 11*96)
printInfo.scalingFactor = 1.0
let printOperation = pdfDocument.printOperation(for: printInfo,scalingMode: .pageScaleNone,autoRotate: false)!
printOperation.showsPrintPanel = true
printOperation.run()
解决方法
经过反复试验,我发现如果文档通过printOperation运行,则linkAnnotation不会保留。因此,我使用“保存”面板将文档保存到文件中。如果单击第一页的绿色区域,将带您到第2页(到达目的地)
import Foundation
import PDFKit
let page1 = PDFPage()
let page2 = PDFPage()
let link = PDFAnnotation(bounds: NSRect(x: 40,y: 750,width: 200,height: 30),forType: .link,withProperties: nil)
let highlight = PDFAnnotation(bounds: NSRect(x: 40,forType: .highlight,withProperties: nil)
highlight.color = .green
highlight.backgroundColor = .red
let text = PDFAnnotation(bounds: NSRect(x: 40,y: 400,forType: .freeText,withProperties: nil)
text.contents = "Destination Arrived!"
link.action = PDFActionGoTo(destination: PDFDestination(page: page2,at: NSPoint(x: 40,y: 430)))
page1.addAnnotation(highlight)
page1.addAnnotation(link)
page2.addAnnotation(text)
let pdfDoc = PDFDocument()
pdfDoc.insert(page1,at: 0)
pdfDoc.insert(page2,at: 1)
let savePanel = NSSavePanel()
savePanel.title = NSLocalizedString("Save file",comment: "enableFileMenuItems")
savePanel.nameFieldStringValue = "LinkAnnotation.pdf"
savePanel.prompt = NSLocalizedString("Save",comment: "enableFileMenuItems")
savePanel.allowedFileTypes = ["pdf"]
let fileManager = FileManager.default
savePanel.begin() { (result2) -> Void in
if result2 == NSApplication.ModalResponse.OK {
let fileWithExtensionURL = savePanel.url!
if fileManager.fileExists(atPath: fileWithExtensionURL.path) {
} else {
pdfDoc.write(to: fileWithExtensionURL)
}
}
}