如何打印用iText创建的PDF? [英] How to print a PDF created with iText?

查看:168
本文介绍了如何打印用iText创建的PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I have created a PDF file with an image in it, I want to print my pdf after creating. Better if I have the PDF in memory instead of having a file, and then send it to the printer... Any Idea ?

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

I am using iText. Check my code:

    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;
    }

提前致谢!

推荐答案

您可以随时使用一个ByteArrayOutputStream,而不是一个FileOutputStream。

You can always use a ByteArrayOutputStream instead of a FileOutputStream.

在你的PDF字节,它的正常你怎么用Java打印问题。很多打印机(或者至少是他们的驱动程序)最近会直接使用PDF,所以在这一点上可以说你已经完成了。

After you have the PDF bytes, its a normal "how do you print in Java" question. Many printers (or at least their drivers) will take PDF directly these days, so at that point one could argue that you're done.

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

PS: Once I tagged your question "Java" it colored your code block using "import" as a keyword and so forth. Something to keep in mind in the future.

这篇关于如何打印用iText创建的PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆