Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate inputStream from Itext pdf source

Tags:

java

pdf

itext

I'm trying to generate an inputStream Object from a file generated by iText library. These are the first bytes in the file:

%PDF-1.4
%âãÏÓ
2 0 obj
<</Length 1571/Filter/FlateDecode>>stream
)©toÿqûºÒç¹Ð4)ÖÞ{Ñ$,·7?ÂDCþDÆü½
like image 724
admin.urban Avatar asked Jan 23 '26 09:01

admin.urban


1 Answers

Suppose that you created your PDF like this:

ByteArrayOutputStream out = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, out);
document.open();
document.add(new Paragraph("Hello World"));
document.close();

In that case you can convert the OutputStream to an InputStream like this:

InputStream in = ByteArrayInputStream(out.toByteArray());

Suppose that you created the PDF like this:

FileOutputStream out = new FileOutputStream("my.pdf");
Document document = new Document();
PdfWriter.getInstance(document, out);
document.open();
document.add(new Paragraph("Hello World"));
document.close();

Then you can create an InputStream like this:

InputStream in = new FileInputStream("my.pdf");
like image 95
Bruno Lowagie Avatar answered Jan 24 '26 23:01

Bruno Lowagie