将多个pdf文档合并到单个文档中不起作用 [英] combining multiple pdf documents into single document not working

查看:121
本文介绍了将多个pdf文档合并到单个文档中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将11个pdf文件合并到一个pdf文件中。我正在使用以下代码,但在最后的pdf中只显示了第一个pdf ...我在循环中记录了pdfurls和CGPDFDocumentRef它们是并非所有时间都是零(在循环中)。这可能是为什么只有第一页显示在最终文档中的原因

I am trying to combine 11 pdf files into a single pdf file.The following code i am using ,but in the final pdf only the first pdf is shown ...i nslogged the pdfurls and CGPDFDocumentRef in the loop and they are not nil all the time(in the loop).What may be the reason why only the first page is displayed in the final document

-(void)mergeDocuments
    {


        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

        NSString *oldFile=[documentsDirectory stringByAppendingPathComponent:@"finalPdf.pdf"];
        NSMutableData *data=[[NSMutableData alloc] init];
         CGRect paperSize=CGRectMake(0,0,kDefaultPageWidth,kDefaultPageHeight);
        UIGraphicsBeginPDFContextToData(data, paperSize, nil);

        for (int pageNumber = 1; pageNumber <= 11; pageNumber++)
        {

            NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",pageNumber]] retain];



            NSURL *pdfUrl = [[NSURL fileURLWithPath:pdfPath] retain];



            UIGraphicsBeginPDFPageWithInfo(paperSize, nil);


            CGContextRef currentContext = UIGraphicsGetCurrentContext();


            CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
            CGContextScaleCTM(currentContext, 1.0, -1.0); 



            CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) pdfUrl);

            CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);


            CGContextDrawPDFPage (currentContext, newPage);
            newPage = nil;       
            CGPDFDocumentRelease(newDocument);
            newDocument = nil;
            [pdfUrl release];

        }

        NSURL *finalUrl=[NSURL URLWithString:oldFile];



        UIGraphicsEndPDFContext(); 
[data writeToURL:finalUrl atomically:YES];
    }


推荐答案

看起来您的代码假设每个文档中只有一个页面,但是当它打开时,每个文件都要求页面 pageNumber ,因此要求page_1.pdf中的第1页,第2页来自page_2.pdf,第3页来自page_3.pdf等...

It looks like your code assumes that there is only one page in each document, however it is asking for page pageNumber from each file as it opens it, and is therefore asking for page 1 from page_1.pdf, page 2 from page_2.pdf, page 3 from page_3.pdf, etc...

如果您只是希望每个文档的第一页改变这一点:

If you just want the first page from each document change this:

CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);

到此:

CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, 1);

为了它的价值,我在我发现之前重新编写了你的​​例行程序(原谅我,但它在ARC项目中,所以你必须重新进行内存管理)如下:

For what it's worth, I re-wrote your routine before I spotted this based on one that I already have (forgive me but it is in an ARC project so you'll have to re-do your memory management) as follows:

(注意:检查错误已删除以使代码更具可读性!)

-(void)mergeDocuments {
    NSArray     *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString    *documentsDirectory = [paths objectAtIndex:0];

    NSString    *oldFilePath=[documentsDirectory stringByAppendingPathComponent:@"finalPdf.pdf"];
    NSURL       *oldFileUrl = [NSURL fileURLWithPath:oldFilePath];

    CGContextRef context = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)oldFileUrl, NULL, NULL);

    for (int docNumber = 1; docNumber <= 11; docNumber++)
    {
        // Get the first page from each source document
        NSString            *pdfPath        = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",docNumber]];
        NSURL               *pdfUrl         = [NSURL fileURLWithPath:pdfPath];
        CGPDFDocumentRef    pdfDoc          = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfUrl);
        CGPDFPageRef        pdfPage         = CGPDFDocumentGetPage(pdfDoc, 1);
        CGRect              pdfCropBoxRect  = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);

        // Copy the page to the new document
        CGContextBeginPage(context, &pdfCropBoxRect);
        CGContextDrawPDFPage(context, pdfPage);

        // Close the source files
        CGContextEndPage(context);
        CGPDFDocumentRelease(pdfDoc);         
    }

    // Cleanup
    CGContextRelease(context);
}

这篇关于将多个pdf文档合并到单个文档中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆