C# .NET - Dynamically populating PDF files from a database

Asked By Henry Taylor on 24-Oct-14 10:46 AM
Before I start understand that I know how to populate pdf files in c# code. This question is geared more toward an algorithm for doing so based on certain aspects of the problem.

I have numerous legal contracts (pdf files) of which a subset will be used and combined into one final larger document depending on what services a customer orders. Most of these pdf files will need to have data pulled from a database to populate fields in the pdf such as First_name, Last_name etc.

In the example above the primary problem is the two fields referenced (First_name, Last_name) are sometimes found in a) different tables and b) they are not always spelled the same (I inherited this mess).

I am thinking it is going to take a certain amount of brute force coding to get this done and that means less flexibility when changes are made or put another way more human intervention to make sure everything is right.  I am told these pdf files do change over time.

I hope I have described the basic problem here.  Is there a masterful way of automating this kind of thing as much as possible?

For instance it occurred to me I could use an XML file to store the SQL statements needed to populate a given pdf file.  Once the name of the pdf file was known the XML node containing the SQL to populate the pdf file could be parsed and executed to bring back the data.  If new fields were added to the pdf file that XML file would need to be updated.

This example solution isn't too bad but I am thinking that maybe someone out there has done this before in a masterful manner and would share that with me.

Thanks.
Robbe Morris replied to Henry Taylor on 25-Oct-14 09:46 AM
Create a table that stores all of the possible field names in the PDF document and map them to the source column and table of that data that is used to populate them.  Should be pretty easy to write code that can handle any of your PDF documents.  Have it handle failure to find PDF fields and their mapped source just in case the document is missing anything.

As for reading and writing to the PDF documents, you'll need a third party component for that.  I use Aspose PDF at aspose.com.