我有一张照相机拍摄的照片,这张照片是720x720像素。 我需要生成包含此图片的PDF。 当我以600 dpi打印此PDF时,我需要将图片精确到2 x 2英寸(如果图像需要放大一点,则不会出现问题)。如何生成必须以英寸以特定尺寸打印的PDF?
我已创建的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);
当我打印由该代码以600dpi打印的图像具有2.4×2.4英寸创建的PDF,而不是2×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英寸。
该OP写道“我有一张照相机拍摄的照片,这张照片是720x720像素” - 这些像素没有固有的“尺寸”。无论OP的问题是什么,它肯定不会将它们视为屏幕像素的1/96英寸的Windows默认值。 – CRD
我的答案不是确定的答案。我只想指出一些事情。我无法解释为什么720 x 720像素的图像产生2.4 x 2.4英寸的PDF。我也不知道OP是否在讨论打印尺寸(可能由打印机缩放)或查看器报告的尺寸。 –
我用我用来打印PDF的代码更新了这个问题。 – SpaceDog