使用 PDFOutline 在 Swift/Cocoa 中将目录添加到 PDFDocument

Adding TOC to PDFDocument in Swift/Cocoa using PDFOutline

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

根据对文档的最佳猜测,我想出了以下内容,我一直在摆弄 w/a 但一点运气都没有。 PDF 输出正常,但其中从来没有 outline/TOC。它可能像缺少作业或其他东西一样简单......任何线索都将不胜感激。

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

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

看来我比我想象的要近得多。主要问题是我需要为 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!)
}