How to Print a PDF File Using Cocoa
来源:互联网 发布:软件测试 翻译 编辑:程序博客网 时间:2024/06/06 01:45
连接:http://www.danandcheryl.com/2010/05/how-to-print-a-pdf-file-using-cocoa
Mac OS X is well known for its great support for PDF files. You can create a PDF file from anything you can print. I thought that using Apple’s PDFKit framework would make it easy to program a way to print an existing PDF file. That turned out not to be the case.
Sending a file to a printer using the lp command is easy. However, this approach does not work for PDF files formatted for landscape printing. You can specify landscape orientation, but I wanted a way to detect the orientation automatically.
PDFKit has a PDFView
object that has a printWithInfo:autoRotate:
method. However, adding aPDFDocument
to a PDFView
and telling it to print doesn’t work. I eventually stumbled onto the fact thatPDFDocument
has a secret method that makes printing easy. So here is the code:
#import <Quartz/Quartz.h>- (void)printPDF:(NSURL *)fileURL { // Create the print settings. NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo]; [printInfo setTopMargin:0.0]; [printInfo setBottomMargin:0.0]; [printInfo setLeftMargin:0.0]; [printInfo setRightMargin:0.0]; [printInfo setHorizontalPagination:NSFitPagination]; [printInfo setVerticalPagination:NSFitPagination]; // Create the document reference. PDFDocument *pdfDocument = [[[PDFDocument alloc] initWithURL:fileURL] autorelease]; // Invoke private method. // NOTE: Use NSInvocation because one argument is a BOOL type. Alternately, you could declare the method in a category and just call it. BOOL autoRotate = YES; NSMethodSignature *signature = [PDFDocument instanceMethodSignatureForSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)]; [invocation setArgument:&printInfo atIndex:2]; [invocation setArgument:&autoRotate atIndex:3]; [invocation invokeWithTarget:pdfDocument]; // Grab the returned print operation. NSPrintOperation *op = nil; [invocation getReturnValue:&op]; // Run the print operation without showing any dialogs. [op setShowsPrintPanel:NO]; [op setShowsProgressPanel:NO]; [op runOperation];}
I consider this code to be in the public domain. Please feel free to copy and paste. And let me know if you find any problems or have suggestions.
- How to Print a PDF File Using Cocoa
- How to print a PDF file Using VB
- How to create a PDF file, cheaply?
- how-to-print-a-long-integer-using-only-putchar
- How to convert a latex to pdf file
- How to Remove PDF/A Information from a file
- How to pad a file with “FF” using dd?
- pdf file to txt file using pdfbox
- Write a method to print the last K lines of an input file using C++
- How to copy a datafile from ASM to a file system not using RMAN
- how to print log for a developer
- How To Load CLOB Data from a File into a CLOB column using PL/SQL
- How to transform a pdf file from database@server side to client ?
- How to open a file
- How To Print Concurrent Requests in PDF Format
- How to read ini file using Java
- ABAP PRINT PDF FILE
- Mount a remote file system using sshfs and fuse - How to install sshfs
- 交互设计中的默认值
- iPhone访问.Net WebService的3种方法
- C run-time library
- Windows Shell编程-第六章.快捷方式的最短路径
- 关于图片的那些事
- How to Print a PDF File Using Cocoa
- Oracle Form 信息提示框之基本用法
- hdu 敌兵布阵 线段树基础
- 跑马灯效果(文字滚动)
- 数据库学习实践(二)----记录浏览
- Windows Shell编程-第七章.侵入Shell
- Java程序员面试中的多线程问题
- getchar()函数 & EOF
- hdu I hate it 区间最大值 线段树