Fill in a template PDF document

Manipulate PDF, Generate PDF
7/15/2015

Fill in a template form for all instances in a database

This code sample shows you how you can import a template document and fill it in with data from a database. This can be used for example for salary slips, where you create a template with fields for employee firstname, last name, salary etc. etc. after which you can fill the fields with data from a database; all in one go!

In the following image you can see the fields in the resulting PDF, notice how they have been flattened and cannot be altered anymore by the user.

append-template.PNG

C# code sample

1 string[] names = { "Robert", "Stam", 2 "Frank", "Rem", 3 "Marnix", "van Valen", 4 "Marco", "Kesseler" }; 5 6 //open the template document 7 using (FileStream templateStream = new FileStream(@"..\..\template.pdf", FileMode.Open, FileAccess.Read, FileShare.Read)) 8 { 9 Document templateDoc = new Document(templateStream); 10 11 Page templatePage = templateDoc.Pages[0]; 12 TextField firstName = templateDoc.Fields["FirstName"] as TextField; 13 TextField lastName = templateDoc.Fields["LastName"] as TextField; 14 15 //make sure all widgets on this page will be flattened 16 foreach (Widget widget in templatePage.Widgets) 17 { 18 widget.Persistency = WidgetPersistency.Flatten; 19 } 20 21 // create the result document 22 Document outputDocument = new Document(); 23 24 for (int i = 0; i < names.Length; i += 2) 25 { 26 //set the field info in the template page. 27 firstName.Value = names[i]; 28 lastName.Value = names[i + 1]; 29 30 //append the cloned page to the output. 31 outputDocument.Pages.Add(templatePage.Clone()); 32 } 33 34 //write result to disk. 35 using (FileStream outFile = new FileStream(@"..\..\appendTemplate.pdf", FileMode.Create, FileAccess.Write)) 36 { 37 outputDocument.Write(outFile); 38 } 39 }

VB.NET code sample

1 Dim names As String() = {"Robert", "Stam", "Frank", "Rem", "Marnix", "van Valen", _ 2 "Marco", "Kesseler"} 3 4 'open the template document 5 Using templateStream As New FileStream("..\..\template.pdf", FileMode.Open, FileAccess.Read, FileShare.Read) 6 Dim templateDoc As New Document(templateStream) 7 8 Dim templatePage As Page = templateDoc.Pages(0) 9 Dim firstName As TextField = TryCast(templateDoc.Fields("FirstName"), TextField) 10 Dim lastName As TextField = TryCast(templateDoc.Fields("LastName"), TextField) 11 12 'make sure all widgets on this page will be flattened 13 For Each widget As Widget In templatePage.Widgets 14 widget.Persistency = WidgetPersistency.Flatten 15 Next 16 17 ' create the result document 18 Dim outputDocument As New Document() 19 20 For i As Integer = 0 To names.Length - 1 Step 2 21 'set the field info in the template page. 22 firstName.Value = names(i) 23 lastName.Value = names(i + 1) 24 25 'append the cloned page to the output. 26 outputDocument.Pages.Add(templatePage.Clone()) 27 Next 28 29 'write result to disk. 30 Using outFile As New FileStream("..\..\appendTemplate.pdf", FileMode.Create, FileAccess.Write) 31 outputDocument.Write(outFile) 32 End Using 33 End Using