如何使用Apache PDFBox在现有文档上设置打印设置属性

时间:2013-04-22 06:14:59

标签: java pdfbox

我是apache PDFBox api的新手,我想用apache PDFBox设置打印设置属性。

在这里,我想将页面大小设置为A4,我还想将Print Scaling选项设置为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 );
    }
}

3 个答案:

答案 0 :(得分:2)

您应该可以通过更新PageFormat的纸张来完成此操作。

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

宽度和长度以英寸为单位。 A4尺寸为8.27×11.69英寸。

答案 1 :(得分:1)

创建您的页面如下:

PDPage page = new PDPage(PDPage.PAGE_SIZE_A4)

答案 2 :(得分: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();