Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine multiple pdf files into a single pdf file programmatically in iphone?

Tags:

ios

pdf

iphone

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

like image 349
sujith1406 Avatar asked Mar 10 '12 11:03

sujith1406


People also ask

How do I put multiple PDFs into one PDF on my iPhone?

Open Acrobat online services from any browser, then tap on the “Select files” button. Navigate to the files you wish to combine and tap to select each one. In the list of files, you can add more by tapping the plus sign. When you have all the files you need, tap the “Merge” button.

Can you combine files on iPhone?

Open Files. Navigate to the folder with the PDFs you want to merge. In the upper-right corner, tap Select. Tap each PDF in the order you want them to appear as a merged PDF.

How do I automatically merge PDF files?

Merge All PDF Files In A Folder Using A Desktop FlowOpen Power Automate Desktop and create a new flow called Merge PDF Document. We want the user to manually select the folder where there are PDFs waiting to be merged. Add a Display select folder dialog action with the title “Select Folder With PDFs To Merge.”


2 Answers

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!

like image 130
cocoakomali Avatar answered Nov 02 '22 03:11

cocoakomali


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):

- (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);
    }
}
like image 26
lnafziger Avatar answered Nov 02 '22 01:11

lnafziger