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

前端 未结 3 1698
借酒劲吻你
借酒劲吻你 2020-12-08 23:52

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 nee

相关标签:
3条回答
  • 2020-12-09 00:33

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 00:39

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

    1. By looking into APIs you should be able to read all input PDF document
    2. get PDF Page for them
    3. create a PDF Context for your new document
    4. draw PDF Pages in PDF Context

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

    CGPDFDocumentCreateWithURL
    CGContextBeginPage
    CGPDFDocumentGetPage
    CGPDFContextCreateWithURL
    CGContextDrawPDFPage
    CGContextEndPage
    

    Check out Apple docs for

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

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

    0 讨论(0)
  • 2020-12-09 00:44

    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!

    0 讨论(0)
提交回复
热议问题