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. 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 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


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

Setting it up

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();

Click here to see the code in ExampleHelper

Go back to List

View PDF files


Q. Is there a simple example to view a PDF file?

org/jpedal/examples/jpaneldemo/JPanelDemo.java is a very simple java PDF viewer with minimal functionality. But we would recommend you look at the full viewer as it is totally configurable and does everything for you.


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).

Viewer viewer = new Viewer(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 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.

 

Go back to List

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();
}

Go back to List

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

/**instance of PdfDecoder to convert PDF into image*/
PdfDecoder decode_pdf = new PdfDecoder(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.

Go back to List

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

Go back to List

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

Go back to List

PDF Text questions


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

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

Go back to List