Need to make some PDFs with Java? If you're not familiar with iText library, here's your chance to see this piece of open source software generate PDFs.
At the moment, I'm creating an XML file in Java and displaying it in a JSP page by transforming it with XSL/XSLT. Now I need to take that XML file and display the same information in a PDF. Following is the Java program which demonstrates the creation of a PDF Document. It creates a PDF document with the name sample.pdf, adds an empty page to it, and saves it in the path C:/itextExamples/ Save this code in a file with the name createPDF.java. Jan 12, 2018 How to create a thread without implementing the Runnable interface in Java? How to read the data from a properties file in Java? How to instantiate a static inner class with reflection in Java? Will a finally block execute after a return statement in a method in Java? Importance of XOR operator in Java? Sep 17, 2007 There's also a package call PDFBox. You deliver the PDF just like any other data stream, In a Servlet set the resonse type to 'application/pdf'. It provides a simple API to create pdf files and print using subclasses of java.awt.Graphics and java.awt.PrintJob. The PDF classes write to an OutputStream in pdf format instead of a typical Graphics object, but the method calls are the same as they would be in any Applet or Application drawing to a canvas.
Join the DZone community and get the full member experience.
Join For FreePDF creation is required in some of Java-based applications, as PDF is one of the most popular document types due to its read-only and platform-independent attributes. iText is an open source library that helps integrate the PDF functionalities (create/manipulate) in your application.
iText has classes as Document
objects, which are basically the main containers, and other classes are residing within them. Paragraph
is a content type that can be written to the Document
object. Other content types are Anchor
, Chapter
, Phrase
, PdfpTable
, Section
, List
, etc. These classes help create a PDF document.
Let's look at the working example.
Required JAR: itextpdf-5.1.0.jar
DataObject.java:
HeaderFooter.java:
PdfCreater.java:
Client.java: