我是apache PDFBox api的新手,我想用apache PDFBox设置打印设置属性。如何使用Apache PDFBox在现有文档上设置打印设置属性
这里我想将页面大小设置为A4
,我也想将打印缩放选项设置为NO SCALING
。
请注意,我有一个我正在加载的PDF输入流。所以我想在打印前在那里设置org.apache.pdfbox.pdmodel.PDDocument
类的打印属性。
我该怎么做?
编辑:
这里是我的同班同学打印的PDF文件。请注意,TODO标记我要将代码更改PDF页面大小添加到A4并将页面缩放设置为无缩放。
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);
}
}
请把你的代码放在这里,这样可以更容易看出你的问题。 – Rudy 2013-04-22 06:24:26