I have a picture taken by a camera and this picture is 720x720 pixels. I need to generate a PDF that contains this picture. When I print this PDF at 600 dpi I need the picture to be exactly 2x2 inches (no problem if the image has to scale up a bit).
I have created the PDF context and write to it like this:
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);
When I print the PDF created by this code at 600 dpi the printed image has 2.4 x 2.4 inches , instead of 2 x 2 inches.
Any ideas?
EDIT : This is the code I am using for printing. Yes, I am setting the scale factor to 1, to prevent the picture from scaling it.
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];
the image printed by this code is 2.4x2.4 inches instead of 2x2 inches.