It looks like your code assumes that there is only one page in each document, however it requests the pageNumber page from each file when it is opened and therefore requests page 1 from page_1.pdf, page 2 from page_2.pdf, page 3 from page_3. pdf etc.
If you want the first page from each document to change this:
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);
:
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, 1);
For what it's worth, I rewrote your routine before I saw it based on what I already have (forgive me, but this is in an ARC project, so you have to redo the memory management) as follows
(NOTE: The error check has been removed to make the code more readable!)
-(void)mergeDocuments { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *oldFilePath=[documentsDirectory stringByAppendingPathComponent:@"finalPdf.pdf"]; NSURL *oldFileUrl = [NSURL fileURLWithPath:oldFilePath]; CGContextRef context = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)oldFileUrl, NULL, NULL); for (int docNumber = 1; docNumber <= 11; docNumber++) {
lnafziger
source share