是否可以在iphone中以编程方式将多个pdf文件合并为一个pdf文件?

时间:2021-03-22 00:26:19

Is it possible to combine multiple pdf files into a single pdf file programmatically in iphone.I have created individual pages from different parts of my program and now need to merge them into a single file

是否可以在iphone中以编程方式将多个pdf文件合并为一个pdf文件。我已经从程序的不同部分创建了单独的页面,现在需要将它们合并到一个文件中

3 个解决方案

#1


5  

Yes you can do that. Please follow the below code

是的,你可以这么做。请按照以下代码

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

So you have to write necessary condition of taking appropriate pages from appropriate pdf before draw the page. You have created a pdf from multiple sources!

因此,您必须在绘制页面之前编写从适当的pdf中获取适当页面的必要条件。您已从多个来源创建了pdf!

#2


5  

Here is a routine that I wrote to take a url for a pdf and append it to a context that you've already created (so that you can call it for multiple files and append them all):

这是我编写的一个例程,用于获取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);
    }
}

#3


1  

Try Quartz 2D APIs. It has very nice support for reading writing PDF files.

试试Quartz 2D API。它非常适合阅读PDF文件。

  1. By looking into APIs you should be able to read all input PDF document
  2. 通过查看API,您应该能够阅读所有输入的PDF文档

  3. get PDF Page for them
  4. 获取PDF页面

  5. create a PDF Context for your new document
  6. 为新文档创建PDF上下文

  7. draw PDF Pages in PDF Context
  8. 在PDF上下文中绘制PDF页面

Please note that PDF page drawing code should be between begin page and end page calls

请注意,PDF页面绘图代码应位于开始页面和结束页面调用之间

CGPDFDocumentCreateWithURL
CGContextBeginPage
CGPDFDocumentGetPage
CGPDFContextCreateWithURL
CGContextDrawPDFPage
CGContextEndPage

Check out Apple docs for

查看Apple文档

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

Check out if it helps, or let me know if you need some sample pseudo code.

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

#1


5  

Yes you can do that. Please follow the below code

是的,你可以这么做。请按照以下代码

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

So you have to write necessary condition of taking appropriate pages from appropriate pdf before draw the page. You have created a pdf from multiple sources!

因此,您必须在绘制页面之前编写从适当的pdf中获取适当页面的必要条件。您已从多个来源创建了pdf!

#2


5  

Here is a routine that I wrote to take a url for a pdf and append it to a context that you've already created (so that you can call it for multiple files and append them all):

这是我编写的一个例程,用于获取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);
    }
}

#3


1  

Try Quartz 2D APIs. It has very nice support for reading writing PDF files.

试试Quartz 2D API。它非常适合阅读PDF文件。

  1. By looking into APIs you should be able to read all input PDF document
  2. 通过查看API,您应该能够阅读所有输入的PDF文档

  3. get PDF Page for them
  4. 获取PDF页面

  5. create a PDF Context for your new document
  6. 为新文档创建PDF上下文

  7. draw PDF Pages in PDF Context
  8. 在PDF上下文中绘制PDF页面

Please note that PDF page drawing code should be between begin page and end page calls

请注意,PDF页面绘图代码应位于开始页面和结束页面调用之间

CGPDFDocumentCreateWithURL
CGContextBeginPage
CGPDFDocumentGetPage
CGPDFContextCreateWithURL
CGContextDrawPDFPage
CGContextEndPage

Check out Apple docs for

查看Apple文档

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

Check out if it helps, or let me know if you need some sample pseudo code.

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