JavaFX应用程序:打印机无法打印文件

时间:2020-08-18 12:31:00

标签: java pdf javafx printing itext

我正在使用itext库在tDEST中创建PDF文档。然后,我要清理大小调整函数中的尾部空格,并将新文档写入DEST。如果我想打印tDest文档,它可以正常工作,但是如果我尝试打印DEST文件,它不会打印任何内容,也不会引发任何异常。

这是我的主班:

String DEST = System.getProperty("user.home") + "\\Documents\\invoice.pdf";
String tDEST = System.getProperty("user.home") + "\\Documents\\temp.pdf";

public static void main(String... args) {
    ....

    resize();

    PDDocument document = null;
    try {
        document = PDDocument.load(new File(DEST));
    } catch (IOException e) {
        e.printStackTrace();
    }

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(document));

    try {
        PrintService myPrintService = findPrintService(Commons.printer.getName());
        job.setPrintService(myPrintService);
        job.print();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Printer ishlamayapti");
    }

    ....
}

public void resize() {
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(DEST);
        PdfReader reader = new PdfReader(tDEST);
        com.itextpdf.text.Rectangle pageSize = reader.getPageSize(1);
        PdfStamper stamper = new PdfStamper(reader, fileOutputStream);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        TextMarginFinder finder = parser.processContent(1, new TextMarginFinder());

        PdfDictionary page = reader.getPageN(1);
        page.put(PdfName.CROPBOX, new PdfArray(new float[]{pageSize.getLeft(), finder.getLly(), pageSize.getRight(), pageSize.getTop()}));
        stamper.markUsed(page);

        stamper.flush();
        stamper.close();
        reader.close();

        fileOutputStream.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

0 个答案:

没有答案