8

Is it possible to combine multiple pdf files into a single pdf file programmatically in iphone.I have created individual pages from different parts of my program and now need to merge them into a single file

sujith1406
  • 2,822
  • 6
  • 40
  • 60

3 Answers3

5

Yes you can do that. Please follow the below code

//Open a pdf context for the single file
UIGraphicsBeginPDFContextToFile(oldFile, paperSize, nil);

//Run a loop to the number of pages you want
for (pageNumber = 1; pageNumber <= count; pageNumber++)
{
//Open a pdf page context
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);

//Get graphics context to draw the page
CGContextRef currentContext = UIGraphicsGetCurrentContext();

//Flip and scale context to draw the pdf correctly
CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0); 

//Get document access of the pdf from which you want a page
CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) newUrl);

//Get the page you want
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);

//Drawing the page
CGContextDrawPDFPage (currentContext, newPage);

//Clean up
newPage = nil;       
CGPDFDocumentRelease(newDocument);
newDocument = nil;
newUrl = nil;

}

UIGraphicsEndPDFContext();

So you have to write necessary condition of taking appropriate pages from appropriate pdf before draw the page. You have created a pdf from multiple sources!

cocoakomali
  • 1,346
  • 1
  • 8
  • 7
  • i did my pdf combining as mentioned above but with nsdata.the problem is that only the first page is printed in the final combined document.this question i have posted in http://stackoverflow.com/questions/9668047/combining-multiple-pdf-documents-into-single-document-not-working – sujith1406 Mar 12 '12 at 13:49
  • @cocoakomali: could you please help on following SO The merged file is not getting display http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios – Navnath Memane May 26 '13 at 16:50
  • By applying above solution I am adding one by one page but get first all page blanks. Only last one is visible. please suggest me where I am going wrong on http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios#comment24146255_16646039 – Navnath Memane May 29 '13 at 09:08
5

Here is a routine that I wrote to take a url for a pdf and append it to a context that you've already created (so that you can call it for multiple files and append them all):

- (void)appendPdfAtURL:(NSURL *)pdfURL toContext:(CGContextRef)pdfDestinationContext {
    CGPDFDocumentRef  pdfDoc = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfURL);
    if (pdfDoc) {
        size_t numPages = CGPDFDocumentGetNumberOfPages(pdfDoc);
        if (numPages > 0) {
            // Loop through each page in the source file
            for (size_t i = 1; i <= numPages; i++) {
                CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, i);
                if (pdfPage) {
                    // Get the page size
                    CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);

                    // Copy the page from the source file to the context
                    CGContextBeginPage(pdfDestinationContext, &pdfCropBoxRect);
                    CGContextDrawPDFPage(pdfDestinationContext, pdfPage);
                }
            }
        }

        // Close the source file
        CGPDFDocumentRelease(pdfDoc);
    }
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Note that since you are writing from a pdf to a pdf that you don't have to translate/scale it before writing it. – lnafziger Mar 10 '12 at 18:44
1

Try Quartz 2D APIs. It has very nice support for reading writing PDF files.

  1. By looking into APIs you should be able to read all input PDF document
  2. get PDF Page for them
  3. create a PDF Context for your new document
  4. draw PDF Pages in PDF Context

Please note that PDF page drawing code should be between begin page and end page calls

CGPDFDocumentCreateWithURL
CGContextBeginPage
CGPDFDocumentGetPage
CGPDFContextCreateWithURL
CGContextDrawPDFPage
CGContextEndPage

Check out Apple docs for

[CGContext][1]
[CGPDFDocument][2]
[CGPDFPage][3]

Check out if it helps, or let me know if you need some sample pseudo code.

msk
  • 8,885
  • 6
  • 41
  • 72