To get you started if you have never done any of this before, let’s go right to first principles, and show just how simple it is creating your own PDF files from scratch – in just 5 lines of code.
First of all, go off and get yourself at least a developer licence for iText. You know you want to!
The basic creation of a PDF has 5 simple steps
- create a new document
- create a writer to output the document
- open the document
- add some text to the document
- close the document – which sends the finished item to the writer you created earlier
The code to do this is as simple as the 5 steps above – YOURFILE is a legal Windows or Mac path to the file you want to create, and will work best if you send it as a parameter to the code
//step 1 document = new Document() //step 2 PdfWriter.getInstance(document, new FileOutputStream(YOURFILE)) //step 3 document.open() //step 4 document.add( new Paragraph ( 'Hello World!' ) ) //step 5 document.close()
Before this will work you need to ‘import’ some classes (think of them as FileMaker custom functions) at the top of the code, after loading the iText and licence JAR into memory
import com.itextpdf.text.pdf.PdfWriter import com.itextpdf.text.Document import com.itextpdf.text.Paragraph
And you should also have this code to use your XML licence file, which should be in a global container field. All our examples assume the licence file is in Globals::g_LICENCE
import com.itextpdf.license.LicenseKey //edit this to point to your XML licence file LFILE = 'Globals::g_LICENCE' kz = new LicenseKey() kl = kz.loadLicenseFile(fmpro.getContainerStream( LFILE ))
Example file is here – in two formats, one to paste into your own ScriptMaster file and one ready to go