0

我有一堆ByteArrayOutputstreamspdf 报告写在特定工作流程的不同部分上。我使用 IText 来完成此任务。现在,最后我想将所有这些单个 ByteArrayOutputstreams 组合成一个更大的ByteArrayOutputstream,以便将所有 pdf 报告组合在一起。

我查看了 Apache Commons 库,但找不到任何有用的东西。

我知道的一种方法是将它们中的每一个转换ByteArrayOutputstreamsbyte[]然后使用System.arraycopy将它们复制到更大的byte[]. 问题是我必须byte[]预先声明结果的大小,这使得它不理想。

有没有其他方法可以复制/附加到/连接我可能错过的 ByteArrayOutputStreams ?

4

3 回答 3

1

Write all but one of their toByteArray() results into the remaining one.

于 2012-07-11T08:02:27.847 回答
1

你可以使用 a List<Byte[]>。您可以将您的字节添加到列表中。

List<Byte[]> listOfAllBytes = new ArrayList<Byte[]>;
ByteArrayOutputstreams byteArray = //...
listOfAllBytes.add(byteArray.toByteArray);

最后,您可以取回完整的字节数组。

于 2012-07-11T07:59:46.390 回答
0
public class Concatenate {

    /** The resulting PDF file. */
    public static final String RESULT
        = "path/to/concatenated_pdf.pdf";

    public static void main(String[] args)
        throws IOException, DocumentException {
        String[] files = { "1.pdf", "2.pdf" };
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
        document.open();
        PdfReader reader;
        int n;
        // loop over the documents you want to concatenate
        for (int i = 0; i < files.length; i++) {
            reader = new PdfReader(files[i]);
            // loop over the pages in that document
            n = reader.getNumberOfPages();
            for (int page = 0; page < n; ) {
                copy.addPage(copy.getImportedPage(reader, ++page));
            }
            copy.freeReader(reader);
        }
        document.close();
    }
}
于 2012-07-12T07:18:01.643 回答