0

I'm trying to print PDF-files that are read from the file system. But can't quite get it to work. I know that I have to have the file in a view and then print the view.

This is my print method:

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSPrintOperation *PDFPrint = [NSPrintOperation printOperationWithView:_pdfView printInfo:printInfo];
[PDFPrint setCanSpawnSeparateThread:YES];
[PDFPrint setShowsProgressPanel:YES];
[PDFPrint runOperation];

And this is how I create the view:

PDFDocument *pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];

PDFPage *firstPage = [pdfDocument pageAtIndex:0];
NSRect bounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
NSSize pixelSize = bounds.size;
NSSize documentSize;

documentSize.width = pixelSize.width;
documentSize.height = pixelSize.height;
PDFView *PDFDocumentView = [[PDFView alloc] initWithFrame:NSMakeRect(0, 0, documentSize.width, documentSize.height)];

[PDFDocumentView setDisplayMode:kPDFDisplaySinglePage];
[PDFDocumentView setDisplayBox:kPDFDisplayBoxMediaBox];
[PDFDocumentView setAutoScales:NO];
[PDFDocumentView setDocument:pdfDocument];
[PDFDocumentView setBackgroundColor:[NSColor redColor]];

return PDFDocumentView;

When I've tweaked this back and forth I've managed to print the PDF but it's not properly aligned.

About the alignment. I can see the red background I've created and I'm not able to see part of the right side of the pdf.

So how do I best read and then print PDF files?

Any ideas?

Mikael
  • 3,572
  • 1
  • 30
  • 43

1 Answers1

0

It seems that it scales correct if I instead use:

[PDFDocumentView printWithInfo:printInfo autoRotate:NO];
Mikael
  • 3,572
  • 1
  • 30
  • 43