Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
668 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...