Java PDF code FAQ

This page lists the frequently asked questions we have encountered on using our PDF library and contains links to code examples which you can use.

The FAQ’s are organized into the following sections:

General View PDF Print PDF PDF Image PDF Font PDF Search PDF Text

General questions

Q. What PDF features are supported?

You can get a full list of the PDF Specification supported here.

Q. Does the software run on client or server?

We have customers using it on both. Viewer, ViewerFX provide viewers, PdfDecoder  provides Swing and non-Swing support and PdfDecoderServer can used on the server in headless mode or for non-GUI and non-printing functions.

Q. Can I log information to a logfile?

//debugging code to create a log
LogWriter.setupLogFile("v"); //echo to screen
LogWriter.log_name =  "/mnt/shared/log.txt"; //logfile

 

Q. How do I get the version number of the software?

There is a public static variable PdfDecoder.version or PdfDecoderServer.version which you can access.

Q. How do I get the size of a PDF page?

There is a small sample program showing how to read the CropBox values for all the page in a PDF file and how to convert into various units. Print out all page sizes

Q. How do I count the number of pages in PDF file?

You need to count the number of entries in the page References object (which is a Tree data structure). Get the page Count
The same code will work on XFA files if you have the XFA version of JPedal.

Q. How do I access the metadata and fields in a PDF file?

A PDF file contains metadata and information fields with optional information details including the file author and PDF creation tools used. Show Document properties

Q. How can I extract the links in a PDF file?

A PDF file usually includes links as Form objects which can be parsed and extracted. Show links in PDF file

Q. How can I extract the outline in a PDF file?

A PDF file can contain an optional outline which can be parsed and extracted. Show outline tree in PDF file

Q. Can I use the software online?

We have put some applications on the website to provide extraction and viewing online. Online PDF tools.

Q. How do I convert PDF to grayscale or black and white?

PDF files are usually displayed and printed in ARGB to provide the highest quality output. Sometimes, you may wish to change this functionality (for example to only print in grayscale or black and white). This tutorial explains how to use hooks in JPedal to achieve this.

JPedal contains an interface org.jpedal.external.JPedalHelper and a static instance of this in PdfDecoder called Helper. Usually Helper is null, but if a static instance of it is created, it will be called at various points. These include controlling the setting of colour and rendering of images, so the ARGB data can be changed.

The interface includes 2 methods which can be used to control the color of the output

 

To activate this functionality, you just have to setup a static instance to PdfDecoder.Helper before you use JPedal. The instance should implement the interface org.jpedal.external.JPedalHelper. There is an example built in to convert all printed output to black and white. To use this you would just add this line to your code before using JPedal.

PdfDecoder.Helper=new org.jpedal. examples.ExampleHelper();

See the Code in ExampleHelper


 

View PDF files

Q. Are there some simple examples to view a PDF file?

org/jpedal/examples/baseviewer/BaseViewer.java and org/jpedal/examples/baseviewer/BaseViewerFX.java are very simple java PDF viewers with minimal functionality.
We would recommend you look our fully featured Swing PDF viewer or JavaFX PDF viewer. They are totally configurable and include lots of functionality (Search/Print/multiple views/etc). You can easily switch between them as they share the same API.

Q. How can I add a Java PDF viewer into my code in 2 lines of code?

Try this Java code (with the JPedal jar on your classpath).

Swing
Viewer viewer = new Viewer(rootContainer, null);
viewer.setupViewer();

Javafx
ViewerFX viewer = new ViewerFX(rootContainer, null);
viewer.setupViewer();

Now the object has been set up correctly it can be added to your application like any other swing component.

Here is the simple Swing application I made, demonstrating how this can be done.

If you wish to remove some or all of the viewer user interface that is also possible by simply setting the preferences to display the desired components and passing in the path (on the file system or within the jar by adding “jar:” to the start of the path) to this file in the Viewer(Container display, String PreferencesPath) constructor.


 

Print PDF files

Q. Is there a complete example to print a PDF file?

org/jpedal/examples/printing/SilentPrint.java is a full code example (with additional features) to print a PDF file.

There is also a 5 step tutorial to add printing to your code on at how-to-print-pdf-files-in-java

Q. Is there a way to Stop printing PDF form objects?

Sometimes you may want to stop PDF Form components printing. JPedal allows considerable flexibility in doing this on any type or subtype of PDF Form Object

This example code will stop printing PDF Forms

try{
//create Map wth int[] array of types and subtypes to ignore (all values in PdfDictionary)
Map map=new HashMap();
//this would stop all subtype popups
map.put(JPedalSettings.IGNORE_FORMS_ON_PRINT,new int[]{PdfDictionary.Popup});
//this would stop all subtype popups and all type PDF Annot
//map.put(JPedalSettings.IGNORE_FORMS_ON_PRINT,new int[]{PdfDictionary.Annot, PdfDictionary.Popup});
//static version for all instances
PdfDecoder.modifyJPedalParameters(map);
//non-static for just this instance
decoder.modifyNonstaticJPedalParameters(map);
}catch(Exception e){
e.printStackTrace();
}


 

PDF Image questions

Q. How do I convert a PDF to Image?

Here is Java code sample to provide PDF to image conversion in Java – just cut and paste into your code (you can also use PDFDecoder if you want the GUI as well).

/**instance of PdfDecoder to convert PDF into image*/
PdfDecoderServer decode_pdf = new PdfDecoderServer(true);

/**set mappings for non-embedded fonts to use*/
FontMappings.setFontReplacements();

/**open the PDF file - can also be a URL or a byte array*/
try {
    	decode_pdf.openPdfFile("C:/myPDF.pdf"); //file
		//decode_pdf.openPdfFile("C:/myPDF.pdf", "password"); //encrypted file
		//decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF
		//decode_pdf.openPdfFileFromURL("http://www.mysite.com/myPDF.pdf",false);
 decode_pdf.setExtractionMode(0, 1f); //do not save images
    	/**get page 1 as an image*/
    	//page range if you want to extract all pages with a loop
    	//int start = 1,  end = decode_pdf.getPageCount();
    	BufferedImage img=decode_pdf.getPageAsImage(1);

	/**close the pdf file*/
	decode_pdf.closePdfFile();

} catch (PdfException e) {
    e.printStackTrace();
}

Q. Is there a complete example to convert a PDF file to Images with options to write out as TIF, JPEG and PNG?

org/jpedal/examples/images/ConvertPagesToImages.java is a full code example (with additional features) to convert PDF to image.

Q. Is there a complete example to convert a PDF file to high resolution Image with options to write out as TIF, JPEG and PNG?

org/jpedal/examples/images/ConvertPagesToHiResImages.java is a full code example with lots of optimisations to produce high resolution versions of a PDF file at the highest quality.

Q. Can I use JPedal to create separations with images, text, shapes on different pages?

There is an entry on our blog with sample screenshots.

Q. How do I save a BufferedImage as a JPEG?

There is an entry on our blog with code for several versions of Java, and a code example in our PDF to Image converter here.


 

PDF Font questions

Q. How do I find out if a PDF file contains embedded fonts?

Code sample to see if any embedded fonts are in the PDF file. See if embedded fonts used


 

PDF Search questions

Q. How do I search for text in a PDF file?

Code sample to search for text in a selected area and return the PDF co-ordinates. Find text

Q. How do I extract the text in a PDF file as a set of words with co-ordinates?

Code sample to extract text as words with the PDF co-ordinates for each. Get text as words with co-ordinates


 

PDF Text questions

Q. How do I select text in the PDF viewer?

There is a tutorial on  highlighting-text-in-viewer