首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MACOS中使用PDFActionGoto将具有PDFPage操作类型的链接注释添加到PDFPage中

如何在MACOS中使用PDFActionGoto将具有PDFPage操作类型的链接注释添加到PDFPage中
EN

Stack Overflow用户
提问于 2020-09-17 15:26:34
回答 1查看 316关注 0票数 1

我一直在尝试向我的PDFPage添加一个链接注释,这样一旦单击它,它就会带我到下一页的特定位置。这就是我正在尝试的方式,但我的代码不起作用:

occur.

  • .freetext

  • 光标不会更改为指针样式。单击该区域后,

  • 导航不再显示。

我希望能在这方面提供任何帮助。这几天我一直在尝试。我使用的是最新版本的xcode (12和Swid5.3)

代码语言:javascript
复制
    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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-21 02:17:23

经过多次尝试和错误之后,我认为如果文档是通过printOperation运行的,那么linkAnnotation就不会被保留。因此,我使用“保存面板”将文档保存为文件。如果你点击第一页的绿色区域,它将带你到第二页(目的地到达)。

代码语言:javascript
复制
    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)
            }
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63941329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档