SnT Thursday - PDF Creation
As proposed by Bruce, Rock et.al., today is Show 'n Tell day.
I'm sure a lot of bloggers will describe some of the bigger projects they have done in the past, so I'll start with just a small but usefull code snippet: How to create pdf files with Domino.
There are a number of ways to do that (as usual), but i will show you the fastest and easiest way today.
1) Create a pdf template (for example create a letter or report in word, transfer to pdf and add some pdf fields using a pdf editor)
2) Download iText (here)
3) Create a java agent and add the iText library's.
4) Have the java agent collect the data you want to convert into a pdf and use the following lines of code to create and populate the pdf document:
// read the pdf template
PdfReader reader = new PdfReader(new URL(url));
// create the pdf document
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(fileName));
//=> Get and set the fields
AcroFields form = stamp.getAcroFields();
// set single value field:
form.setField("fieldname.0",fieldvalue);
// for fields with multiple values multiple fields are created in the pdf template ending in .0, .1 etc (this is the standard naming in pdf templates)
// transfer multiple values to multiple fields
Vector sM = notesdocument.getItemValue("MVField");
for (int i=0; i > sM.size(); i++){
  field = "fieldname." + String.valueOf(i);
  value = String.valueOf(sM.elementAt(i));
  form.setField(field ,value);
}
// Flatten the pdf to prevent editing and close
stamp.setFormFlattening(true);
stamp.close();
The pdf document is now populated and can be mailed, added to a database or displayed on the web.
Technorati tag: Show-n-Tell Thursday
 16 February 2006  comments (19)