Formatting the Text in a PDF using Java
We can add nested tables to a PDF by installing the document class. Following are the steps to format the text in a PDF using java.
1. Create a PDF writer object
The PdfWriter class here represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the trail of the file where the PDF is to be created.
Create the PdfWriter class by passing a string value (representing the trail where you would like to make a PDF) to its constructor.
2. Create a PDFdocument object
The PdfDocument class is the class that represents the PDF Document in iText. This class belongs to the package com.itextpdf.kernel.pdf. To create this class (in writing mode), you would like to pass an object of the category PdfWriter to its constructor.
Create the PdfDocument class by passing the above created PdfWriter object to its constructor.
3. Add text to the document
Add the desired text to your PDF document
4. Set text color and font
Set the text color of the text added in the previous step using setFontColor() method.
Set the font of the text added in the previous step using setFont() method.
5. Add new text
Add new text which you want to be formatted or added in the text added in step 3.
6. Add the new text to the document
Add the newly created text to our original text. We can do that by using doc.add() method which takes the variable in which the text is stored as a parameter.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.