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

查看:66
本文介绍了在 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 文件,同时生成输出.释放只发生在最后.我尝试将一个包含 500 页(~15MB)的 PDF 文件与另一个包含 100 页(~3MB)的 PDF 文件结合起来,它产生了一个只有 ~5MB 大小(魔术?)的 600 页(当然!)的新文件.执行大约需要 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天全站免登陆