Creating PDF Forms using the Datalogics PDF Java Toolkit

Sample of the Week:

Forms are frequently a customer’s first introduction to a new company or service. When I go to see a new doctor or dentist, I fill out a form. When I want to get a quote for a new car insurance policy, I fill out a form. A few weeks ago, I had to fill out a form just to get a tour of a gym that I wanted to use while traveling… I was in town for a week and had no plans on joining… yet… I had to fill out a form.
A filled form is how we introduce ourselves to a new company or service… but… an empty form is how they introduce themselves to us… so the experience better be a good one… and that starts with establishing or leveraging your brand… and branding is all about fonts.
When PDF files are created by Adobe Acrobat, unless you’ve modified the defaults, fonts that are required to display the document properly will be embedded. But because font files can be large, the fonts are subset so that only the characters that are used in the document are embedded. For example if your company logo, “Datalogics”, for example uses Helvetica Black and that font is only used in the logo, only the 10 characters in the word “Datalogics” are embedded in the document. This works well for the static parts of the document where we know ahead of time what characters will be used. For interactive forms, we need a different approach.
When creating a form in Adobe Acrobat, the Text Field Properties dialog allows you to specify a font for use when displaying the field. Acrobat will automatically attempt to embed the entire font because it doesn’t know what characters will be needed when a user fills out the form. If the font licensing forbids font embedding, Acrobat will warn you but most fonts allow embedding for this purpose.
eval-nowJust like Adobe Acrobat, the Datalogics PDF Java Toolkit can embed fonts in a document for use in displaying PDF form data. However, simply specifying the font property of a new field that you create programmatically is not sufficient, the developer must also tell the Java Toolkit to embed the font which, fortunately, is pretty straightforward. The Datalogics PDF Java Toolkit sample AddFieldUsingGivenFont is highly focused on just this step and shows developers how to load a font for use when creating interactive text fields; PDFFieldText objects. It puts the new font in the Resources dictionary of the PDF and sets up the default appearance for the fields so that they use this font.
Code Snippet:

// Build the font resource.
PDFFont theFont = PDFFontUtils.createFontForEditting(pdfDoc, daFont, embedFont, opentypeOK);
// Add the font to the resources.
PDFInteractiveForm formDict = pdfDoc.requireCatalog().procureInteractiveForm();
PDFFontMap fontMap = formDict.procureResources().procureFontMap();
ASName theName = fontMap.getNewName(daFont);
fontMap.set(theName, theFont);
// Set up the Default Appearance for the new fields.
PDFDefaultAppearance defaultAppearance = PDFDefaultAppearance.newInstance(pdfDoc, theName, 10, 0);

This sample gives developers the flexibility to use whatever fonts they require to conforms to brand guidelines when creating PDF form fields programmatically. View and download the AddFieldUsingGivenFont sample or get all the samples and documentation by requesting an evaluation of the Datalogics PDF Java Toolkit.

Share this post with your friends

Leave a Comment

Your email address will not be published.

Get instant access to the latest PDF news, tips and tricks!

Do you want monthly updates on the latest document technology trends?

By submitting the form, you agree to receive marketing emails from Datalogics. You may unsubscribe at any time. 

Like what you're reading?

Get Datalogics blogs sent right to your inbox!

By submitting the form, you agree to receive marketing emails from Datalogics. You may unsubscribe at any time.