java 如何从 iText 获取书面 PDF 的 OutputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31430465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the OutputStream of a written PDF from iText
提问by 99maas
I need your help in getting and storing the written PDF from iText in an OutputStream
and then to convert it to an InputStream
.
我需要您的帮助来从 iText 中获取和存储书面 PDF OutputStream
,然后将其转换为InputStream
.
The code of writing the PDF is below:
编写PDF的代码如下:
public void CreatePDF() throws IOException {
try{
Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
OutputStream out = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(doc, out);
doc.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
table.addCell(cell);
doc.add(table);
doc.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
So I am seeking your help to write that PDF in an OutputStream
and then to convert it to InputStream
.
因此,我正在寻求您的帮助,以将 PDF 编写OutputStream
为InputStream
.
I need to get the InputStream
value so I can pass it to the line for the file download:
我需要获取该InputStream
值,以便将其传递给文件下载行:
StreamedContent file = new DefaultStreamedContent(InputStream, "application/pdf", "xxx.pdf");
Updated Jon Answer:
更新乔恩回答:
public InputStream createPdf1() throws IOException {
Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer;
try {
writer = PdfWriter.getInstance(doc, out);
} catch (DocumentException e) {
}
doc.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
table.addCell(cell);
doc.add(table);
}
catch ( Exception e)
{
e.printStackTrace();
}
return new ByteArrayInputStream(out.toByteArray());
}
采纳答案by Jon Skeet
You should change the declaration of out
to be of type ByteArrayOutputStream
rather than just OutputStream
. Then you can call ByteArrayOutputStream.toByteArray()
to get the bytes, and construct a ByteArrayInputStream
wrapping that.
您应该将 的声明更改out
为类型,ByteArrayOutputStream
而不仅仅是OutputStream
. 然后你可以调用ByteArrayOutputStream.toByteArray()
来获取字节,并构造一个ByteArrayInputStream
包装。
As an aside, I wouldn't catch Exception
like that, and I'd use a try-with-resources statement to close the document, assuming it implements AutoCloseable
. It's also a good idea to follow Java naming conventions. So for example, you might have:
顺便Exception
说一句,我不会像那样捕获,并且我会使用 try-with-resources 语句来关闭文档,假设它实现AutoCloseable
. 遵循 Java 命名约定也是一个好主意。例如,您可能有:
public InputStream createPdf() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (Document doc = new Document(PageSize.A4, 50, 50, 50, 50)) {
PdfWriter writer = PdfWriter.getInstance(doc, out);
doc.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
table.addCell(cell);
doc.add(table);
}
return new ByteArrayInputStream(out.toByteArray());
}