【发布时间】: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