7

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!

tzippy
  • 6,458
  • 30
  • 82
  • 151

1 Answers1

9

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++];
    }
}
fjoachim
  • 906
  • 6
  • 11
  • I'm really not quite sure how this would work, when I try to use "fast enumeration" on a `PDFDocument` I get, `Collection expression type "PDFDocument *" may not respond to 'countByEnumeratingWithState:objects:count:'` – Alex Gray Feb 27 '13 at 16:02
  • 1
    Yes, you have to iterate through all the pages of each PDFDocument. I fixed the error. – fjoachim Feb 28 '13 at 20:08