【问题标题】:How to put the ByteArrayInputStream content into a PDF using iText?如何使用 iText 将 ByteArrayInputStream 内容放入 PDF?
【发布时间】:2015-02-05 14:23:27
【问题描述】:

我有以下情况,入一个方法我有:

ByteArrayInputStream fis = new ByteArrayInputStream(Bean.getValoreString("PDFmulti", "PDF").getBytes());

如您所见,fis 变量是 ByteArrayInputStreamBean.getValoreString("PDFmulti", "PDF").getBytes()返回一个 byte[]

所以现在我需要使用 iText 将 fis 对象的内容放入 PDF。

我能做些什么呢?我认为我必须读取此输入流并将其内容放入 ByteArrayOutputStream,如下所示:

public static byte[] readFully(InputStream stream) throws IOException
{
    byte[] buffer = new byte[8192];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int bytesRead;
    while ((bytesRead = stream.read(buffer)) != -1)
    {
        baos.write(buffer, 0, bytesRead);
    }
    return baos.toByteArray();
}

然后呢?

【问题讨论】:

    标签: java pdf pdf-generation itext bytearrayoutputstream


    【解决方案1】:

    在对您之前的问题https://stackoverflow.com/questions/28342714/how-to-convert-a-string-object-representing-a-pdf-into-a-bytearrayinputstream-th 的评论中,您说我必须连接所有 PDF 以创建一个 PDF。

    这是您在新问题中忽略的重要信息。如果我阅读了您的新问题,就好像您想以其他形式保留 byte[] 中存在的 PDF。例如:您想将其存储为文件。

    如果是这样,那么您不需要 iText!只需将字节写入FileOutputStream

    但是,既然我知道您需要连接文件,我知道您需要多个 PdfReader 实例,然后将这些 PdfReader 实例与 PdfCopy(或 PdfSmartCopy)结合使用来创建一个PDF 来自一系列不同的 PDF。

    这是一个完全不同的问题!在那种情况下,你为什么要创建一个ByteArrayOutputStream?有一个PdfReader contructor 接受InputStream 作为参数。为什么不将fis 传递给该构造函数?

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 2011-12-31
      • 2014-08-22
      • 2022-08-04
      • 1970-01-01
      • 2022-01-08
      • 2014-10-01
      • 1970-01-01
      • 2014-08-23
      相关资源
      最近更新 更多