What techniques available to fill a PDF form automatically using external data and save them. I have to use data from a database to fill a template PDF and save a copy of it on disk with that data. Language and platform is not issue but it would be good if it can run on windows and Linux.
If your form is based on AcroForm technology, you can use iText 7 to fill it out like this:
1 2 3 4 5 6 7 8 PdfReader reader = new PdfReader(src); PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(reader, writer); PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true); Map fields = form.getFormFields(); fields.get(key).setValue(value); form.flattenFields(); pdfDoc.close();
1 2 3 4 5 6 7 8 PdfReader reader = new PdfReader(src); PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(reader, writer); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); IDictionary<string, PdfFormField> fields = form.GetFormFields(); fields[key].SetValue(value); form.FlattenFields(); pdfDoc.Close();
In this snippet,
src is the source of a PDF file and
dest is the path to the resulting PDF. The
key corresponds with the name of a field in your template. The
value corresponds with the value you want to fill in. If you want the form to keep its interactivity, you need to remove the
form.flattenFields(); otherwise all form fields will be removed, resulting in a flat PDF.
Click this link if you want to see how to answer this question in iText 5.