java 如何从 iText 获取书面 PDF 的 OutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31430465/
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-11-02 18:36:33  来源:igfitidea点击:

How to get the OutputStream of a written PDF from iText

javapdfitextinputstreamoutputstream

提问by 99maas

I need your help in getting and storing the written PDF from iText in an OutputStreamand then to convert it to an InputStream.

我需要您的帮助来从 iText 中获取和存储书面 PDF OutputStream,然后将其转换为InputStream.

The code of writing the PDF is below:

编写PDF的代码如下:

public void  CreatePDF() throws IOException {
      try{
        Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
        OutputStream out = new ByteArrayOutputStream();            
        PdfWriter writer = PdfWriter.getInstance(doc, out);

        doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);
        doc.close();

      }
        catch (Exception e) {
          e.printStackTrace();
                }
    } 

So I am seeking your help to write that PDF in an OutputStreamand then to convert it to InputStream.

因此,我正在寻求您的帮助,以将 PDF 编写OutputStreamInputStream.

I need to get the InputStreamvalue so I can pass it to the line for the file download:

我需要获取该InputStream值,以便将其传递给文件下载行:

StreamedContent file = new DefaultStreamedContent(InputStream, "application/pdf", "xxx.pdf");

Updated Jon Answer:

更新乔恩回答:

public InputStream createPdf1() throws IOException {
    Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
    try {        

    ByteArrayOutputStream out = new ByteArrayOutputStream();            
        PdfWriter writer;
            try {
                writer = PdfWriter.getInstance(doc, out);
            } catch (DocumentException e) {
            }
            doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);

        }
        catch ( Exception e)
        {
        e.printStackTrace();
        }
    return new ByteArrayInputStream(out.toByteArray());
}

采纳答案by Jon Skeet

You should change the declaration of outto be of type ByteArrayOutputStreamrather than just OutputStream. Then you can call ByteArrayOutputStream.toByteArray()to get the bytes, and construct a ByteArrayInputStreamwrapping that.

您应该将 的声明更改out为类型,ByteArrayOutputStream而不仅仅是OutputStream. 然后你可以调用ByteArrayOutputStream.toByteArray()来获取字节,并构造一个ByteArrayInputStream包装。

As an aside, I wouldn't catch Exceptionlike that, and I'd use a try-with-resources statement to close the document, assuming it implements AutoCloseable. It's also a good idea to follow Java naming conventions. So for example, you might have:

顺便Exception说一句,我不会像那样捕获,并且我会使用 try-with-resources 语句来关闭文档,假设它实现AutoCloseable. 遵循 Java 命名约定也是一个好主意。例如,您可能有:

public InputStream createPdf() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();            
    try (Document doc = new Document(PageSize.A4, 50, 50, 50, 50)) {
        PdfWriter writer = PdfWriter.getInstance(doc, out);
        doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);
    }
    return new ByteArrayInputStream(out.toByteArray());
}