Open In App

Adding Paragraphs as Text to a PDF using Java

Last Updated : 08 Aug, 2022
Like Article

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 the iText library.

1. PdfWriter

Java has an inbuilt package com.itextpdf.kernel.pdf, which basically provides classes and modules to create PDF documents in Java. An available class in this package is PdfWriter. We instantiate an object of this class and pass as argument the file-path along with the name of the new PDF file we wish to create. An object is passed onto this class, in order to append text to the specified file location. The following Java code snippet illustrates the usage of this class : 

// path to create the file 
String file_path = "C:/appendtexttopdfjava.pdf";

// creating an object of PdfWriter class with file_path as argument
PdfWriter pdf_writer = new PdfWriter(file_path); 

This creates a new PDF file in the C: with the name as appendtexttopdfjava.pdf

2. PdfDocument

The package com.itextpdf.kernel.pdf contains another class to represent the specified PDF file in iText which provides a user with the ease of adding various features like page fonts, file attachment by incorporating various methods of this class. An object needs to be instantiated of this class by passing as argument the pdf_writer object created using PdfWriter class. The following Java code snippet illustrates the usage of this class : 

// Representing PDF document in iText 
PdfDocument pdf_doc = new PdfDocument(pdf_writer); 

3. Document

The Document class of the package com.itextpdf.layout takes as an argument the PdfDocument object created and instantiates the Document used as a source for all PDF operations to be performed. It acts as a container for content that needs to be modified or appended to the document file. 

// Instantiating a document object from pdf document object 
Document document = new Document(pdf_doc); 

When an object of this class is created, the file is visualized as a stream of characters, to which operations (addition of new characters, modification of previous ones, deletion, etc. )  can be performed upon. 

4. Paragraph

The Paragraph class of the Java inbuilt package com.itextpdf.layout.element is basically a child of the Document class. It creates an object using the text stream, that is the contents that are actually going to be added to the PDF document. The content may be a block of lines, which needs to be added to the document class object using the add() method provided by the document class. The paragraph class is basically an element of the document class. Multiple paragraph objects can be created and added to the same document. 

After writing the content to the document, it is closed. 

//content to be added to the pdf document
String paragraph = "Geeks For Geeks makes you learn coding. It also provides competitions"; 
//Creating a paragraph class object
Paragraph para_obj = new Paragraph (paragraph); 

//adding paragraph to the document object 

//closing the document after writing the contents

The following Java code indicates the function of addition of a paragraph to the Java file : 


// importing the required packages
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
public class AppendtoPdf {
    public static void main(String args[]) throws Exception
        // path to create the file
        String file_path = "C:/appendtexttopdfjava.pdf";
        // creating PdfWriter object
        PdfWriter pdf_writer = new PdfWriter(file_path);
        // Representing PdfDocument object
        PdfDocument pdf_doc = new PdfDocument(pdf_writer);
        // Creating a Document
        // Instantiating a document object from pdf document
        // object
        Document document = new Document(pdf_doc);
        // paragraph to be added
        String para
            = "Learn Data Structures Online At Your Own Pace With
              The Best Of Faculty In The Industry
                  .The Best Data Structures Course Available
                      Online From Skilled And Experienced
                          Faculty.Premium Video Lectures
                  .Active Discussion Forum
                  .Course Certificate.Get Certified.";
              // Creating Paragraph object
              Paragraph paragraph_obj
            = new Paragraph(para);
        // Adding paragraphs to document
        // Closing the document
        // final message
            "Finished writing contents to the file!");

The code upon execution on the terminal produces the following output on the terminal and saves a file in C: at the local computer. 

Finished writing contents to the file!

The contents of the saved PDF file are as follows : 

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads