Generate PDF using Java from scratch without any library (Multiple pages and graphics)

In earlier article “Generate PDF using Java from scratch without any library”, we generated very basic minimal PDF using pure Java without any library. Now in this article we will improve that code further.

PDF improvements in this article:

  • Add more pages to PDF
  • Add more text in page.
  • Add Graphics like rectangle, line, curve etc. Will also use colors to fill up graphics.

As we are going to create bigger PDF now, its better to improve our code design so that it will be easier to build PDF.

Design & code Improvements

  • Make code more object oriented so that we can build bigger PDFs easily.
  • Automate PDF object numbering so that we can keep adding more and more PDF objects easily.
  • Convert PDF keywords to readable constants so that its easy to recognize code.

Below diagram shows the design of classes for PDF objects that we will follow in this article along with their hierarchy & there inter-linking with each other.





As per PDF specifications, this is the general structure of PDF file.

So this will be our corresponding java object linking representing above PDF structure.

 

Abstract PDF Object

This will be abstract class which all PDF objects will extend. Each PDF object needs object number & generation number. So we will put it separately in PDFObjectReference class as shown below. We won’t be setting these numbers manually, but we will populate them programmatically. We have also provided method to add custom attributes which can be String or reference to other PDF objects.

As per out knowledge from earlier article, we have improved & prepared building methods in this abstract class. For attributes, depending on type of object, we differently append value while building.



Catalog Object

This is the first object in PDF which links to pages object.

Pages & Page objects

These objects contain list of pages & individual page. Page object can contain streams of different types like text, graphics etc.



Font Object

Abstract stream object

This is abstract representation of stream object which all stream objects will inherit.

Text stream object

Text stream uses different keywords from PDF specification. We have pulled out String constants with readable names. This object will also create stream format as per specifications.



Graphics stream object

Graphics stream objects are bit complicated. This deals with lot of co-ordinates to draw graphics. To get more details about this refer to PDF Specifications. We have fetched PDF keywords in String constants to make it more readable.

PDF object

Now this is the final encapsulating PDF object. This class will take care of numbering all the PDF objects so that we don’t have to do this manually. This class will also add



Now let build PDF

Now we generate a PDF like this,

  1. Page 1 – Header text, then 2 lines of normal text.
  2. Page 2 – Rectangle & line
  3. Page 3 – Curve with colors

Here is the complete code Complete code with all PDF object classes and PDFWithTextAndGraphics.java


Output:

Here is the text output of this program.

PDF opened in PDF reader

 



Leave a Reply

Your email address will not be published.