如何将带注释的PDF转换为图像?

时间:2012-06-15 07:13:48

标签: cocoa

我有一个带注释的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存储到图像中?

1 个答案:

答案 0 :(得分: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];