在iPhone上,是否可以通过编程将多个PDF文件合并为单个PDF文件?

8

在iPhone上,是否有可能通过编程将多个PDF文件合并成单个PDF文件。我已经从程序的不同部分创建了单独的页面,现在需要将它们合并成一个文件。


3个回答

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!


我按照上述方法使用NSData完成了PDF合并,但问题是最终合并的文档中只有第一页被打印出来。我已在http://stackoverflow.com/questions/9668047/combining-multiple-pdf-documents-into-single-document-not-working发布了这个问题。 - sujith1406
@cocoakomali:你能帮忙解决以下这个问题吗?合并的文件没有被显示。http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios - Navnath Memane
通过应用上述解决方案,我逐页添加,但首先所有页面都是空白的。只有最后一页可见。请建议我在http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios#comment24146255_16646039上出了什么问题。 - Navnath Memane

5

这是我编写的一个例程,它可以将PDF文件的URL添加到您已经创建的上下文中(这样您就可以调用它来处理多个文件并将它们全部附加):

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

请注意,由于您正在从PDF写入到PDF,因此在编写之前无需进行翻译/缩放。 - lnafziger

1

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

  1. 通过查看API,您应该能够读取所有输入的PDF文档
  2. 获取它们的PDF页面
  3. 为您的新文档创建一个PDF上下文
  4. 在PDF上下文中绘制PDF页面

请注意,PDF页面绘制代码应该在begin page和end page调用之间。

CGPDFDocumentCreateWithURL
CGContextBeginPage
CGPDFDocumentGetPage
CGPDFContextCreateWithURL
CGContextDrawPDFPage
CGContextEndPage

查看苹果文档以获取更多信息

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

看看这是否有帮助,或者如果您需要一些示例伪代码,请告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接