Is it possible to combine several PDF files into one PDF file programmatically on iphone?

Is it possible to combine several PDF files into one PDF file programmatically in iphone. I created separate pages from different parts of my program and now I have to combine them into one file

+7
source share
3 answers

Yes you can do it. Please follow the code below

//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(); 

Thus, you must write the necessary condition for taking the corresponding pages from the corresponding pdf before drawing the page. You have created a PDF from several sources!

+5
source

Here is the routine I wrote to take the url for pdf and add it to the context that you have already created (so that you can call it for several files and add 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); } } 
+5
source

Try the Quartz 2D API. It has very good support for reading PDF files.

  • When learning the API, you should be able to read all input PDF document files
  • get a pdf page for them
  • Create a PDF context for your new document.
  • draw PDF pages in PDF

Please note that the PDF page drawing code must be between the start and end pages.

 CGPDFDocumentCreateWithURL CGContextBeginPage CGPDFDocumentGetPage CGPDFContextCreateWithURL CGContextDrawPDFPage CGContextEndPage 

Check out Apple docs for

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

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

+1
source

All Articles