Warm tip: This article is reproduced from serverfault.com, please click

Show the contents of pdf Document generated in java program ,without creating pdf file, in JFrame

发布于 2020-12-07 05:20:18


I am creating a pdf file from java program using itextpdf-5.1.0.jar file.
It has Document class whose instance contains entire data to be written to the pdf file. After creating the pdf file and saving it in any of our system drives, the same can be viewed in my java program using the classes in icepdf-viewer-6.2.2.jar.

Instead of creating a file in system drive and reloading the same in java application, is there any class which takes the instance of Document class of itextpdf-5.1.0.jar as a parameter and show the contents of pdf file directly the in Jframe?

Questioner
svkvvenky
Viewed
0
mkl 2020-12-07 16:10:09

Your question contains an incorrect assumption:

It has Document class whose instance contains entire data to be written to the pdf file.

While an itext Document instance indeed is the object you add all the content to that itext shall layout for you, that instance does not contain that content at all. Instead it immediately forwards it to registered listener objects which in turn write it as soon as possible to a PdfWriter instance which in turn writes it as soon as possible to a stream it is instantiated for.

Thus, your question

is there any class which takes the instance of Document class of itextpdf-5.1.0.jar as a parameter and show the contents of pdf file directly the in Jframe

clearly has to be answered: No, there cannot be such a class.

What you can do, though, is instantiating the PdfWriter with a ByteArrayOutputStream instead of a FileOutputStream to produce the pdf in memory: After closing your Document you can retrieve the byte array from the ByteArrayOutputStream which then hold the finished pdf. You then can try and show the pdf from memory with the pdf viewer component of your choice as long as that viewer supports Document input via byte array or input stream (via a ByteArrayInputStream).