在iphone中是否可以通过编程方式将多个pdf文件合并成单个pdf文件?

在iphone中是否可以通过编程方式将多个pdf文件合并成单个pdf文件?,iphone,ios,pdf,Iphone,Ios,Pdf,是否可以在iphone中以编程方式将多个pdf文件合并为单个pdf文件。我已经从程序的不同部分创建了单独的页面,现在需要将它们合并为单个文件。是的,您可以这样做。请按照下面的代码 //Open a pdf context for the single file UIGraphicsBeginPDFContextToFile(oldFile, paperSize, nil); //Run a loop to the number of pages you want for (pageNumber

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

是的,您可以这样做。请按照下面的代码

//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上下文
UIGraphicsBeginPDFContextToFile(旧文件,纸张大小,无);
//按所需页数运行循环

对于(pageNumber=1;pageNumber试试Quartz 2D API。它对读写PDF文件有很好的支持

  • 通过查看API,您应该能够阅读所有输入PDF 文件
  • 获取他们的PDF页面
  • 为新文档创建PDF上下文
  • 在PDF上下文中绘制PDF页面
  • 请注意,PDF页面绘图代码应介于开始页面调用和结束页面调用之间

    CGPDFDocumentCreateWithURL
    CGContextBeginPage
    CGPDFDocumentGetPage
    CGPDFContextCreateWithURL
    CGContextDrawPDFPage
    CGContextEndPage
    
    查看苹果文档,了解更多信息

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

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

    以下是我编写的一个例程,用于获取pdf的url并将其附加到您已经创建的上下文中(以便您可以调用多个文件并将其全部附加):

    -(void)appendPdfAtURL:(NSURL*)pdfURL to context:(CGContextRef)PDFDestationContext{
    CGPDFDocumentRef pdfDoc=CGPDFDocumentCreateWithURL((uu桥u保留的CFURLRef)pdfURL);
    如果(pdfDoc){
    大小\u t numPages=CGPDFDocumentGetNumberOfPages(pdfDoc);
    如果(数值大于0){
    //循环浏览源文件中的每个页面
    
    对于(size_t i=1;我注意到,由于您是从pdf编写到pdf的,因此在编写之前不必进行翻译/缩放。我按照上面提到的方式进行了pdf合并,但使用了nsdata。问题是最终合并的文档中只打印了第一页。我在@cocoakomali中发布了这个问题:您能帮助我完成以下工作吗因此,通过应用上述解决方案,合并的文件无法显示。我正在逐个添加页面,但首先获得所有页面空白。只有最后一个可见。请告诉我在###Swift 5上哪里出了问题##
    - (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);
        }
    }