Formatting the Text in a PDF using Java
Last Updated :
02 Mar, 2023
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.
Java
// Java program to Format the Text in a PDF
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
public class PdfTextFormat {
public static void main(String args[]) throws Exception
{
String file
= "C:/EXAMPLES/itextExamples/GfG.pdf";
// Creating a PdfDocument object
PdfDocument pdfDoc
= new PdfDocument(new PdfWriter(file));
// Creating a Document object
Document doc = new Document(pdfDoc);
// Adding text to the document
Text text1 = new Text("Hi I'm Mayank Tyagi");
// Setting color to the text
text1.setFontColor(Color.BLACK);
// Setting font to the text
text1.setFont(PdfFontFactory.createFont(
FontConstants.HELVETICA));
// Creating a paragraph 1
Paragraph para1 = new Paragraph(text1);
Text text2 = new Text(
"I'm a technical content writer at GeeksforGeeks");
// Setting color to the text
text2.setFontColor(Color.BLACK);
// In version 7.2.x the above line may be replaced by
// text2.setFontColor(ColorConstants.BLACK);/*com.itextpdf.kernel.colors.ColorConstants*/
// Setting font to the text
text2.setFont(PdfFontFactory.createFont(
FontConstants.HELVETICA));
// Creating a paragraph 2
Paragraph para2 = new Paragraph(text2);
// Adding paragraphs to the document
doc.add(para1);
doc.add(para2);
// Closing the document
doc.close();
System.out.println("Text added successfully..");
}
}
Output

Similar Reads
Formatting Text on a Slide in a PPT using Java To Format text on a slide in PowerPoint Presentation using Java, use a Java Library called Apache POI. Apache POI is a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project provides pure Java libraries for reading and writing files in Microsoft Office for
3 min read
Formatting the Content of a Cell in a Table of PDF using Java iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. iText is a Java library originally created by Bruno Lowagie which allows creating, reading, and manipulating PDF files. Java allows incorporating various fully developed packages and
4 min read
Adding Images to a Table in PDF using Java PDFBox is an open-source library which is written in Java. It helps in the development and conversion of PDF Documents. PDFBox library comes in form of a JAR file. It can create new PDF documents, manipulate existing documents, bookmark the PDF and also extract content from PDF documents. We can use
7 min read
Create a Table in a PDF Using Java The creation of a table in a PDF using Java is done by installing the document class. While instantiating this class, pass a PdfDocument object as a parameter to its constructor. Then, to feature a table to the document, instantiate the Table class, and add this object to the document using the add(
1 min read
Adding Paragraphs as Text to a PDF using Java iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. Java allows us to incorporate various fully developed packages and modules in order to work with PDF files. We will see how to create a PDF document and add a paragraph to it using t
4 min read
Shrinking the Contents in a PDF using Java Program to shrink the contents of a PDF document. The external jar file is required to import in the program. Below is the implementation for the same. Approach: 1. Create an empty PDF file. Assign the path of the empty PDF to a String variable.Import PdfWriter from the package com.itextpdf.kernel.p
5 min read