如何从PdfDocument中提取byte []数组

问题描述 投票:-1回答:2

经过大量的研究,我仍然找不到从PdfDocument对象中提取byte[]的方法。我怎样才能做到这一点?

我尝试过使用FileInputStream,但实际上我没有PdfDocument的“物理路径”,因为我正在以编程方式创建一个。而且,我对byte[]不太熟悉。

有人可以帮我一把吗?

    PdfDocument pdfDocumentWithoutSplit = getPdfUtils().generatePdfDocumentByMedia(shippingLabel);

        for (int i = 1; i < pdfDocumentWithoutSplit.getNumberOfPages() + 1; i++) {
            final ByteArrayOutputStream pdfByteArray = new ByteArrayOutputStream();
            final PdfDocument pdfDocument = new PdfDocument(new PdfWriter(pdfByteArray));

            pdfDocument.movePage(pdfDocumentWithoutSplit.getPage(i), i);
            pdfByteArray.close();
             //now here I need to get the bytes of each pdfDocument somehow

        }

干杯

java arrays pdf inputstream itext7
2个回答
0
投票

PDF中的所有内容都应作为字符串处理。首先,您需要搜索物理路径(您可以使用正则表达式或类似的字符串处理来根据您生成它的方式以及您正在使用的语言来搜索路径)。然后使用PDF阅读器(因为它不是纯文本文档)在PDF中搜索看起来像字节数组的字符串。最后,您需要通过提取内部数据并使用拆分或数组生成方法将字符串转换为数组。祝好运。


0
投票
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PdfDocument pdfDocument = new PdfDocument(new PdfWriter(baos ));
        pdfDocument.movePage(pdfDocumentWithoutSplit.getPage(i), i);
        pdfDocument.close();
        // should close the PdfWriter, and hence the ByteArrayOutputStream
        baos .close();
        byte[] bytes = baos .toByteArray();

关闭内容将刷新内存中的任何缓冲数据,并填充ByteArrayOutputStream。

© www.soinside.com 2019 - 2024. All rights reserved.