在我的应用程序中,我需要阅读现有的 pdf 并将条形码添加到现有的 PDF 并将其传递给输出流。这里现有的pdf就像模板。我正在使用 iText jar 添加条形码。
我想知道将PdfStamper
对象转换为字节数组或PdfContentByte
字节数组的可能性。有人可以帮忙吗?
在我的应用程序中,我需要阅读现有的 pdf 并将条形码添加到现有的 PDF 并将其传递给输出流。这里现有的pdf就像模板。我正在使用 iText jar 添加条形码。
我想知道将PdfStamper
对象转换为字节数组或PdfContentByte
字节数组的可能性。有人可以帮忙吗?
你的问题不清楚。我假设您想写入 aByteArrayOutputStream
而不是 a FileOutputStream
。在 iText 网站上有不同的例子来说明如何做到这一点。
例如,请参阅FormServlet示例,其中说:
// We create an OutputStream for the new PDF
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Now we create the PDF
PdfStamper stamper = new PdfStamper(reader, baos);
然后在后面的示例中,我们这样做:
// We write the PDF bytes to the OutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
如果你想要一个byte[]
,你可以简单地这样做:
byte[] pdfBytes = baos.toByteArray();
我希望您的问题不是关于PdfContentByte
向 a 写入流,byte[]
因为那没有意义:内容流不包含任何资源,例如字体、图像、表单 XObjects 等...