DEV Community

loading...

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

loizenai profile image loizenai ・2 min read

https://grokonez.com/java/java-itext-write-read-pdf-file-example

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

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/grokonez-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 grokonez-itext.pdf file with 2 pages ->

Page 1:

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

https://grokonez.com/java/java-itext-write-read-pdf-file-example

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

Discussion (0)

Forem Open with the Forem app