java pdfbox printerjob标签上的大小错误

时间:2018-03-05 14:06:12

标签: java pdf pdfbox

我使用我的应用程序创建了一个PDF文件,其中包含标签的尺寸。当我用Adobe Reader打印此文件时,结果来自顶部。当我用Java打印时,并不是所有内容都会被打印出来。

  1. = Adob​​e,2。pdfbox
  2. 1. = Adobe, 2. pdfbox

    public static PrintService choosePrinter(String printerName) {
        PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers
        PrintService printService = null;
        int count = service.length;
    
        for (int i = 0; i < count; i++) {
            if (service[i].getName().equalsIgnoreCase(printerName)) {
                printService = service[i];
                i = count;
            }
        }
        return printService;
    }
    
    public static void printPDF(String fileName, PrinterSetting printerSetting) throws IOException, PrinterException {
        PDDocument pdf = PDDocument.load(new File(fileName));
        PrinterJob job = PrinterJob.getPrinterJob();
    
        // define custom paper
        Paper paper = new Paper();
        paper.setSize( printerSetting.getPageHeight(), printerSetting.getPageWidth()); // 1/72 inch
        paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
    
        // custom page format
        PageFormat pageFormat = new PageFormat();
        pageFormat.setPaper(paper);
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
    
        // override the page format
        Book book = new Book();
        // append all pages
        PDFPrintable pdfPrintable = new PDFPrintable(pdf, Scaling.SHRINK_TO_FIT);
        book.append(pdfPrintable, pageFormat, pdf.getNumberOfPages());
        job.setPageable(book);
    
        PrintService printService = choosePrinter(printerSetting.getPrinterName());
        if(printService != null)
            job.setPrintService(printService);
    
        job.print();
    }
    

0 个答案:

没有答案