我正在为iphone/ipad编写应用程序,将相机图像(.png)转换为pdf并保存到/ user/documents文件夹。现在我试图找出如何将另一个pdf附加到现有文档,以使它们成为多页。基本上,如果您将doc1.pdf和doc2.pdf保存在文档文件夹中,那么您如何合并这两个pdf使其成为一个包含两页的文档?将pdf页面添加到现有pdf objective-c
感谢任何帮助或建议,你们可以给我。
感谢,
我正在为iphone/ipad编写应用程序,将相机图像(.png)转换为pdf并保存到/ user/documents文件夹。现在我试图找出如何将另一个pdf附加到现有文档,以使它们成为多页。基本上,如果您将doc1.pdf和doc2.pdf保存在文档文件夹中,那么您如何合并这两个pdf使其成为一个包含两页的文档?将pdf页面添加到现有pdf objective-c
感谢任何帮助或建议,你们可以给我。
感谢,
下面是在另一个附加一个PDF格式的样本代码:
// 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);
谢谢。这条线给我0x0。 CFURLRef pdfURL1 =(CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath1];我证实了我的pdf的路径是正确的。我正在使用ARC。 – user1834869
@ user1834869,看起来像阅读PDF文件的一些问题。你通常如何做?您是否检查过该代码是否可以在保存到doc目录后读回它? – iDev
嗨frnd。你可以与我分享你的PDF代码逻辑吗?我的电子邮件ID是[email protected]。其实我无法保存PDF文件。我的新PDF文件与第一个PDF文件重叠,所以我以后无法合并文件 –
这是不是这个答案,但如果有人想[追加一个现有的PDF文件](http://stackoverflow.com/a/15355168/1603234) – Hemang
嗨frnd。你可以与我分享你的PDF代码逻辑吗?我的电子邮件ID是[email protected]。其实我无法保存PDF文件。我的新PDF文件与第一个PDF文件重叠,所以我以后无法合并文件 –