2012-03-10 84 views

回答

5

是的,你可以做到这一点。请按照下面的代码

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

所以你必须写在绘制页面之前从适当的PDF采取适当的页面的必要条件。您已经创建了来自多个来源的PDF!

+0

我做了我的pdf结合如上所述,但与nsdata.the问题是,只有第一页打印在最终的组合document.this问题我已发布在http://stackoverflow.com/questions/9668047/combining-多pdf文档到单文档不工作 – sujith1406 2012-03-12 13:49:43

+0

已发布到此问题链接的答案是正确的。 – cocoakomali 2012-03-12 18:00:55

+0

@cocoakomali:你可以请帮助以下SO合并文件没有得到显示http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge- in-ios – 2013-05-26 16:50:25

1

尝试使用Quartz 2D API。它对阅读PDF文件有非常好的支持。

  1. 通过寻找到的API,你应该能够读取所有输入PDF 文件
  2. 获取PDF页面他们
  3. 创建一个PDF上下文为新文档
  4. 在PDP上下文
  5. 平局PDF页面

请注意,PDF页面的绘制代码应与开始页面和结束页面调用

CGPDFDocumentCreateWithURL 
CGContextBeginPage 
CGPDFDocumentGetPage 
CGPDFContextCreateWithURL 
CGContextDrawPDFPage 
CGContextEndPage 

退房苹果文档的

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

退房,如果有帮助,还是让我知道,如果你需要一些示例伪代码。

5

下面是我写的采取一个网址给PDF并将它添加到你已经创建了一个上下文(这样就可以调用它的多个文件,并追加其全部)例行:

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

请注意,既然您正在从pdf写入pdf,在写入之前您不必翻译/缩放它。 – lnafziger 2012-03-10 18:44:53

相关问题