【问题标题】:Merging PDF Files in Cocoa在 Cocoa 中合并 PDF 文件
【发布时间】:2011-04-10 08:09:36
【问题描述】:

我想连接多个 PDF 文件以形成一个 PDF。 现在我已经走了这么远,我知道,PDFKit 是正确的方法(我猜)。 但我不确定如何完成合并。 我应该有一个PDFDocument 和几个PDFPage 然后在PDFDocument 上调用insertPage 吗? 还是有更简单的方法?我不想更改 PDF,我只想合并它们。 非常感谢!

【问题讨论】:

    标签: objective-c cocoa pdfkit


    【解决方案1】:

    正如您所指出的,您需要一个输出 PDFDocument 对象,该对象将包含所有输入 PDF 文件的所有页面。为此,您需要遍历所有输入文件,为每个文件创建 PDFDocument 对象并遍历所有页面以使用 insertPage 将它们添加到输出 PDFDocument 对象中。

    假设inputDocuments是一个或多个PDFDocument对象的NSArray,你可以使用这个sn-p:

    PDFDocument *outputDocument = [[PDFDocument alloc] init];
    NSUInteger pageIndex = 0;
    for (PDFDocument *inputDocument in inputDocuments) {
        for (NSUInteger j = 0; j < [inputDocument pageCount]; j++) {
            PDFPage *page = [inputDocument pageAtIndex:j];
            [outputDocument insertPage:page atIndex:pageIndex++];
        }
    }
    

    【讨论】:

    • 当我尝试在 PDFDocument 上使用“快速枚举”时,我真的不太确定这是如何工作的,Collection expression type "PDFDocument *" may not respond to 'countByEnumeratingWithState:objects:count:'
    • 是的,您必须遍历每个 PDFDocument 的所有页面。我修复了错误。
    猜你喜欢
    • 2013-04-09
    • 2011-03-27
    • 1970-01-01
    • 2011-09-27
    • 2013-04-02
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    相关资源
    最近更新 更多