DEV Community

loading...

Java – Apache PDFBox Write/Read PDF File Example

loizenai profile image loizenai ・2 min read

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

Discussion (0)

Forem Open with the Forem app