【发布时间】:2017-02-19 09:55:22
【问题描述】:
我有一张相机拍摄的照片,这张照片是 720x720 像素。 我需要生成包含此图片的 PDF。 当我以 600 dpi 打印此 PDF 时,我需要图片正好是 2x2 英寸(如果图像必须放大一点也没问题)。
我已经创建了 PDF 上下文并这样写:
CGFloat ratio = 600.0f/72.0f;
CGFloat contextWidth = floorf(imageWidth * ratio);
CGFloat contextHeight = floorf(imageHeight * ratio);
CGSize PDFContextSize = CGSizeMake(contextWidth, contextHeight);
CGRect mediaBox = CGRectZero;
mediaBox.size = PDFContextSize;
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &mediaBox, auxillaryInformation);
CGImageRef imageRef = [image CGImage];
CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawImage(pdfContext, CGRectMake(0.0f, 0.0f, mediaBox.size.width, mediaBox.size.height), imageRef);
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
当我以 600 dpi 打印此代码创建的 PDF 时,打印的图像有 2.4 x 2.4 英寸,而不是 2 x 2 英寸。
有什么想法吗?
编辑:这是我用于打印的代码。是的,我将比例因子设置为 1,以防止图片缩放。
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithData:pdfData];
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSRect printRect = [printInfo imageablePageBounds];
PDFPage *firstPage = [pdfDoc pageAtIndex:0];
NSRect bounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
NSSize pixelSize = bounds.size;
NSRect frame = NSMakeRect(0.0f, 0.0f, 0.0f, 0.0f);
frame.size = pixelSize;
PDFView *pdfView = [[PDFView alloc] initWithFrame:frame];
[pdfView setScaleFactor:1.0f];
[pdfView setDocument: pdfDoc];
[printInfo setTopMargin:verticalMargin];
[printInfo setBottomMargin:verticalMargin];
[printInfo setLeftMargin:horizontalMargin];
[printInfo setRightMargin:horizontalMargin];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticalPagination:NSFitPagination];
[printInfo setVerticallyCentered:YES];
[printInfo setHorizontallyCentered:YES];
[printInfo setScalingFactor:1.0f];
NSPrintOperation *myop = [NSPrintOperation printOperationWithView:pdfView printInfo:printInfo];
[myop runOperation];
此代码打印的图像是 2.4x2.4 英寸,而不是 2x2 英寸。
【问题讨论】:
标签: ios macos cocoa pdf-generation