• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

How to use PDFBox to Create / Read PDF in Java + Maven

November 20, 2017 by javainterviewpoint Leave a Comment

Apache PDFBox is an open source library for Java to work with PDF documents.In this article, we will learn how to use PDFBox to Create / Read PDF in Java. 

In order to use Apache PDFBox we need to have the following dependencies added in your project.

  • pdfbox-2.0.7.jar
  • fontbox-2.0.7.jar
  • commons-logging-1.2.jar

If you are running on maven add the below dependency to your pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javainterviewpoint</groupId>
	<artifactId>PDFBoxExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>PDFBoxExample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.7</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Other Posts which you may like …

  • Jackson 2 JSON Parser – Convert JSON to/from Java Object
  • How to Convert JSON to / from Java Map using JACKSON
  • Jackson Tree Model Example – JsonNode
  • Jackson Streaming API Example | Read and Write JSON
  • Jackson JSON Example | ObjectMapper and @JSONView
  • How to Parse JSON to/from Java Object using Boon JSON Parser
  • How to Read and Write JSON using GSON
  • How to write JSON object to File in Java?
  • How to read JSON file in Java – JSONObject and JSONArray
  • Jersey Jackson JSON Tutorial
  • Spring REST Hello World Example – JSON and XML responses
  • How to Convert Java Object to JSON using JAXB
  • How to Read Excel File in Java using POI
  • How to Write Excel File in Java using POI
  • JAXB Tutorial – What is JAXB
  • JAXB Marshalling Example – Convert Java Object to XML in Java
  • JAXB UnMarshalling Example – Convert XML to Java Object
  • How to Convert Java Object to JSON using JAXB

How to use PDFBox to Create / Read PDF

PDFBox create PDF Example

package com.javainterviewpoint;
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;

public class CreatePDF
{
    public static void main(String[] args)
    {
        //Creating a new document
        PDDocument document = new PDDocument();
        
        //Creating a new page and adding it to the document
        PDPage page = new PDPage();
        document.addPage(page);
        
        PDFont font = PDType1Font.HELVETICA_BOLD_OBLIQUE;
        
        try
        {
            //ContentStream holds the content
            PDPageContentStream contentStream = new PDPageContentStream(document,page);
                        
            //Set the starting offset for contentStream and font
            contentStream.beginText();
            contentStream.setFont(font, 14);
            //Text offset
            contentStream.newLineAtOffset(100, 500);
           
            //Display the mentioned text at the offset specified
            contentStream.showText("PDF created using Apache PDFBox 2.0");
            contentStream.endText();
         
            //Closing the contentStream
            contentStream.close();
            //Location for saving the pdf file
            document.save("c://JavaInterviewPoint//Hello.pdf");
            //Closing the document
            document.close();
        }
        catch(IOException ie)
        {
            ie.printStackTrace();
        }
    }
}

In order to create a new PDF all we need to do is

  • Create a instance of PDDocument and PDPage
PDDocument document = new PDDocument();
PDPage page = new PDPage();
  • Add the page to the document
document.addPage(page);
  • Create a new PDPageContentStream instance passing the above created document and page
PDPageContentStream contentStream = new PDPageContentStream(document,page);
  • Using the showText() method display the content which we need to display
contentStream.showText("PDF created using Apache PDFBox 2.0");
contentStream.endText();
  • Finally, close the PDPageContentStream, PDDocument
document.save("c://JavaInterviewPoint//Hello.pdf");
document.close();

Changing PDFBox PDType0Font

By default PDFBox supports a standard set of 14 fonts listed below, which will always be available when consuming PDF documents.

Standard Font Description
PDType1Font.TIMES_ROMAN Times regular
PDType1Font.TIMES_BOLD Times bold
PDType1Font.TIMES_ITALIC Times italic
PDType1Font.TIMES_BOLD_ITALIC Times bold italic
PDType1Font.HELVETICA Helvetica regular
PDType1Font.HELVETICA_BOLD Helvetica bold
PDType1Font.HELVETICA_OBLIQUE Helvetica italic
PDType1Font.HELVETICA_BOLD_OBLIQUE Helvetica bold italic
PDType1Font.COURIER Courier
PDType1Font.COURIER_BOLD Courier bold
PDType1Font.COURIER_OBLIQUE Courier italic
PDType1Font.COURIER_BOLD_OBLIQUE Courier bold italic
PDType1Font.SYMBOL Symbol Set
PDType1Font.ZAPF_DINGBATS Dingbat Typeface

In our previous example we have used “HELVETICA_BOLD” font, “PDType1Font” font supports only the above mentioned 14 fonts. In order to use a custom font then we have to use “PDType0Font” passing our custom font. lets look into the below example where we try to create a PDF with “CALIBRI” font.

package com.javainterviewpoint;

import java.io.File;
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.PDType0Font;

public class ChangeFont
{
    public static void main(String[] args)
    {
        // Creating a new document
        PDDocument document = new PDDocument();

        // Creating a new page and adding it to the document
        PDPage page = new PDPage();
        document.addPage(page);

        try
        {
            // Manually loading the font
            PDFont font = PDType0Font.load(document, new File("c://JavaInterviewPoint//calibri.ttf"));
            // ContentStream holds the content
            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            // Set the starting offset for contentStream and font
            contentStream.beginText();
            contentStream.setFont(font, 14);
            // Text offset
            contentStream.newLineAtOffset(100, 500);

            // Display the mentioned text at the offset specified
            contentStream.showText("Changing the font - Apache PDFBox 2.0");
            contentStream.endText();

            // Closing the contentStream
            contentStream.close();
            // Location for saving the pdf file
            document.save("c://JavaInterviewPoint//Hello1.pdf");
            // Closing the document
            document.close();
        } catch (IOException ie)
        {
            ie.printStackTrace();
        }
    }
}

PDFBox Extract Text Line By Line

In order to extract Text from a PDF we need to use PDFTextStripper class, in the below example we will try to extract text from the first page of the PDF.

package com.javainterviewpoint;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class ExtractText
{
    public static void main(String[] args)
    {
        try
        {
            File file = new File("c://JavaInterviewPoint//Hello.pdf");
            //Reading the pdf file
            PDDocument document = PDDocument.load(file);
            
            //Get the number of pages
            System.out.println("Number of pages in the pdf :"+document.getNumberOfPages());
            
            //Strip the text from a particular page
            PDFTextStripper textStripper = new PDFTextStripper();
            //Lets read page 1
            textStripper.setStartPage(1);
            textStripper.setEndPage(1);
            System.out.println("Text in the pdf >>> "+textStripper.getText(document));
        }
        catch(IOException ie)
        {
            ie.printStackTrace();
        }
    }
}

Output:

Number of pages in the pdf :1
Text in the pdf >>> PDF created using Apache PDFBox 2.0

Filed Under: Core Java, Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·