Combining PDF files in Cocoa

I would like to combine several PDF files to form one PDF file. Now I have come so far that I know PDFKit is the right way (I think). But I'm not sure how to achieve a merger. Should I have one PDFDocument and multiple PDFPage , and then call insertPage on the PDFDocument ? Or is there a much simpler way? I do not want to change the PDF files, I just want to combine them. Many thanks!

+6
source share
1 answer

As you pointed out, you need one PDFDocument output object that will contain all pages of all PDF input files. To do this, you need to skip all the input files, create PDFDocument objects for each of them, and PDFDocument over all the pages to add them using insertPage to the output PDFDocument object.

Assuming inputDocuments is an NSArray one or 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++]; } } 
+7
source

Source: https://habr.com/ru/post/885598/


All Articles