Combining multiple PDFs into one document does not work

I am trying to combine 11 pdf files into one pdf file. The following code, which I use, but in the final pdf, only the first PDF is shown ... I nslogged pdfurls and CGPDFDocumentRef in a loop, and they are not zero all the time (in a loop). What could be the reason why only the first page is displayed in the final document

-(void)mergeDocuments { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder NSString *oldFile=[documentsDirectory stringByAppendingPathComponent:@"finalPdf.pdf"]; NSMutableData *data=[[NSMutableData alloc] init]; CGRect paperSize=CGRectMake(0,0,kDefaultPageWidth,kDefaultPageHeight); UIGraphicsBeginPDFContextToData(data, paperSize, nil); for (int pageNumber = 1; pageNumber <= 11; pageNumber++) { NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",pageNumber]] retain]; NSURL *pdfUrl = [[NSURL fileURLWithPath:pdfPath] retain]; UIGraphicsBeginPDFPageWithInfo(paperSize, nil); CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(currentContext, 0, paperSize.size.height); CGContextScaleCTM(currentContext, 1.0, -1.0); CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) pdfUrl); CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber); CGContextDrawPDFPage (currentContext, newPage); newPage = nil; CGPDFDocumentRelease(newDocument); newDocument = nil; [pdfUrl release]; } NSURL *finalUrl=[NSURL URLWithString:oldFile]; UIGraphicsEndPDFContext(); [data writeToURL:finalUrl atomically:YES]; } 
+2
source share
2 answers

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++) { // Get the first page from each source document NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",docNumber]]; NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath]; CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfUrl); CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, 1); CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); // Copy the page to the new document CGContextBeginPage(context, &pdfCropBoxRect); CGContextDrawPDFPage(context, pdfPage); // Close the source files CGContextEndPage(context); CGPDFDocumentRelease(pdfDoc); } // Cleanup CGContextRelease(context); } 
+3
source

If you need all the pages of all the PDF source files, your for loop is incorrect.

The cycle counter "page number" runs from 1 to 11. You use the same variable to open the corresponding file and also extract the page from this pdf. So your for loop will create pdf with

1st page of 1st pdf, 2nd page of 2nd pdf, ...., 11th page of 11th pdf

If your 2nd - 11th PDF files do not have as many pages, the final result will obviously only have the first page of the first pdf.

You need 2 for cycles. One for iterating through pdf files, and another for repeating each page of each pdf file.

  for (int documentNumber = 1; documentNumber <= 11; documentNumber++) { NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",documentNumber]] retain]; NSURL *pdfUrl = [[NSURL fileURLWithPath:pdfPath] retain]; UIGraphicsBeginPDFPageWithInfo(paperSize, nil); CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(currentContext, 0, paperSize.size.height); CGContextScaleCTM(currentContext, 1.0, -1.0); CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) pdfUrl); int numberOfPages = CGPDFDocumentGetNumberOfPages(newDocument); for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++) { CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber); CGContextDrawPDFPage (currentContext, newPage); //any other page rendering newPage = nil; } CGPDFDocumentRelease(newDocument); newDocument = nil; [pdfUrl release]; } 
+1
source

All Articles