我是 apache PDFBox api 的新手,我想使用 apache PDFBox 设置打印设置属性。
在这里,我想将页面大小设置为,A4
并且我还想将打印缩放选项设置为NO SCALING
.
在此处记下我正在加载一个准备好的 PDF 输入流。所以我想在打印org.apache.pdfbox.pdmodel.PDDocument
之前用类设置打印属性。
我怎样才能做到这一点 ?
编辑:
这是我打印 PDF 文件的课程。请注意, 我想在其中添加代码的TODO标记将 PDF 页面大小更改为A4并将页面缩放设置为NO SCALING。
public class PrintPDF extends Applet{
private PrintPDF(){
//static class
}
public static void main(String a[]) throws Exception{
System.out.println("Printing Started...");
String password = "";
String pdfFile = "D://TEST//output.pdf";
boolean silentPrint = true;
String printerindx = "1";
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
if( pdfFile == null )
{
usage();
}
PDDocument document = null;
try
{
document = PDDocument.load( pdfFile );
//TODO : ADD CODE TO SET PAPER SIZE TO A4 AND
//ADD CODE TO SET PAGE SCALING TO : NO SCALING
if( document.isEncrypted() )
{
document.decrypt( password );
}
PrinterJob printJob = PrinterJob.getPrinterJob();
if(printerindx != null )
{
PrintService[] printServices = PrinterJob.lookupPrintServices();
for(PrintService printService : printServices){
if(printService.getName().equals("FG_LASER_PRINTER")){
printJob.setPrintService(printService);
}
}
}
if( silentPrint ){
document.silentPrint( printJob );
}else{
document.print( printJob );
}
}
finally{
if( document != null ){
document.close();
}
}
System.out.println("Printing Completed...");
}
/**
* This will print the usage requirements and exit.
*/
private static void usage()
{
System.err.println( "Usage: java org.apache.pdfbox.PrintPDF [OPTIONS] <PDF file>\n" +
" -password <password> Password to decrypt document\n" +
" -silentPrint Print without prompting for printer info\n"
);
System.exit( 1 );
}
}