2010-09-23 51 views
3

我試圖將pdf頁面繪製到pdf上下文中,然後將其保存到磁盤。我似乎無法弄清楚什麼是錯的。有人能給我幾個指點。謝謝。使用Quartz將PDF文檔保存到磁盤

- (void)testQuartz:(NSData *)pdfDocumentData 
{ 
    //Create the pdf document reference 
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData); 
    CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider); 
    CGDataProviderRelease(dataProvider); //Release the data provider 

    //Create the pdf context 
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 0); 
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0); 

    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData); 
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL); 

    if (CGPDFDocumentGetNumberOfPages(document) > 0) 
    {  
     //Draw the page onto the new context 
     page = CGPDFDocumentGetPage(document, 0); 
     CGContextDrawPDFPage(pdfContext, page); 
    } 
    else 
    { 
     NSLog(@"Failed to create the document"); 
    } 

    //Write to disk 
    [(NSMutableData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES]; 

    //Clean up 
    CGDataConsumerRelease(dataConsumer); 
    CFRelease(mutableData); 
    CGPDFDocumentRelease(document); 
} 
+0

那麼有什麼不工作?它不是在編譯?建築時是否收到錯誤或警告?你的日誌消息是輸出嗎? – Abizern 2010-09-23 17:12:21

+0

代碼執行時,我不會收到任何警告或錯誤。我知道有什麼地方是錯誤的,因爲PDF文件無法打開。 – David 2010-09-23 17:26:22

回答

12

所以我發現它有3個錯誤的東西。

  1. PDF頁面從1開始,而不是0
  2. 需要告訴PDP上下文時,頁面的開始和結束。
  3. 在將數據寫入磁盤之前釋放pdf上下文。這是由於某種原因的關鍵。

以下是修正後的代碼。

- (void)testQuartz:(NSData *)pdfDocumentData 
{  
    //Create the pdf document reference 
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData); 
    CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider); 

    //Create the pdf context 
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1 
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0); 

    //NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height); 
    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData); 
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL); 


    if (CGPDFDocumentGetNumberOfPages(document) > 0) 
    {  
     //Draw the page onto the new context 
     //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1 

     CGPDFContextBeginPage(pdfContext, NULL); 
     CGContextDrawPDFPage(pdfContext, page); 
     CGPDFContextEndPage(pdfContext); 
    } 
    else 
    { 
     NSLog(@"Failed to create the document"); 
    } 

    CGContextRelease(pdfContext); //Release before writing data to disk. 

    //Write to disk 
    [(NSData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES]; 

    //Clean up 
    CGDataProviderRelease(dataProvider); //Release the data provider 
    CGDataConsumerRelease(dataConsumer); 
    CGPDFDocumentRelease(document); 
    CFRelease(mutableData); 
}