如何在 MACOS 中使用 swift 将具有 PDFActionGoto 操作类型的 link 注释添加到 PDFPage
How to add a link annotation with a PDFActionGoto action type to a PDFPage using swift in MACOS
我一直在尝试向我的 PDFPage 添加 link 注释,以便在单击后将我带到下一页的特定位置。这是我正在尝试的方式,但我的代码不起作用:
- 光标未变为指针样式。
- 点击该区域后,导航不会发生。
- .自由文本注释正在运行并显示。
对于这方面的任何帮助,我将不胜感激。最近几天我一直在尝试这个。我正在使用最新版本的 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, 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()
经过多次尝试和错误后,我发现如果文档是 运行 通过 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, 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)
}
}
}
我一直在尝试向我的 PDFPage 添加 link 注释,以便在单击后将我带到下一页的特定位置。这是我正在尝试的方式,但我的代码不起作用:
- 光标未变为指针样式。
- 点击该区域后,导航不会发生。
- .自由文本注释正在运行并显示。
对于这方面的任何帮助,我将不胜感激。最近几天我一直在尝试这个。我正在使用最新版本的 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, 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()
经过多次尝试和错误后,我发现如果文档是 运行 通过 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, 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)
}
}
}