如何pdfstamper转换为字节数组 [英] How to convert pdfstamper to byte array

查看:2042
本文介绍了如何pdfstamper转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我需要读取现有的PDF并添加栏code到现有的PDF并将其传递到输出流。这里现有的PDF就像模板。我使用iText的罐子加入吧code。

In my application, i need to read the existing pdf and add barcode to the existing PDF and pass it to output stream. here the existing pdf is like template. I am using iText jar for adding barcode.

我想知道 PdfStamper 对象转换为字节数组或 PdfContentByte 来的字节数组的可能性。任何人都可以在这方面的帮助?

I want to know the possibilities of converting PdfStamper object to byte array or PdfContentByte to byte array. Can anyone help on this?

推荐答案

您的问题还不清楚。我假设你想要写一个,而不是 ByteArrayOutputStream 的FileOutputStream 。有关于如何做到这一点的iText站点不同的例子。

Your question is unclear. I assume that you want to write to a ByteArrayOutputStream instead of to a FileOutputStream. There are different examples on how to do that on the iText web site.

例如,参见 FormServlet 例子,它说:

See for instance the FormServlet example where it says:

// We create an OutputStream for the new PDF
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Now we create the PDF
PdfStamper stamper = new PdfStamper(reader, baos);

然后在后面的例子中,我们做到这一点:

Then later in the example, we do this:

// We write the PDF bytes to the OutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);

如果你想有一个字节[] ,你可以简单地做到这一点:

If you want a byte[], you can simply do this:

byte[] pdfBytes = baos.toByteArray();

我希望你的问题不是写一个 PdfContentByte 流到字节[] ,因为这止跌吨意义:一个内容流不包含任何资源,例如字体,图片形式XObject的,等...

I hope your question wasn't about writing a PdfContentByte stream to a byte[] because that wouldn't make sense: a content stream doesn't contain any resources such as fonts, images, form XObjects, etc...

这篇关于如何pdfstamper转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆