2010-08-31 117 views
0

我想從現有的pdf文檔創建一個新的PDF文件。那是我有一個pdf文件說「old.pdf」,並從該用戶可以選擇一些頁面,並從那些選定的頁面我想創建新的PDF文件,說「temp1.pdf」。是否有可能創建「temp1.pdf」? Plz幫助我,並提供一些代碼示例來做到這一點。 謝謝。iphone從pdf頁面創建PDF文件

+0

你的問題到底是什麼?如何保存到磁盤?如何創建PDF文件? – 2010-09-01 00:15:03

+0

我已經PDF文件,然後從該PDF文件的選定頁面,我想創建新的PDF文件。 – jaynaiphone 2010-09-01 06:14:05

+0

我已經執行了你的代碼,它的工作很完美。我用不同種類的pdf測試過,我沒有看到任何問題。 – CKT 2010-12-08 13:48:15

回答

0

這是我的演示代碼,你可以試試看。但是這個代碼有一些問題。新生成的PDF出現亂碼文本,我仍然無法解決這個問題(有誰可以幫忙?)。

//old pdf 
NSArray* old_PDF_paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* old_PDF_documentsDirectory = [old_PDF_paths objectAtIndex:0]; 
NSString* old_PDF_ABSOLUTE_PATH = [old_PDF_documentsDirectory stringByAppendingPathComponent:@"old.pdf"]; 

//new generate pdf 
NSArray* output_PDF_paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* output_PDF_documentsDirectory = [output_PDF_paths objectAtIndex:0]; 
NSString* output_PDF_ABSOLUTE_PATH = [output_PDF_documentsDirectory stringByAppendingPathComponent:@"temp1.pdf"]; 
NSString* outpath = output_PDF_ABSOLUTE_PATH; 

NSURL *url = [[NSURL alloc] initFileURLWithPath:old_PDF_ABSOLUTE_PATH]; 
CGPDFDocumentRef originalPDF = CGPDFDocumentCreateWithURL((CFURLRef)url); 

// query for the number of pages 
int ct = CGPDFDocumentGetNumberOfPages(originalPDF); 

CGFloat width = 0.0f; 
CGFloat height = 0.0f; 

CGRect outRect = CGRectMake(0, 0, width, height); 
CFURLRef outurl = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)outpath, kCFURLPOSIXPathStyle, 0); 
CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 

// create a context for drawing 
CGContextRef context = CGPDFContextCreateWithURL ((CFURLRef)outurl, &outRect, myDictionary); // 5 
CFRelease(myDictionary); 

for(int i = 1; i <= ct; ++i) { 
    //page render 
    CGPDFPageRef pdfPage = CGPDFDocumentGetPage(originalPDF, i); 
    CGRect pageMediaBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); 
    CGContextBeginPage (context, &pageMediaBox); 
    CGContextDrawPDFPage(context, pdfPage); 
    CGContextEndPage (context); 
} 

CGContextClosePath(context); 
CGContextRelease(context); 
+0

好的謝謝你的幫助。我會試試這個,讓你知道。 – jaynaiphone 2010-09-14 13:41:10

+0

我實現了這個代碼,它爲originalPDF提取null,因此我無法進一步處理。你能告訴我什麼是補救措施? – Sarah 2011-01-19 07:41:07