Java - iText Write/Read PDF File Example - PdfReader + PdfWriter » grokonez

Java - iText Write/Read PDF File Example - PdfReader + PdfWriter » grokonez

In the tutorial, we show how to Write/Read PDF File with iText library. ContentsCreate Maven ProjectWrite Text to PDF with iTextRead Text from PDF with iTextSourceCode 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: Write Text to PDF with iText We use PdfWriter to write … Continue reading "Java – iText Write/Read PDF File Example – PdfReader + PdfWriter"

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

java itext pdfreader pdfwriter

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Online Training | Java Online Course | ITGuru

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects

Best Java IDE 2020 | Most Popular Java IDE

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects

Learning by Doing: How to Learn Java Basics by Building Your Own Project

Check out some ideas for sample projects that can help you learn Java, and programming in general, in a fun and engaging way.

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...