Java - Apache PDFBox Write/Read PDF File Example » grokonez

Java - Apache PDFBox Write/Read PDF File Example » grokonez

In the tutorial, we show how to Write/Read PDF File with PDFBox library. ContentsCreate Maven ProjectWrite Text to PDF with PDFBoxRead Text from PDF file with PDFBoxSourceCode Create Maven Project We create a Maven project with PDFBox dependency -> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.8</version> </dependency> Project structure -> Write Text to PDF with PDFBox – PdfBox … Continue reading "Java – Apache PDFBox Write/Read PDF File Example"

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

Java – Apache PDFBox Write/Read PDF File Example

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

Create Maven Project

We create a Maven project with PDFBox dependency ->
<dependency>
     <groupId>org.apache.pdfbox</groupId>
     <artifactId>pdfbox</artifactId>
     <version>2.0.8</version>
</dependency>

Project structure ->

java-pdfbox-read-write-text-pdf-file-project-structure

Write Text to PDF with PDFBox

- PdfBox library provides API which is based on stream manipulation (PDPageContentStream class).

Implement code at PDFBoxWriteText.java ->


package com.grokonez.pdfbox;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class PDFBoxWriteText {

    private static String FILE_PATH_NAME = "src/main/resources/grokonez-about.pdf";

    public static void main(String[] args) throws IOException {
        writeTextToPdfFile();
    }

    private static void writeTextToPdfFile() throws IOException {
        try (PDDocument doc = new PDDocument()) {

            /* 
             * Create a PDF Page:
             * PDF Page 1 ->
             */
            PDPage myPage = new PDPage();
            doc.addPage(myPage);

            try (PDPageContentStream cont = new PDPageContentStream(doc, myPage)) {

                cont.beginText();

                cont.setFont(PDType1Font.TIMES_BOLD, 12);
                cont.setLeading(15.5f);

                cont.newLineAtOffset(25, 700);
                String line1 = "Who We Are?";

                cont.showText(line1);
                cont.newLine();
                cont.newLine();

                cont.setFont(PDType1Font.TIMES_ROMAN, 12);
                String line2 = "- We are passionate engineers in software development. ";
                cont.showText(line2);
                cont.newLine();

                String line3 = "- We focus on how to do things that can both respect users and make money.";
                cont.showText(line3);
                cont.newLine();

                cont.endText();
            }

            doc.save(FILE_PATH_NAME);

            /*
             * Create a new PDF Page
             * -> PDF Page 2:
             */
            PDPage myPage2 = new PDPage();
            doc.addPage(myPage2);

            try (PDPageContentStream cont = new PDPageContentStream(doc, myPage2)) {

                cont.beginText();

                cont.setLeading(15.5f);

                cont.newLineAtOffset(25, 700);
                cont.setFont(PDType1Font.TIMES_ROMAN, 12);

                // line 1 = "What does grokonez mean?"
                cont.showText("What does ");

                cont.setFont(PDType1Font.TIMES_BOLD, 12);
                cont.showText("grokonez");

                cont.setFont(PDType1Font.TIMES_ROMAN, 12);
                cont.showText(" mean?");

                cont.newLine();
                cont.newLine();

                cont.setFont(PDType1Font.TIMES_ROMAN, 12);

                String line2 = "Well, ‘grokonez’ is derived from the words ‘grok’ and ‘konez’.";
                cont.showText(line2);
                cont.newLine();

                String line3 = "– ‘grok’ means understanding (something) intuitively or by empathy.";
                cont.showText(line3);
                cont.newLine();

                String line4 = "– ‘konez’ expresses ‘connect’ that represents the idea ‘connect the dots’, ‘connect everything’.";
                cont.showText(line4);
                cont.newLine();

                cont.endText();
            }

            doc.save(FILE_PATH_NAME);

            System.out.println("Done!");
        }
    }
}

-> Results:

  • Page 1:
java-pdfbox-read-write-text-pdf-file-page-1
  • Page 2:

More at:

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

Java – Apache PDFBox Write/Read PDF File Example

java apache pdfbox

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).

Going Beyond Java 8: Local Variable Type Inference (var) - DZone Java

In this article, we will talk about the most important new feature introduced with Java 10, officially called local variable type inference. An extremely important function in java. You will regret skipping this article.

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

Spring Boot and Java 16 Records

In this article, we will discuss Java 16's newest feature, Records. Then we will apply this knowledge and use it in conjunction with a Spring Boot application. An extremely important function in java. You will regret skipping this article.

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