iphone – 在iOS上合并PDF文件

iphone – 在iOS上合并PDF文件,第1张

概述有没有办法在iOS中合并PDF文件,也​​就是说,将另一个的页面添加到另一个的最后,并将其保存到磁盘? 我出来了这个解决方案: // Documents dirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDi 有没有办法在iOS中合并pdf文件,也​​就是说,将另一个的页面添加到另一个的最后,并将其保存到磁盘?解决方法 我出来了这个解决方案:

// documents dirNSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);Nsstring *documentsDirectory = [paths objectAtIndex:0];// file pathsNsstring *pdfPath1 = [documentsDirectory stringByAppendingPathComponent:@"1.pdf"];Nsstring *pdfPath2 = [documentsDirectory stringByAppendingPathComponent:@"2.pdf"];Nsstring *pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:@"out.pdf"];// file URLsCFURLRef pdfURL1 = (CFURLRef)[[NSURL alloc] initfileURLWithPath:pdfPath1];CFURLRef pdfURL2 = (CFURLRef)[[NSURL alloc] initfileURLWithPath:pdfPath2];CFURLRef pdfURLOutput = (CFURLRef)[[NSURL alloc] initfileURLWithPath:pdfPathOutput];// file referencesCGpdfdocumentRef pdfRef1 = CGpdfdocumentCreateWithURL((CFURLRef) pdfURL1);CGpdfdocumentRef pdfRef2 = CGpdfdocumentCreateWithURL((CFURLRef) pdfURL2);// Number of pagesNSInteger numberOfPages1 = CGpdfdocumentGetNumberOfPages(pdfRef1);NSInteger numberOfPages2 = CGpdfdocumentGetNumberOfPages(pdfRef2);// Create the output contextCGContextRef writeContext = CGPdfcontextCreateWithURL(pdfURLOutput,NulL,NulL);// Loop variablesCGpdfpageRef page;CGRect mediaBox;// Read the first pdf and generate the output pagesNSLog(@"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 pagesNSLog(@"GENErating PAGES FROM pdf 2 (%i)...",numberOfPages2);for (int i=1; i<=numberOfPages2; i++) {    page = CGpdfdocumentGetPage(pdfRef2,page);    CGContextEndPage(writeContext);      }NSLog(@"DONE!");// Finalize the output fileCGPdfcontextClose(writeContext);// Release from memoryCFRelease(pdfURL1);CFRelease(pdfURL2);CFRelease(pdfURLOutput);CGpdfdocumentRelease(pdfRef1);CGpdfdocumentRelease(pdfRef2);CGContextRelease(writeContext);

这里最大的问题是内存分配.您可以看到,在这种方法中,您必须阅读要合并的两个pdf文件,同时生成输出.这些版本只在最后发生.我尝试将一个500页(约15MB)的pdf文件与另外一个包含100页(〜3MB)的pdf文件结合起来,并且生成了一个只有大约5MB大小(大约为5MB)的600页(当然!)的新的pdf文件.执行时间约为30秒(不用考虑iPad 1),并分配17MB(uch!).该应用程序幸运地没有崩溃,但我认为iOS会喜欢杀死一个这样的消费17MB的应用程序.,P

总结

以上是内存溢出为你收集整理的iphone – 在iOS上合并PDF文件全部内容,希望文章能够帮你解决iphone – 在iOS上合并PDF文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1083743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存