Java – Apache PDFBox Write/Read PDF File Example
Tags: java, pdfbox, pdf
Published: 2021-05-04 18:27:58

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.

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

<pre><code class="language-html">&lt;dependency&gt;
     &lt;groupId&gt;org.apache.pdfbox&lt;/groupId&gt;
     &lt;artifactId&gt;pdfbox&lt;/artifactId&gt;
     &lt;version&gt;2.0.8&lt;/version&gt;
&lt;/dependency&gt;</code></pre>

Project structure ->

<img src="https://grokonez.com/wp-content/uploads/2018/11/java-pdfbox-read-write-text-pdf-file-project-structure.png" alt="java-pdfbox-read-write-text-pdf-file-project-structure" width="757" height="273" class="alignnone size-full wp-image-15479" />

<h2>Write Text to PDF with PDFBox</h2>
- <strong>PdfBox</strong> library provides API which is based on stream manipulation (<code>PDPageContentStream</code> class).

Implement code at <code>PDFBoxWriteText.java</code> ->

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

-> Results:

- Page 1:

<img src="https://grokonez.com/wp-content/uploads/2018/11/java-pdfbox-read-write-text-pdf-file-page-1.png" alt="java-pdfbox-read-write-text-pdf-file-page-1" width="567" height="171" class="alignnone size-full wp-image-15480" />

- Page 2:

More at:

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

Java – Apache PDFBox Write/Read PDF File Example