【问题标题】:Adding TOC to PDFDocument in Swift/Cocoa using PDFOutline使用 PDFOutline 在 Swift/Cocoa 中将 TOC 添加到 PDFDocument
【发布时间】:2018-08-04 21:00:03
【问题描述】:

我正在处理一个小程序,它需要多个单页 PDF 并将它们合并到一个多页 PDF 中。我在 Swift4/MacOS/Cocoa 中工作,我一生都无法在 Swift 中找到任何类型的示例来创建大纲/仅遍历现有的大纲(我非常熟悉)。

使用对文档的最佳猜测,我想出了以下我一直在玩弄的东西,但一点运气都没有。 PDF 出来的很好,但里面从来没有大纲/目录。它可能就像缺少任务或其他东西一样简单......任何线索将不胜感激。

顺便说一句,它在两个循环而不是一个循环中的原因是因为我认为我可能需要先添加所有页面 - 但尝试这样做并没有什么不同。如果可能,最终只会有一个循环。

static func mergePagesIntoSinglePDF(streamId: String, numPages: Int)
    {
        let newPDF = PDFDocument()
        var directoryURLStr = ""

        for pageNum in 1...numPages {

            let directoryUrl = getFileURL(streamId: streamId, recNum: pageNum)
            directoryURLStr = directoryUrl!.absoluteString

            if let pdfDocument = PDFDocument(url: directoryUrl!),
                let pdfPage = pdfDocument.page(at: 0)
            {
                newPDF.insert(pdfPage, at: newPDF.pageCount)    
            }
        }

        for pageNum in 1...numPages {

            let newDest:PDFDestination = PDFDestination.init(page: newPDF.page(at: pageNum-1)!, at:NSPoint(x:1,y:1))
            let newTOCEntry:PDFOutline = PDFOutline.init()

            newTOCEntry.destination = newDest
            newTOCEntry.label = "This is page: \(pageNum)"
            newPDF.outlineRoot?.insertChild(newTOCEntry, at: pageNum-1)
        }

        directoryURLStr = (getFileURL(streamId: streamId)?.absoluteString)!
        let fileURL = URL(string: directoryURLStr)

        newPDF.write(to: fileURL!)
    }

【问题讨论】:

    标签: swift cocoa pdf-generation


    【解决方案1】:

    看来我比我想象的要近得多。主要问题是我需要为 PDFOutline 创建一个根节点。我还添加了一些内容以使 NSPoint 更智能一些,因为您不能在 PDF 中真正假设“1,1” hack 是一个有效的坐标(通常是......但不能假设)。当然,现在可以删除双循环,但为了清楚起见,我将其保留在:

    static func mergePagesIntoSinglePDF(streamId: String, numPages: Int)
    {
        let newPDF = PDFDocument()
    
        newPDF.outlineRoot = PDFOutline();  // CREATE PDF OUTLINE ROOT NODE!
    
        var directoryURLStr = ""
    
        for pageNum in 1...numPages {
    
            let directoryUrl = getFileURL(streamId: streamId, recNum: pageNum)
            directoryURLStr = directoryUrl!.absoluteString
    
            if let pdfDocument = PDFDocument(url: directoryUrl!),
                let pdfPage = pdfDocument.page(at: 0)
            {
                newPDF.insert(pdfPage, at: newPDF.pageCount)    
            }
        }
    
        for pageNum in 1...numPages {
    
            let pdfPage = newPDF.page(at: pageNum-1)!
    
            // ADD A LITTLE CODE TO MAKE THE NSPoint IN THE DESTINATION MORE SOUND
    
            let pdfPageRect = pdfPage.bounds(for: PDFDisplayBox.mediaBox)
            let topLeft = NSMakePoint(pdfPageRect.minX, pdfPageRect.height + 20)
            let destination = PDFDestination(page: pdfPage, at: topLeft)
    
            let newDest = PDFDestination(page: pdfPage, at:topLeft)
            let newTOCEntry = PDFOutline()
    
            newTOCEntry.destination = newDest
            newTOCEntry.label = "\(streamId) page \(pageNum)"
            newPDF.outlineRoot!.insertChild(newTOCEntry, at: pageNum-1)
        }
    
        directoryURLStr = (getFileURL(streamId: streamId)?.absoluteString)!
        let fileURL = URL(string: directoryURLStr)
    
        newPDF.write(to: fileURL!)
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多