1

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

4

1 回答 1

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];
于 2012-06-19T05:18:28.023 回答