我是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