将多个pdf文档合并到单个文档中不起作用

时间:2012-03-12 13:36:04

标签: iphone ios pdf

我正在尝试将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];
    }

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

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

循环计数器'pageNumber'从1运行到11.您使用相同的变量打开相应的文件以及从该pdf中获取页面。 所以,你的for循环将产生一个

的pdf
  

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

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

你需要2个循环。一个用于遍历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];            
    }
相关问题