【问题标题】:combining multiple pdf documents into single document not working将多个 pdf 文档合并为单个文档不起作用
【发布时间】:2012-03-12 13:36:04
【问题描述】:

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

-(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];
    }

【问题讨论】:

  • 尝试使用 CGContextBeginPage 而不是 UIGraphicsBeginPDFPageWithInfo 创建页面
  • @lukya 试过了,但没有帮助
  • 你用什么来查看pdf并确定它只有一页?
  • @Inafziger 我正在对我的应用程序的文档文件夹进行操作,并使用 adobe reader 打开 pdf 文档。
  • 好的,我在适用于 iOS 的 pdf 阅读器上遇到了同样的问题,结果证明是一个错误,阅读器只显示第一页(即使它们都在那里)。

标签: iphone ios pdf


【解决方案1】:

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

如果您只想从每个文档的第一页更改此:

CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);

对此:

CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, 1);

为了它的价值,在我发现这个之前,我根据我已经拥有的一个例程重新编写了你的​​例程(请原谅我,但它是在一个 ARC 项目中,所以你必须重新进行内存管理)如下:

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

-(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);
}

【讨论】:

  • CGContextDrawPDFPage(context, pdfPage); 后面缺少一个CGContextEndPage(context); 否则你会生成一个日志警告:不要嵌套调用这个函数——结果不会是你期望的。跨度>
【解决方案2】:

如果你想要的是所有源 PDF 文件的所有页面,那么你的 for 循环是错误的。

您循环计数器“pageNumber”从 1 运行到 11。您正在使用相同的变量来打开相应的文件以及从该 pdf 中获取页面。 因此,您的 for 循环将生成一个带有

的pdf

第 1 个 pdf 的第 1 页,第 2 个 pdf 的第 2 页,....,第 11 个 pdf 的第 11 页

如果您的第 2 - 11 个 pdf 文件没有那么多页,最终输出显然只有第一个 pdf 的第一页。

您需要 2 个 for 循环。一个用于遍历 pdf 文件,另一个用于遍历每个 pdf 文件的每一页。

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

        NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",documentNumber]] 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);
        int numberOfPages = CGPDFDocumentGetNumberOfPages(newDocument);

        for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++)
        {
            CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);
            CGContextDrawPDFPage (currentContext, newPage);
            //any other page rendering
            newPage = nil;       
        }

        CGPDFDocumentRelease(newDocument);
        newDocument = nil;
        [pdfUrl release];            
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多