繁体   English   中英

如何从 PdfDocument 中提取字节 [] 数组

[英]How to extract the byte [] array from a PdfDocument

经过大量研究,我仍然找不到从 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

        }

干杯

        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。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM