如何将PDF转换为NSImage中,改变DPI?(How to Convert PDF to NSI

2019-10-20 03:12发布

我试图PDF页面转换为NSImage中,并保存为JPG文件成功。 然而,输出结果作为正常72 DPI。 我想了DPI切换到300 DPI,但失败了。 下面是代码:

- (IBAction)TestButton:(id)sender {

    NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"1.pdf"];

    NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
    NSPDFImageRep *pdfImg = [NSPDFImageRep imageRepWithData:pdfData];


    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSInteger pageCount = [pdfImg pageCount];



    for(int i = 0 ; i < pageCount ; i++) {
        [pdfImg setCurrentPage:i];
        NSImage *temp = [[NSImage alloc] init];
        [temp addRepresentation:pdfImg];

        CGFloat factor = 300/72; // Scale from 72 DPI to 300 DPI
        //NSImage *img; // Source image
        NSSize newSize = NSMakeSize(temp.size.width*factor, temp.size.height*factor);
        NSImage *scaledImg = [[NSImage alloc] initWithSize:newSize];
        [scaledImg lockFocus];
        [[NSColor whiteColor] set];
        [NSBezierPath fillRect:NSMakeRect(0, 0, newSize.width, newSize.height)];
        NSAffineTransform *transform = [NSAffineTransform transform];
        [transform scaleBy:factor];
        [transform concat];
        [temp drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
        [scaledImg unlockFocus];


        NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
        NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
        NSString *pageName = [NSString stringWithFormat:@"Page_%ld.jpg", (long)[pdfImg currentPage]];
        [fileManager createFileAtPath:[NSString stringWithFormat:@"%@%@", pdfPath, pageName] contents:finalData attributes:nil];
    }
}

Answer 1:

由于OS X 10.8, NSImage中具有基于块初始化剂绘制基于矢量内容转换成一个位图。
我们的想法是提供每当请求图像的表示被称为绘图处理器。
点和像素之间的关系通过使NSSize(在分)初始化剂和显式设置为表示像素尺寸来表示:

NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"1.pdf"];
NSData* pdfData = [NSData dataWithContentsOfFile:pdfPath];
NSPDFImageRep* pdfImageRep = [NSPDFImageRep imageRepWithData:pdfData];
CGFloat factor = 300/72;
NSInteger pageCount = [pdfImageRep pageCount];
for(int i = 0 ; i < pageCount ; i++)
{
    [pdfImageRep setCurrentPage:i];
    NSImage* scaledImage = [NSImage imageWithSize:pdfImageRep.size flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
        [pdfImageRep drawInRect:dstRect];
        return YES;
    }];
    NSImageRep* scaledImageRep = [[scaledImage representations] firstObject];
    /*
     * The sizes of the PDF Image Rep and the [NSImage  imageWithSize: drawingHandler:]-context
     * are defined in terms of points.
     * By explicitly setting the size of the scaled representation in Pixels, you 
     * define the relation between points & pixels.
     */
    scaledImageRep.pixelsWide = pdfImageRep.size.width * factor;
    scaledImageRep.pixelsHigh = pdfImageRep.size.height * factor;
    NSBitmapImageRep* pngImageRep = [NSBitmapImageRep imageRepWithData:[scaledImage TIFFRepresentation]];
    NSData* finalData = [pngImageRep representationUsingType:NSJPEGFileType properties:nil];
    NSString* pageName = [NSString stringWithFormat:@"Page_%ld.jpg", (long)[pdfImageRep currentPage]];
    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@%@", pdfPath, pageName] contents:finalData attributes:nil];
}


Answer 2:

您可以通过NSImageRep的大小设置为大于图像的大小以外设置保存在图像文件的元数据的决议

[pngImageRep setSize:NSMakeSize(targetWith, targetHeight)]

在那里你必须targetWidth和targetHeight初始化到你想要的值

编辑:我猜你想写“scaledImg”不是“临时”

NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[scaledImg TIFFRepresentation]];

编辑2:关于第二个想法,这将让你一个较大的图像,但只作为一个小的伸出来的版本。 在weichsel与下面的修改答案的方法可能是你真正想要的(但上面的代码仍然有效用于设置元数据)

NSSize newSize = NSMakeSize(pdfImageRep.size.width * factor,pdfImageRep.size.height * factor);
NSImage* scaledImage = [NSImage imageWithSize:newSize flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
    [pdfImageRep drawInRect:dstRect];
    return YES;
}];


文章来源: How to Convert PDF to NSImage and change the DPI?