将多个pdf文档合并成单个文档不起作用

我试图将11个PDF文件合并成一个PDF文件。我正在使用下面的代码,但在最后的PDF只显示第一个pdf文件…我nslogged pdfurls和CGPDFDocumentRef在循环中,他们不是零(在循环中)。可能是为什么在最终文档中只显示第一页的原因

-(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]; } 

它看起来像你的代码假设每个文档中只有一个页面,但是它打开每个文件时要求页面pageNumber ,因此要求页面1 page_1.pdf,页面2 page_2.pdf,第3页from page_3.pdf等…

如果你只是想从每个文件的第一页改变这一点:

 CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber); 

对此:

 CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, 1); 

在我发现这个基础上,我已经有了(原谅我,但在ARC项目,所以你将不得不重新做你的内存pipe理)之前,我重写了你的例程,如下所示:

(注意:错误检查已被删除,使代码更易读!)

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

如果你想要的是所有源PDF文件的所有页面,你的for循环是错误的。

你循环计数器'pageNumber'运行从1到11.你正在使用相同的variables来打开相应的文件,以及从该pdf中获取页面。 所以,你的for循环会产生一个pdf

第1页的第1页,第2页的第2页,第11页的第11页

如果你的2nd-11th pdf文件没有那么多的页面,那么最终的结果显然只有第一个pdf的第一页。

你需要2个循环。 一个遍历PDF文件,另一个遍历每个PDF文件的每个页面。

  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]; }