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

前端 未结 3 1704
借酒劲吻你
借酒劲吻你 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);
        }
    }
    

提交回复
热议问题