Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging PDF Files in Cocoa

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!

like image 261
tzippy Avatar asked Apr 10 '11 08:04

tzippy


People also ask

How do I join two PDF files together?

Open Acrobat to combine files: Open the Tools tab and select "Combine files." Add files: Click "Add Files" and select the files you want to include in your PDF. You can merge PDFs or a mix of PDF documents and other files.

Which app is best for merging PDF files?

iLovePDF iLovePDF is another all-in-one PDF software with the PDF merger capability that you can also use as a PDF reader. It is also available on the web and major mobile and desktop platforms, including Windows, macOS, Android, and iOS.

How can I merge PDF files free?

Select the files you want to merge using the Acrobat PDF combiner tool. Reorder the files if needed. Click Merge files. Download the merged PDF.

How do I merge PDF files in Linux terminal?

Combine PDFs with the gs command Use the -sDEVICE attribute to specify the output device or function. Use the -sOUTPUTFILE to specify the merged PDF file. Use the -dBATCH to specify the PDF files to combine in the order you want them to appear. The command above will output merged_file.


1 Answers

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.

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++];
    }
}
like image 92
fjoachim Avatar answered Nov 15 '22 09:11

fjoachim