Cocoa-从文件系统读取PDF文件并打印它们

Cocoa-从文件系统读取PDF文件并打印它们,cocoa,pdf,printing,Cocoa,Pdf,Printing,我正在尝试打印从文件系统读取的PDF文件。但不能完全让它工作。我知道我必须将文件放在视图中,然后打印视图 这是我的打印方法: NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo]; NSPrintOperation *PDFPrint = [NSPrintOperation printOperationWithView:_pdfView printInfo:printInfo]; [PDFPrint setCanSpawnSeparateT

我正在尝试打印从文件系统读取的PDF文件。但不能完全让它工作。我知道我必须将文件放在视图中,然后打印视图

这是我的打印方法:

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSPrintOperation *PDFPrint = [NSPrintOperation printOperationWithView:_pdfView printInfo:printInfo];
[PDFPrint setCanSpawnSeparateThread:YES];
[PDFPrint setShowsProgressPanel:YES];
[PDFPrint runOperation];
这就是我创建视图的方式:

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;
当我来回调整这个时,我已经成功地打印了PDF,但它没有正确对齐

关于路线。我可以看到我创建的红色背景,但无法看到pdf右侧的一部分

那么,如何最好地阅读并打印PDF文件呢


有什么想法吗?

如果我改为使用:

[PDFDocumentView printWithInfo:printInfo autoRotate:NO];
查看答案。