my first PDF

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

  1. create a new document
  2. create a writer to output the document
  3. open the document
  4. add some text to the document
  5. 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
//step 4
document.add( new Paragraph ( 'Hello World!' ) )
//step 5

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

myFirstPDF ZIP

Posted in example

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


Recent Posts