If you want to view PDF files in your Java application you will quickly find that Java has no native support for the PDF file format. It is for this reason that you would need a Java PDF viewer.
Today I’m going to walk you through the steps required to use the PDF Viewer in our JPedal Java PDF Library. Once installed you will have a Java PDF API to access all aspects of the viewer from your Java code.
How to open PDF files in Java with Maven
There are a few ways you can install JPedal, you can either download the trial JAR from our website, or you can add JPedal to your project dependencies. This will allows you to view PDF documents in Java.
In this tutorial, I am going to be using Maven however you can also use Gradle.
<repositories>
<repository>
<id>IDRsolutions</id>
<name>IDR Solutions</name>
<url>https://maven.idrsolutions.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.idrsolutions</groupId>
<artifactId>jpedal-trial</artifactId>
<version>2022.10</version>
</dependency>
</dependencies>
You can find the latest version number of JPedal on our support site.
Once you have the dependency setup, you can use the code below to view PDF in Java.
How to view the PDF files from the Command line
If you want to quickly view PDF files, you may use the JPedal Viewer. To launch the viewer you can double-click to open the JAR or you can run the following:
java -jar jpedal.jar –view [optional/filename.pdf]
How to view the PDF files in an existing Java application
JPedal provides a very easy to use Java PDF API for adding a PDF Viewer to your Java code. JPedal includes a single Viewer class to open and view PDF files. Creating an instance of the JPedal Viewer only requires two lines of code:
Viewer viewer = new Viewer();
viewer.setupViewer();
You can then use the following to open and display a PDF file:
viewer.executeCommand(Commands.OPENFILE, new Object[] {"pdfFile.pdf"});
How to customise your Java PDF Viewer
You may want to customise the look and feel of the PDF view, for example you might not want customers to be able to print your PDFs, or you might want to hide the menu buttons.
All of this and more is possible using a properties profile:
- Create an XML properties file by exporting and modifying the default one. In JPedal go to Edit -> Preferences and click Save As.
- Customise the properties file to your requirements. For example:
<textSelect value="false"/>
<Printbutton value="false"/>
- Create a viewer and load the properties file using
Viewer viewer = new Viewer("path/to/prefs.xml");
Learn more
Visit www.idrsolutions.com/jpedal to learn more about JPedal.
Visit support.idrsolutions.com/jpedal for comprehensive documentation and tutorials.
Why do developers choose JPedal over alternatives?
- Actively developed commercial library with full support and no third party dependencies.
- Simple licensing options and source code access for OEM users.
- Process PDF files up to 3x faster than alternative Java PDF libraries.
The JPedal PDF library allows you to solve these problems in Java
Viewer viewer = new Viewer();
viewer.setupViewer();
viewer.executeCommand(ViewerCommands.OPENFILE, "pdfFile.pdf");
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});
//Convenience static method (see class for additional options)
ExtractTextAsWordList.writeAllWordlistsToDir("inputFileOrDirectory", "outputDir", -1);
//Convenience static method (see class for additional options)
ArrayList resultsForPages = FindTextInRectangle.findTextOnAllPages("/path/to/file.pdf", "textToFind");
PrintPdfPages print = new PrintPdfPages("C:/pdfs/mypdf.pdf");
if (print.openPDFFile()) {
print.printAllPages("Printer Name");
}
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});