Java – iText Write/Read PDF File Example – PdfReader + PdfWriter

Java-iText-Write-Read-PDF-File-feature-images

In the tutorial, we show how to Write/Read PDF File with iText library.

Create Maven Project

We create a Maven project with iText dependency:

<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itextpdf</artifactId>
   <version>5.5.10</version>
</dependency>

Project structure:

Java-iText-Write-Read-PDF-File-project-structure

Write Text to PDF with iText

We use PdfWriter to write text to PDF file, WriteTextToPdfFile.java ->

package iTextJavaReadWriteTextPdfFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class WriteTextToPdfFile {

	private static final String FILE_PATH_NAME = "./src/main/resources/ozenero-itext.pdf";
	
	public static void main(String[] args) {
		writeTextToPdfFile();
	}
	
	private static void writeTextToPdfFile() {
        Document document = new Document();

        try {

            PdfWriter.getInstance(document, new FileOutputStream(new File(FILE_PATH_NAME)));

            //open
            document.open();
            
            // Paragraph 1 ->
            Paragraph p = new Paragraph("This is a paragraph 1",
            								FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC));

            document.add(p);

            // Paragraph 2 ->
            Paragraph p2 = new Paragraph();
            p2.add("This is a paragraph 2");
            p2.setAlignment(Element.ALIGN_CENTER);
            
            document.add(p2);

            // Paragraph 3 ->
            Font f = new Font();
            f.setStyle(Font.BOLD);
            f.setSize(30);
            f.setColor(255, 0, 0);
            
            Paragraph p3 = new Paragraph("This is a paragraph 3", f);
            p3.setAlignment(Element.ALIGN_RIGHT);

            document.add(p3);
            
            // Paragraph 4 ->
            Paragraph p4 = new Paragraph("Grokonez",
            								FontFactory.getFont(FontFactory.HELVETICA, 250, Font.BOLDITALIC));
            document.add(p4);
            		
            // Finish task ->
            document.close();
            
            System.out.println("Finish!");
         
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

}

We have a ozenero-itext.pdf file with 2 pages ->

Page 1:

Java-iText-Write-Read-PDF-File-write-to-pdf-page-1

Page 2:

Java-iText-Write-Read-PDF-File-write-to-pdf-page-2

Read Text from PDF with iText

We use PdfReader to read PDF text from each Page, ReadTextFromPdfFile.java ->

package iTextJavaReadWriteTextPdfFile;

import java.io.IOException;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

public class ReadTextFromPdfFile {

	private static final String FILE_PATH_NAME = "./src/main/resources/ozenero-itext.pdf";

    public static void main(String[] args) {
    	readTextFromPdfFile();
    }
    
    private static void readTextFromPdfFile() {
        PdfReader reader;

        try {
            reader = new PdfReader(FILE_PATH_NAME);
            
            for(int i=1; i<=reader.getNumberOfPages(); i++) {
                String text = PdfTextExtractor.getTextFromPage(reader, i);
                System.out.println("Page number = " + i + ",  -> content:");
                System.out.println(text + "\n");
            }
            
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

-> Results in console:

Page number = 1,  -> content:
This is a paragraph 1
This is a paragraph 2
This is a paragraph 3
Gro

Page number = 2,  -> content:
kon
ez

SourceCode

iTextJavaReadWriteTextPdfFile

0 0 votes
Article Rating
Subscribe
Notify of
guest
794 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments