2012-11-19 69 views
1

我正在爲iphone/ipad編寫應用程序,將相機圖像(.png)轉換爲pdf並保存到/ user/documents文件夾。現在我試圖找出如何將另一個pdf附加到現有文檔,以使它們成爲多頁。基本上,如果您將doc1.pdf和doc2.pdf保存在文檔文件夾中,那麼您如何合併這兩個pdf使其成爲一個包含兩頁的文檔?將pdf頁面添加到現有pdf objective-c

感謝任何幫助或建議,你們可以給我。

感謝,

+0

這是不是這個答案,但如果有人想[追加一個現有的PDF文件](http://stackoverflow.com/a/15355168/1603234) – Hemang

+0

嗨frnd。你可以與我分享你的PDF代碼邏輯嗎?我的電子郵件ID是[email protected]。其實我無法保存PDF文件。我的新PDF文件與第一個PDF文件重疊,所以我以後無法合併文件 –

回答

1

下面是在另一個附加一個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); 

Source

+0

謝謝。這條線給我0x0。 CFURLRef pdfURL1 =(CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPath1];我證實了我的pdf的路徑是正確的。我正在使用ARC。 – user1834869

+0

@ user1834869,看起來像閱讀PDF文件的一些問題。你通常如何做?您是否檢查過該代碼是否可以在保存到doc目錄後讀回它? – iDev

+0

嗨frnd。你可以與我分享你的PDF代碼邏輯嗎?我的電子郵件ID是[email protected]。其實我無法保存PDF文件。我的新PDF文件與第一個PDF文件重疊,所以我以後無法合併文件 –