Java打印 - 如何设置纸张尺寸和方向属性

时间:2018-02-28 10:40:05

标签: java file pdf printing attributes

我需要打印PDF并配置纸张尺寸和方向。 这是我的代码,但属性不起作用。

private void doPrint(){
    try{

        PDDocument aImprimir = PDDocument.load(new File("./file.pdf")); //org.apache.pdfbox.pdmodel.PDDocument
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(MediaSizeName.ISO_A8);
        attributes.add(OrientationRequested.PORTRAIT);

        PrintService myPrintService = PrintServiceLookup.lookupDefaultPrintService(); //javax.print.PrintService & javax.print.PrintServiceLookup

        PrinterJob job = PrinterJob.getPrinterJob(); //java.awt.print.PrinterJob

        job.setPageable(new PDFPageable(aImprimir));
        job.setPrintService(myPrintService);
        job.print(attributes);
        aImprimir.close();
    }
    catch(DocumentException | PrinterException | IOException | NullPointerException e){
        System.out.println("Error" + e):
    }
}

0 个答案:

没有答案