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

0lvr5msh  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(341)

我正在itext库的tdest中创建一个pdf文档。然后我在resize函数中清理尾随空格,并将一个新文档写入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();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题