java 如何打印用 iText 创建的 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4609667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:12:28  来源:igfitidea点击:

How to print a PDF created with iText?

javaprintingpdf-generationitext

提问by MadMad666

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 ?

嗨,我创建了一个带有图像的 PDF 文件,我想在创建后打印我的 pdf。如果我将 PDF 保存在内存中而不是文件中,然后将其发送到打印机,那就更好了……知道吗?

I am using iText. Check my code:

我正在使用 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;
    }

Thanks in advance!

提前致谢!

采纳答案by Mark Storer

You can always use a ByteArrayOutputStream instead of a FileOutputStream.

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

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.

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

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.

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