【问题标题】:How to get the OutputStream of a written PDF from iText如何从 iText 获取书面 PDF 的 OutputStream
【发布时间】:2015-10-04 11:56:19
【问题描述】:

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

编写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();
                }
    } 

因此,我正在寻求您的帮助,以在 OutputStream 中编写该 PDF,然后将其转换为 InputStream

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

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

更新乔恩答案:

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

【问题讨论】:

  • out 中的PdfWriter.getInstance(doc, out) 是什么?
  • @JonSkeet 我错过了一行。请查看更新后的帖子

标签: java pdf itext inputstream outputstream


【解决方案1】:

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

顺便说一句,我不会像这样捕获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());
}

【讨论】:

  • 感谢您的帮助,但是,我有一个问题,希望您对此进行澄清。我在代码中有一行需要传递 InputStream 值: StreamedContent file = new DefaultStreamedContent(stream, "application/pdf", "xxx.pdf");在流值中,我需要传递一个 InputStream,但在您的解决方案中,它是 ByteArrayInputStream
  • 我已经用该行更新了代码,这样会更清楚
  • @99maas:你不明白ByteArrayInputStream InputStream吗?如果您不熟悉 Java 中的多态性和继承是如何工作的,我建议您先回到基础再继续。
  • 感谢您的建议。我会检查一下,因为我不熟悉 InputStream 和 OutputStream 的工作原理
  • 最后一行显示out is not defined的错误 return new ByteArrayInputStream(out.toByteArray());
猜你喜欢
  • 2017-09-30
  • 2020-06-27
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 2015-07-03
相关资源
最近更新 更多