Pass data from a form into a pdf
In the previous tutorial we had created a new form using the import of a pdf document.
The simple example form is used to enter some customer data.
In the following example we now want to return the entered data from todo4teams back to the original PDF.
In the first step we create in our form a corresponding action button "Übernahme nach pdf".
The functionality for the transmission of data back to the original pdf-file is made via Javascript in the callback action of this button.
The following sample script for the callback action performs various commands, including
- the path to the original pdf-file is set.
- the fields defined in the form will be transformed and mapped to the field names of PDFs.
- the PDF is generated and will be displayed in a separate window.
We also find again the already known unfavorable form field named "Textfeld 1" from the previous example, but this time it is filled with the contents of the box "Kunde_Vorname" from our form in todo4teams.
var tmpdir = java.lang.System.getProperty("java.io.tmpdir");
var sep = java.lang.System.getProperty("file.separator");
// Pfad des zu erstellenden PDF-Dokuments:
var filename = tmpdir+sep+"Tutorial PDF.pdf";
// Laden der PDF-Vorlage über HTTP:
var url = new java.net.URL("file:///home/marcus/Dokumente/Tutorials/Import und Export von PDF's/formular.pdf");
var pdfreader = new com.lowagie.text.pdf.PdfReader(url.openStream());
var stamp = new com.lowagie.text.pdf.PdfStamper(pdfreader, new java.io.FileOutputStream(filename));
var form = stamp.getAcroFields();
var Kunde_Vorname = helper.getFormValue(formName,"Kunde_Vorname");
form.setField("Textfeld 1", Kunde_Vorname);
var Kunde_Name = helper.getFormValue(formName,"Kunde_Name");
form.setField("Name", Kunde_Name);
var Kunde_Strasse = helper.getFormValue(formName,"Kunde_Strasse");
form.setField("Strasse", Kunde_Strasse);
var Kunde_PLZ = helper.getFormValue(formName,"Kunde_PLZ");
form.setField("PLZ", Kunde_PLZ);
var Kunde_Ort = helper.getFormValue(formName,"Kunde_Ort");
form.setField("Ort", Kunde_Ort);
stamp.close();
stamp.close();
try{
// Fertiges Dokument öffnen:
java.awt.Desktop.getDesktop().open(new java.io.File(filename));
}
catch(e){
println(e);
}
// println('ok');
That is the look of the button's draft after inserting the Javascript code:
To validate the functionality we switch in the form editor to the interactive mode, fill the fields with content and press the button "Übernahme nach pdf".
The original pdf-file will be opened with the data entered in todo4teams.
In case of some existing formatting errors these will show up in the finished pdf.
In our example the height of the form fields may have to be adjusted to be able to read the text in full.
It is sufficient for this purpose to change the original pdf-file. Adjustments in todo4teams must not be made, though however the field names within the pdf may not be changed.
The pdf was edited and the line height increased so that the content is now completely visible.