我一直在尝试向我的PDFPage添加一个链接注释,这样一旦单击它,它就会带我到下一页的特定位置。这就是我正在尝试的方式,但我的代码不起作用:
occur.
我希望能在这方面提供任何帮助。这几天我一直在尝试。我使用的是最新版本的xcode (12和Swid5.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, y: 1000, width: 600, height: 25), 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, y: 1000, width: 600, height: 25), forType: .freeText, 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()发布于 2020-09-21 02:17:23
经过多次尝试和错误之后,我认为如果文档是通过printOperation运行的,那么linkAnnotation就不会被保留。因此,我使用“保存面板”将文档保存为文件。如果你点击第一页的绿色区域,它将带你到第二页(目的地到达)。
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, y: 750, width: 200, height: 30), forType: .highlight, withProperties: nil)
highlight.color = .green
highlight.backgroundColor = .red
let text = PDFAnnotation(bounds: NSRect(x: 40, y: 400, width: 200, height: 30), 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)
}
}
}https://stackoverflow.com/questions/63941329
复制相似问题