Java – iText Write/Read PDF File Example – PdfReader + PdfWriter
Tags: java, itext, pdfwriter, pdfreader
Published: 2021-05-04 18:01:01

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.

<!--more-->
<h2>Create Maven Project</h2>
We create a Maven project with iText dependency:

<pre><code class="language-html">&lt;dependency&gt;
   &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;
   &lt;artifactId&gt;itextpdf&lt;/artifactId&gt;
   &lt;version&gt;5.5.10&lt;/version&gt;
&lt;/dependency&gt;</code></pre>

Project structure:

<img src="https://grokonez.com/wp-content/uploads/2018/11/Java-iText-Write-Read-PDF-File-project-structure-1.png" alt="Java-iText-Write-Read-PDF-File-project-structure" width="750" height="264" class="alignnone size-full wp-image-15463" />
<h2>Write Text to PDF with iText</h2>
We use <code>PdfWriter</code> to write text to PDF file, <code>WriteTextToPdfFile.java</code> ->

<pre><code class="language-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();
        }
	}

}</code></pre>

We have a <code>grokonez-itext.pdf</code> file with 2 pages ->

Page 1:

<img src="https://grokonez.com/wp-content/uploads/2018/11/Java-iText-Write-Read-PDF-File-write-to-pdf-page-1.png" alt="Java-iText-Write-Read-PDF-File-write-to-pdf-page-1" width="590" height="495" class="alignnone size-full wp-image-15464" />

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

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