【问题标题】:How to print a PDF created with iText?如何打印使用 iText 创建的 PDF?
【发布时间】:2011-06-04 07:48:30
【问题描述】:

您好,我创建了一个包含图像的 PDF 文件,我想在创建后打印我的 pdf。如果我在内存中有 PDF 而不是文件,然后将其发送到打印机,那就更好了...有什么想法吗?

我正在使用 iText。检查我的代码:

    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Image;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Rectangle;
    import com.lowagie.text.pdf.PdfContentByte;
    import com.lowagie.text.pdf.PdfPrinterGraphics2D;
    import com.lowagie.text.pdf.PdfTemplate;
    import com.lowagie.text.pdf.PdfWriter;

    import javax.imageio.ImageIO;

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;


        private boolean exportToPdfThroughPNG(String fileName, float width, float height) throws DocumentException, IOException {
        logger.debug("[boolean exportToPdfQuick() throws IOException, DocumentException]");

        BufferedImage pngFile = createPngFile();

        Document document = new Document();
        document.setPageSize(new Rectangle(width, height));
        PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        Image image = Image.getInstance(Toolkit.getDefaultToolkit().createImage(pngFile.getSource()), Color.WHITE);
        document.add(image);
        // If some day anyone wants to put text in the pdf. @Eduardo
        // document.add(new Paragraph("title of the process"));
        document.close();

        return true;
    }

提前致谢!

【问题讨论】:

  • 这是在 Windows 上运行,还是在其他平台上运行?

标签: java printing pdf-generation itext


【解决方案1】:

您始终可以使用 ByteArrayOutputStream 而不是 FileOutputStream。

获得 PDF 字节后,这是一个正常的“你如何用 Java 打印”的问题。如今,许多打印机(或至少是他们的驱动程序)将直接获取 PDF,因此到那时有人可能会争辩说您已经完成了。

PS:一旦我将您的问题标记为“Java”,它就会使用“import”作为关键字等为您的代码块着色。将来要记住的事情。

【讨论】:

  • 谢谢马克,我工作太累了,终于可以解决这个问题了... PS:我不是使用这个编辑器的专家!对不起!
猜你喜欢
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 2012-01-29
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多