2

I am writing an app for an iphone/ipad that converts camera image (.png) to pdf and save to the /user/documents folder. Now I'm trying to figure out how to append another pdf to an existing document so they will become multipage. Basically, if you have doc1.pdf and doc2.pdf saved in the documents folder how would you merge the 2 pdfs so they become 1 document with 2 pages?

Appreciate any help or recommendations you guys can give me.

Thanks,

akjoshi
  • 15,374
  • 13
  • 103
  • 121
user1834869
  • 23
  • 1
  • 3
  • This is not an answer for this, but If someone want to [append an existing pdf file](http://stackoverflow.com/a/15355168/1603234) – Hemang Mar 12 '13 at 07:03
  • Hi frnd. Can you share your PDF code logic with me. my email ID is nav.memane@yahoo.com. Actually I am not able to save the PDF file. My new PDF file is overlapping the first one so I could not able to merge the file later – Navnath Memane May 20 '13 at 08:58

1 Answers1

5

Here is a sample code for appending one pdf over another one:

// Documents dir
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// File paths
NSString *pdfPath1 = [documentsDirectory stringByAppendingPathComponent:@"pdf1.pdf"];
NSString *pdfPath2 = [documentsDirectory stringByAppendingPathComponent:@"pdf2.pdf"];
NSString *pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:@"pdfout.pdf"];

// File URLs
CFURLRef pdfURL1 = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath1];
CFURLRef pdfURL2 = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath2];
CFURLRef pdfURLOutput = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPathOutput];

// File references
CGPDFDocumentRef pdfRef1 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL1);
CGPDFDocumentRef pdfRef2 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL2);

// Number of pages
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2);

// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

// Loop variables
CGPDFPageRef page;
CGRect mediaBox;

// Read the first PDF and generate the output pages
NSLog(@"Pages from pdf 1 (%i)", numberOfPages1);
for (int i=1; i<=numberOfPages1; i++) {
  page = CGPDFDocumentGetPage(pdfRef1, i);
  mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  CGContextBeginPage(writeContext, &mediaBox);
  CGContextDrawPDFPage(writeContext, page);
  CGContextEndPage(writeContext);
}

// Read the second PDF and generate the output pages
NSLog(@"Pages from pdf 2 (%i)", numberOfPages2);
for (int i=1; i<=numberOfPages2; i++) {
  page = CGPDFDocumentGetPage(pdfRef2, i);
  mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  CGContextBeginPage(writeContext, &mediaBox);
  CGContextDrawPDFPage(writeContext, page);
  CGContextEndPage(writeContext); 
}
NSLog(@"Done");

// Finalize the output file
CGPDFContextClose(writeContext);

// Release from memory
CFRelease(pdfURL1);
CFRelease(pdfURL2);
CFRelease(pdfURLOutput);
CGPDFDocumentRelease(pdfRef1);
CGPDFDocumentRelease(pdfRef2);
CGContextRelease(writeContext);

Source

iDev
  • 23,310
  • 7
  • 60
  • 85
  • Thanks. this line is giving me 0x0. CFURLRef pdfURL1 = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath1]; I verified that the path of my pdf is correct. I'm using ARC. – user1834869 Nov 20 '12 at 04:20
  • @user1834869, Looks like some problem with reading the pdf file. How are you normally doing it? Have you checked with that code whether you are able to read it back after saving to doc directory? – iDev Nov 20 '12 at 04:22
  • Hi frnd. Can you share your PDF code logic with me. my email ID is nav.memane@yahoo.com. Actually I am not able to save the PDF file. My new PDF file is overlapping the first one so I could not able to merge the file later – Navnath Memane May 20 '13 at 08:57