Using Java inheritance to customise your PDF forms in our PDF library/converter

One of my favourite features of Java is inheritance. It allows you to extend someone else’s Java code, replacing just the bits you need. Customising our PDF forms code is a really nice example of how it works.

We have an interface (FormFactory) which provides the GUI components to represent PDF forms. Most of the time we deal with the forms as an object (we recommend you generally use the object not the gui component as they sync themselves and the object is more flexible). But you still need the GUI representation. So we have 3 versions of FormFactory:-

1. SwingFormFactory provides the Swing components (so it will generate a JTextField, etc) and setup with images,etc.

2. ULCFormFactory provides the same for our implementation which uses Canoo’s ULC Components.

3. HTMLFormFactory and SVGFormFactory works slightly differently. They write out the HTML or SVG form code needed into the HTML5 or SVG.

Having an instance of FormFactory makes it much easier for us to code and to decouple the GUI implementation from the main decoder (which does not need to know what it is creating – it just creates the FormObject and passes it to the instance of FormFactory). So we could easily create an implementation for JavaFX (JavaFXFormFactory) and we would just need to add code to create JavaFX forms in the new class.

It also makes it much easier for users to extend or tweak the forms code. It is not a good idea to try and replace our implementation (which is updated by us on a regular basis) but Java makes it very easy to replace just the bits you want by extending the class. If you wanted to change the popup fields, you could do the following:-

1. Create a new class MyCustomSwingFormFactory which extends SwingFormFactory

2. Add a single method

public JComponent getPopupComponent(FormObject form, PdfObject popupObj, int cropBoxWith)

This will replace the code in the default version. Note it returns an Object, which we will cast to JComponent. This will replace the default version we have (shown below).

/**
* public method to allow user to replace Popup with their own implementation
* @param form
* @param popupObj
* @return Swing component to use as popup (see org.jpedal.objects.acroforms.overridingImplementations.PdfSwingPopup)
*/
public JComponent getPopupComponent(FormObject form, PdfObject popupObj, int cropBoxWith) {
return new PdfSwingPopup(form,popupObj,cropBoxWith);
}

3. Tell the software to use your new version of the FormFactory. This is explained in this tutorial.

This works really nicely as it means any fixes or improvements we add to our SwingFormFactory will automatically be picked up by your version without you even having to know about them.

You could also use the same mechanism to add your own custom forms into HTML5 or SVG by using your own extended version of HTMLFormFactory. Maybe an article for another day?

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>