In this article, we will see some examples for creating and reading a PDF file using Apache PDFBox.
We can create a Java project using an IDE like Eclipse or use a build tool like Maven to create a java project.
Here is the maven archetype we will be using :
mvn archetype:generate –DgroupId=com.mycompany.app –DartifactId=PDFUtil –DarchetypeArtifactId=maven–archetype–quickstart –DinteractiveMode=false
Add the following dependency for Apache PDFBox:
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.0</version> </dependency>
For maven basics, refer this article :
Maven Basics
If you are creating the project directly using Eclipse, the jar file can be downloaded from http://pdfbox.apache.org
Creating a PDF and writing to it
In this example, we will create a PDF file and write some text to it.
package com.mycompany.app; 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.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; /** * Hello world! * */ public class App { public static void main( String[] args ) { try { createPDFDocument("C:\\blogs\\TestPDF.pdf"); } catch (IOException e) { e.printStackTrace(); } } private static void createPDFDocument(String name) throws IOException{ // Create a new empty document PDDocument document = new PDDocument(); // Create a new blank page and add it to the document PDPage page = new PDPage(); document.addPage( page ); // Create a new font object selecting one of the PDF base fonts PDFont font = PDType1Font.TIMES_ROMAN; // Start a new content stream which will "hold" the to be created content PDPageContentStream contentStream = new PDPageContentStream(document, page); // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World" contentStream.beginText(); contentStream.setFont( font, 12 ); contentStream.moveTextPositionByAmount( 100, 700 ); contentStream.drawString( "Hello World" ); contentStream.endText(); // Make sure that the content stream is closed: contentStream.close(); // Save the newly created document document.save(name); // finally make sure that the document is properly // closed. document.close(); }
Output :
Reading Text from the PDF
Add the following method to read the text from the PDF file.
private static void readPDFDocument(String name) throws IOException{ PDDocument pdf = PDDocument.load(new File(name)); PDFTextStripper stripper = new PDFTextStripper(); String pdfText = stripper.getText(pdf); System.out.println("Text from the pdf is : " + pdfText); }
public static void main( String[] args ) { try { readPDFDocument("C:\\blogs\\TestPDF.pdf"); } catch (IOException e) { e.printStackTrace(); } }
Output :
Text from the pdf is : Hello World
Reference
You may also like :
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post