将pdf页面添加到现有的pdf objective-c [英] Add pdf page to an existing pdf objective-c

查看:118
本文介绍了将pdf页面添加到现有的pdf objective-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iphone / ipad编写一个应用程序,将相机图像(.png)转换为pdf并保存到/ user / documents文件夹。现在我想弄清楚如何将另一个pdf附加到现有文档,以便它们成为多页。基本上,如果您将doc1.pdf和doc2.pdf保存在文档文件夹中,您将如何合并2个pdf,以便它们成为1个包含2个页面的文档?

I am writing an app for an iphone/ipad that converts camera image (.png) to pdf and save to the /user/documents folder. Now I'm trying to figure out how to append another pdf to an existing document so they will become multipage. Basically, if you have doc1.pdf and doc2.pdf saved in the documents folder how would you merge the 2 pdfs so they become 1 document with 2 pages?

感谢任何你们可以给我的帮助或建议。

Appreciate any help or recommendations you guys can give me.

谢谢,

推荐答案

以下是将一个pdf附加到另一个上的示例代码:

Here is a sample code for appending one pdf over another one:

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

// File paths
NSString *pdfPath1 = [documentsDirectory stringByAppendingPathComponent:@"pdf1.pdf"];
NSString *pdfPath2 = [documentsDirectory stringByAppendingPathComponent:@"pdf2.pdf"];
NSString *pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:@"pdfout.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(@"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(@"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 objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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