【问题标题】:How to write Java code for printing a PDF created with iText如何编写 Java 代码以打印使用 iText 创建的 PDF
【发布时间】:2012-01-29 05:36:33
【问题描述】:

我编写了使用 iText 库创建 PDF 的 Java 代码,问题是我无法使用 iText 打印此 PDF,所以我在谷歌上搜索并找到了一个名为 PDFrenderer 的 Java PDF 库。 问题是我如何使用 PDFrenderer 库来编写一个程序来帮助我打印我的 PDF 文件?假设 pdfwriter 代码是使用 iText 创建的。我正在开发一个应用程序(桌面),客户可以在其中生成 PDF 并将它们直接发送到打印机。

感谢任何帮助

穆阿德

【问题讨论】:

    标签: java pdf printing itext


    【解决方案1】:

    我用它来打印 PDF...

    public class PDFPrinter {
    
    public PDFPrinter(File file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            FileChannel fc = fis.getChannel();
            ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            fis.close();
            fc.close();
            PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
            PDFPrintPage pages = new PDFPrintPage(pdfFile);
    
            // Create Print Job
            PrinterJob pjob = PrinterJob.getPrinterJob();
            PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
            Paper a4paper = new Paper();
            double paperWidth = 8.26;
            double paperHeight = 11.69;
            a4paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
    
            /*
             * set the margins respectively the imageable area
             */
            double leftMargin = 0.3;
            double rightMargin = 0.3;
            double topMargin = 0.5;
            double bottomMargin = 0.5;
    
            a4paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
                    (paperWidth - leftMargin - rightMargin) * 72.0,
                    (paperHeight - topMargin - bottomMargin) * 72.0);
            pf.setPaper(a4paper);
    
            pjob.setJobName(file.getName());
            Book book = new Book();
            book.append(pages, pf, pdfFile.getNumPages());
            pjob.setPageable(book);
    
            // Send print job to default printer
            if (pjob.printDialog()) {
                pjob.print();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (PrinterException e) {
            JOptionPane.showMessageDialog(null, "Printing Error: "
                    + e.getMessage(), "Print Aborted",
                    JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }
    
    class PDFPrintPage implements Printable {
        private PDFFile file;
    
        PDFPrintPage(PDFFile file) {
            this.file = file;
        }
    
        public int print(Graphics g, PageFormat format, int index)
                throws PrinterException {
            int pagenum = index + 1;
    
            // don't bother if the page number is out of range.
            if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
                // fit the PDFPage into the printing area
                Graphics2D g2 = (Graphics2D) g;
                PDFPage page = file.getPage(pagenum);
                double pwidth = format.getImageableWidth();
                double pheight = format.getImageableHeight();
    
                double aspect = page.getAspectRatio();
                double paperaspect = pwidth / pheight;
    
                Rectangle imgbounds;
    
                if (aspect > paperaspect) {
                    // paper is too tall / pdfpage is too wide
                    int height = (int) (pwidth / aspect);
                    imgbounds = new Rectangle(
                            (int) format.getImageableX(),
                            (int) (format.getImageableY() + ((pheight - height) / 2)),
                            (int) pwidth, height);
                } else {
                    // paper is too wide / pdfpage is too tall
                    int width = (int) (pheight * aspect);
                    imgbounds = new Rectangle(
                            (int) (format.getImageableX() + ((pwidth - width) / 2)),
                            (int) format.getImageableY(), width, (int) pheight);
                }
    
                // render the page
                PDFRenderer pgs = new PDFRenderer(page, g2, imgbounds, null,
                        null);
                try {
                    page.waitForFinish();
                    pgs.run();
                } catch (InterruptedException ie) {
                }
    
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }
    }
    }
    

    我称之为:

    new PDFPrinter(file);
    

    P.S.:你需要 PDFRender.jar

    【讨论】:

      【解决方案2】:

      这可能会有所帮助:How to Integrate with the Desktop Class

      【讨论】:

      • 感谢 FGraviton 的帮助
      【解决方案3】:

      Java Desktop API 允许将电子邮件和打印等任务委托给操作系统。 http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

      【讨论】:

      • 谢谢乔普,我会检查的
      猜你喜欢
      • 2011-06-04
      • 1970-01-01
      • 2011-03-04
      • 2019-03-03
      • 2018-03-27
      • 2016-04-04
      • 1970-01-01
      • 2013-09-11
      • 2012-11-03
      相关资源
      最近更新 更多