2012-06-15 58 views
1

我有一個帶註釋的PDF,並希望將其轉換爲圖像。 PDF已成功轉換爲圖片,但轉換中缺少註釋。如何將帶註釋的PDF轉換爲圖像?

這是代碼:

NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfFile:PdfFilePath]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

[img setCurrentPage:0]; 

NSImage *temp = [[NSImage alloc] init]; 
[temp addRepresentation:img]; 
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]]; 
NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil]; 
NSString *pageName = [NSString stringWithFormat:@"Page1_%d.jpeg", 0]; 
NSString *imgFilePath = [NSString stringWithFormat:@"%@/%@",@"/Users/Desktop/PDFImages", pageName]; 
[fileManager createFileAtPath:imgFilePath contents:finalData attributes:nil]; 

是否有存儲註釋PDF格式的圖像以不同的方式?

回答

0

使用圖像鎖定和解鎖功能解決了該問題。

 NSSize  pageBounds1; 
    pageBounds1 = [page1 boundsForBox: [_pdfView displayBox]].size; 
    NSImage *temp = [[NSImage alloc] initWithSize:pageBounds1]; 

    [temp lockFocus]; 
    [page1 drawWithBox:kPDFDisplayBoxMediaBox]; 
    [temp unlockFocus]; 

    NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]]; 
    NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil]; 
    NSString *pageName = [NSString stringWithFormat:@"Page1_%d.jpg", pageIndex]; 
    NSString *imgFilePath = [NSString stringWithFormat:@"%@/%@",pathForImages, pageName]; 
    [fileManager createFileAtPath:imgFilePath contents:finalData attributes:nil];