如何从 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 中搜索看起来像字节数组的字符串。最后,您需要通过提取内部数据并使用拆分或数组生成方法将字符串转换为数组。祝你好运。

最新更新