如何在MACOS中使用Swift将具有PDFActionGoto动作类型的链接注释添加到PDFPage [英] How to add a link annotation with a PDFActionGoto action type to a PDFPage using swift in MACOS

查看:41
本文介绍了如何在MACOS中使用Swift将具有PDFActionGoto动作类型的链接注释添加到PDFPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试向我的PDFPage添加链接注释,以便单击后将其带到下一页的特定位置.这是我正在尝试的方式,但是我的代码无法正常工作:

I have been trying to add a link annotation to my PDFPage so that once clicked it will take me to a specific location of the next page. This is the way I am trying but my code is not working:

  1. 光标不会更改为指针样式.
  2. 单击该区域后,不会进行导航.
  3. .freetext注释正在工作并得到显示.

在此方面,我将不胜感激.最近几天,我一直在尝试这种方法.我正在使用最新版本的xcode(12和Swift 5.3)

I would appreciate any help regarding this. I have been trying this for the last couple of days. I am using latest version of xcode (12 and 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 运行,则不会保留链接注释.因此,我使用保存"面板将文档保存到文件中.如果单击第一页的绿色区域,将带您到第2页(到达目的地)

After much trial and error I figured that if the document is run through printOperation linkAnnotation is not retained. So I used save panel to save document to file instead. If you click on the green area of the first page it will take you to the page 2 (Destination Arrived)

    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)
            }
        }
    }

这篇关于如何在MACOS中使用Swift将具有PDFActionGoto动作类型的链接注释添加到PDFPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆