In this tutorial, we shall learn to setup a Java project with PDFBox, and start working with PDFBox examples.

Steps to Setup a Java project with PDFBox

Following are the steps to be followed to setup PDFBox in Eclipse Java Project. The steps should remain the same for other IDEs as well.

  1. Create a new Java Project in Eclipse, PdfBox2Examples.
    File ? New ? Java Project
  2. Download jars from https://pdfbox.apache.org/download.cgi.

Download PdfBox jars - Setup a Java project with PdfBox - PdfBox Tutorial - www.tutorialkart.com
Download PdfBox Jars

Download apache commons logging jar from here.

Add all these jars to the Build Path.

Select Project “PdfBox2Examples” ? File ? Properties ? Java Build Path ? Libraries ? Add JARs

Setup Build Path - Setup a Java project with PdfBox - PdfBox Tutorial - www.tutorialkart.com
Setup Build Path

The Java Project, PdfBox2Examples, is ready to work with PDFBox libraries.

Run the following example to verify if the setup is successful.

Now, we shall run the following example in the project, to confirm if the setup is successful.

ExtractPdfText.java

</>
Copy
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

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

/**
 * Example program to verify the setup of pdfbox libraries
 */
public final class ExtractPdfText {

	/**
	 * Print text present in the document
	 */
	public static void main( String[] args ) throws IOException
	{
		String fileName = "sample.pdf"; // provide the path to pdf file
		PDDocument document = null;
		
		try
		{
			document = PDDocument.load( new File(fileName));
			PDFTextStripper stripper = new PDFTextStripper();
			String pdfText = stripper.getText(document).toString();
			System.out.println( "Text in the area:" + pdfText);
		}
		finally
		{
			if( document != null )
			{
				document.close();
			}
		}
	}
}

Conclusion

In this PDFBox Tutorial, we have learnt to setup a Java project with PDFBox, and start working with PDFBox examples.