合并iOS上的PDF文件 [英] Merge PDF files on iOS

查看:190
本文介绍了合并iOS上的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS中是否有合并PDF文件的方法,即将另一个文件的页面附加到另一个文件的末尾并保存到磁盘?

Is there a way in iOS to merge PDF files, that is, append the pages of one at the end of another and save it to disk?

推荐答案

我出来了这个解决方案:

I came out with this solution:

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

// File paths
NSString *pdfPath1 = [documentsDirectory stringByAppendingPathComponent:@"1.pdf"];
NSString *pdfPath2 = [documentsDirectory stringByAppendingPathComponent:@"2.pdf"];
NSString *pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:@"out.pdf"];

// File URLs
CFURLRef pdfURL1 = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath1];
CFURLRef pdfURL2 = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath2];
CFURLRef pdfURLOutput = (CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPathOutput];

// File references
CGPDFDocumentRef pdfRef1 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL1);
CGPDFDocumentRef pdfRef2 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL2);

// Number of pages
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2);

// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

// Loop variables
CGPDFPageRef page;
CGRect mediaBox;

// Read the first PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 1 (%i)...", numberOfPages1);
for (int i=1; i<=numberOfPages1; i++) {
    page = CGPDFDocumentGetPage(pdfRef1, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);
}

// Read the second PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 2 (%i)...", numberOfPages2);
for (int i=1; i<=numberOfPages2; i++) {
    page = CGPDFDocumentGetPage(pdfRef2, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);      
}
NSLog(@"DONE!");

// Finalize the output file
CGPDFContextClose(writeContext);

// Release from memory
CFRelease(pdfURL1);
CFRelease(pdfURL2);
CFRelease(pdfURLOutput);
CGPDFDocumentRelease(pdfRef1);
CGPDFDocumentRelease(pdfRef2);
CGContextRelease(writeContext);

这里最大的问题是内存分配。正如你所看到的,在这种方法中,你必须读取要合并的PDF文件,同时生成输出。发布只发生在结束。我试图结合一个PDF文件与500页(〜15MB)与另一个包含100页(〜3MB),它产生了一个600页(当然!)只有〜5MB大小的一个新的(魔术?)。执行花了大约30秒(不是那么糟糕,考虑到iPad 1)和分配17MB(哎哟!)。应用程序幸运没有崩溃,但我认为iOS会喜欢杀死一个应用程序消耗17MB像这一个。 ; P

The biggest issue here is memory allocation. As you can see, in this approach you have to read both PDF files you want to merge with and, at the same time, generate the output. The releases only occur at the end. I tried combining a PDF file with 500 pages (~15MB) with another containing 100 pages (~3MB) and it produced a new one with 600 pages (of course!) having only ~5MB size (magic?). The execution took around 30 seconds (not so bad, considering an iPad 1) and allocated 17MB (ouch!). The app luckily didn't crash, but I think iOS would love to kill an app consuming 17MB like this one. ;P

这篇关于合并iOS上的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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