2013-04-22 204 views
3

我是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); 
    } 
} 
+0

請把你的代碼放在這裏,這樣可以更容易看出你的問題。 – Rudy 2013-04-22 06:24:26

回答

2

你應該能夠通過更新PageFormat的Paper做到這一點。

pdDocument.getPageFormat().getPaper().setSize(double width, double length); 

寬度和長度都是英寸。 A4尺寸是8.27×11.69英寸。

+1

感謝您的回覆魯迪。讓我試試這個,並且會讓你知道它的工作與否。另外告訴我,我們可以更改打印縮放選項嗎? – 2013-04-22 06:48:36

+0

我不知道如果PDFBox能夠設置... – Rudy 2013-04-23 07:11:19

+0

對我來說,它確實再次打印我的文檔的小A7摘錄 – 2013-10-07 13:37:12

1

像下面創建你的頁面:

PDPage page = new PDPage(PDPage.PAGE_SIZE_A4) 
+0

感謝您的回覆Sumit Singh。在這裏,我已經意識到解決方案..我們必須創建我們自己的頁面。但我的問題是..我有PDDocument可用,我想更新現有的頁面。我怎樣才能做到這一點 ? – 2013-04-22 06:47:37

0

我有這個相同的問題,我覺得這太難了,做這件事很簡單。對我而言,接受的答案pdDocument.getPageFormat(int page)已被棄用且不可用。儘管這並不理想,但我最終還是決定採取以下措施 - 甚至不使用PDFBox打印。

以下代碼一次將頁面轉換爲圖像,並使用java2d來調整大小並將其打印出來。

PDDocument pdfdoc = PDDocument.load(pdfPane.pdfFile); 
@SuppressWarnings("unchecked") 
final List<PDPage> pdfPages = pdfdoc.getDocumentCatalog().getAllPages(); 

PrinterJob pjob = PrinterJob.getPrinterJob(); 
pjob.setJobName(pdfPane.pdfFile.getName()); 
pjob.setPrintable(new Printable() 
{ 
    @Override 
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException 
    { 
    if (page > pdfPages.size()) 
     return NO_SUCH_PAGE; 
    try 
    { 
     g.drawImage(pdfPages.get(page).convertToImage() 
       ,(int)pf.getImageableX() 
       ,(int)pf.getImageableY() 
       ,(int)pf.getImageableWidth() 
       ,(int)pf.getImageableHeight() 
       ,null); 
    } 
    catch (IOException e) 
    { 
     LoggerUtil.error(e); 
    } 
    return PAGE_EXISTS; 
    } 
}); 
pjob.print(); 
pdfdoc.close();