在Cocoa中合并PDF文件 [英] Merging PDF Files in Cocoa

查看:109
本文介绍了在Cocoa中合并PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接几个PDF文件以形成一个单一的PDF。
现在我到达了,我知道,PDFKit是正确的方式去(我猜)。
但是我不知道,如何完成合并。
我应该有一个 PDFDocument 和几个 PDFPage ,然后调用 insertPage PDFDocument
或者有更简单的方法吗?我不想改变PDF的contetwise,我只是想合并他们。
非常感谢。

I would like to concatenate several PDF files to form one single PDF. Now I've come so far that I know, PDFKit is the proper way to go (I guess). But I am not sure, how to accomplish the merging. Should I have one PDFDocument and several PDFPage and then call insertPage on the PDFDocument ? Or is there a much simpler way? I dont want to alter the PDFs contetwise, I just want to merge them. Thanks a lot!

推荐答案

正如你所说,你需要一个输出 PDFDocument 对象,它将包含所有输入PDF文件的所有页面。为此,您需要遍历所有输入文件,为每一个创建 PDFDocument 对象,并遍历所有页面,使用 insertPage $

As you indicated, you need one output PDFDocument object which will contain all pages of all input PDF files. To do so, you'll need to loop through all input files, create PDFDocument objects for each one and iterate over all pages to add them using insertPage to the output PDFDocument object.

假设 inputDocuments 是一个或多个 PDFDocument 对象的 NSArray ,您可以使用此代码片段:

Assuming that inputDocuments is an NSArray of one ore more PDFDocument objects, you can use this snippet:

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++];
    }
}

这篇关于在Cocoa中合并PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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