2013-04-22 202 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();