java pdfbox printerjob wrong scaling / page format

前端 未结 1 1289
名媛妹妹
名媛妹妹 2021-01-14 18:54

I\'m trying to print an existing pdf file with pdfbox. Currently I\'m using pdfbox 2.0.0 RC3 through maven.

This is my current code:

PDDocument docum         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 19:29

    Try this for pdfbox 2.0.0-RC3 version

    PDDocument doc = PDDocument.load(new File("test.pdf"));
    PDFPrintable printable = new PDFPrintable(doc, Scaling.SHRINK_TO_FIT);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);
    job.print();
    

    Here is another version

    PDDocument doc = PDDocument.load(new File("test.pdf"));
    PrinterJob job = PrinterJob.getPrinterJob();
    
    // define custom paper
    Paper paper = new Paper();
    paper.setSize(306, 396); // 1/72 inch
    paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins
    
    // custom page format
    PageFormat pageFormat = new PageFormat();
    pageFormat.setPaper(paper);
    
    // override the page format
    Book book = new Book();
    // append all pages
    book.append(new PDFPrintable(doc, Scaling.SHRINK_TO_FIT), pageFormat, doc.getNumberOfPages());
    job.setPageable(book);
    
    job.print();
    

    0 讨论(0)
提交回复
热议问题