Writing code

Groovy is a programming language written as a ‘superset’ of Java, a dynamic language with features similar to those of Python, Ruby, but which compiles to Java.

Groovy code can be more compact, because it does not require all the elements that Java requires, with a syntax that is much easier to read.

You will need to become familiar with the syntax to help with troubleshooting and amending the examples, but once you have mastered the basics you are likely to find it easy to use. There are many resources on the web to help you, but here are a few starters.

  • The core syntax is reversed from FileMaker scripting where we say what to do, and on what: e.g. Length(Table::Field)
  • In Groovy, we apply the ‘method’ after saying what it applies to: e.g. number.length() (this applies the length() method to a number object)
  • The big plus is that Groovy is an object-oriented language.
  • Everything we create is an object of one sort or another – an Integer, an Array, a List, a String for example and for each of these there is a set of actions we can apply to them, and this uses the dot-notation.
  • Get used to reading code in this way – here’s a thing, DOT, now do this to that thing.

What about JAR files?

A jar file is a compressed collection of Java classes which we can use once you have ‘loaded’ the jar file into the JVM created when you start up FileMaker with the ScriptMaster plug-in installed.
There are many of them available, covering a wide range of additional functionality, and here we are dealing with iText of course. With the iText.jar loaded into memory then everything inside it is available to our FileMaker code.
There are four jars which provide iText functions, although not all of them will be needed for most basic functions.

  1. itextpdf-x.y.z.jar: the core library
  2. itext-xtra-x.y.z.jar: extra functionality (PDF 2 and more!)
  3. itext-pdfa-x.y.z.jar: PDF/A-related functionality
  4. xmlworker-x.y.z.jar: XML (and HTML) functionality
  5. you will also need to also download the Bcpkix and Bcprov jars from BouncyCastle as they are no longer bundled into the iText jar

First steps

The common pattern you will see is that we need to import the classes required as the first step.

import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.Document

Once the class is loaded it can then be used on your code. This next line creates an object, called ‘doc’, which is actually an object with the Document class

doc = new Document()

If we then ask it what kind of object it is:

return doc.getClass()

we will get the answer:

class com.itextpdf.text.Document

Get used to troubleshooting your code by asking what class things are, as only certain actions can be used on specific classes of object.

Getting a result

At the end we also need to know if our code has been successful. This will normally be done by returning True or the value or text of an error to the calling FileMaker script. You should become used to reading any error messages as they will always give a clue as to what was unsuccessful. These may be generic rather specific, but will apply to the first error found in your code. Unlike scripting in FileMaker you are not protected from your own errors and there are some basic syntax errors that can cause untold pain.

  • Unbalanced ( ) or { } at both ends of code
  • Unbalanced ” ” or ‘ ‘ round text or literals
  • Trying to do something to an empty object
  • CapITaliSAtion!!! – Groovy is CASE SENSITIVE.
  • not creating an object and then trying to use it
  • not closing things when it is required
  • using the wrong ‘type’ of parameter e.g. 1 can be a string or a number
  • using a 1 which is an integer when 1 or 1.0 (a Float, or Double) is required

Creating useful functions

You can create simple functions which just do one job, or something more complex which can produce either one complex result OR multiple different results depending what you feed it as parameters.
With iText there are 3 basic operations and two more advanced areas

  • Create a file from scratch
  • Adapt an existing file or files
  • Stamp a file – literally like writing over something with a marker
  • Fill in and create Forms
  • Apply a digital signature

In addition it is possible to convert HTML to PDF, and hopefully you will learn over time to progress down this list.

Where to learn

I really recommend getting a copy of the reference book, from the publishers Manning or Amazon, and iText also has a substantial set of examples on their developer website to accompany the book. All their examples are written in Java, so they can be simplified greatly, and also a couple of things don’t work as expected if you just try and copy the code, but they are a great resource for learning the basic principles. iText developers hang out on StackOverflow so get an account and start searching for answers there too.
This site, and the support site, will offer simple working examples from which you can learn, there is a wider range of sample code available through the shop which will get released over time, and in response to your requests, and we will also make available more complex examples for you to purchase (to save the time involved in learning)

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/43/d71805609/htdocs/clickandbuilds/FMtoPDF/wp-includes/class-wp-comment-query.php on line 399