Search code examples
c#exceladobevba

Fill PDF form data from Excel with Adobe Acrobat Pro


Is there any to fill PDF form fields using an Excel Data source? (Think 1000 pdf's)

For each row in Excel create a new blank pdf based from template "myForm.pdf" and fill in the matching values from the columns in Excel to the fields in "myForm.pdf" then save it as "myForm(i).pdf"

Finally Merge all the pdfs into a single PDF document.

VBA or Javascript for Adobe is fine as long as the concept holds true. Some manual parts are ok if it's not too much.

Not too many tutorials out there apparently so I really appreciate any expertise here.

Thanks!


Solution

  • As promised :)

    TRIED AND TESTED

    Note: requires iTextSharp.

    // Add reference to iTextSharp.dll. Freely available on the web. Free to use for non commercial applications.
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using iTextSharp;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.xml;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // Original Pdf
                string PDFFile = "C:\\MyOriginalPDF.pdf";
            
                // New Pdf
                string newPDFFile = "C:\\NewPDFFILE.pdf";
    
                PdfReader pdfReader = new PdfReader(PDFFile);
                PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newPDFFile, FileMode.Create));
    
                AcroFields pdfFFields = pdfStamper.AcroFields;
    
                // Fill PDF Form Fields
                pdfFFields.SetField("FieldName1", "Value1");
                pdfFFields.SetField("FieldName2", "Value2");
                //
                // And So on
            
                // Use this to remove editting options by setting it to false
                // To keep editing option leave it as TRUE
                pdfStamper.FormFlattening = true;
    
                // close the pdf
                pdfStamper.Close();
            }
        }
    }
    

    Edit: To interact with Excel From C#, I would recommended visiting the below mentioned link.

    Topic: VB.NET and Excel

    Link: VB.NET and Excel

    To convert the VB.Net Code to C# use the below link :)

    Topic: Convert C# Code to VB.Net and vice-verse

    Link: http://www.developerfusion.com/tools

    Do let me know if you are still stuck and we will take it from there... :)

    More Edits!!!

    Inspired by your post, I finally ended up writing an article on my blog. Now all you have to do is copy the entire code and convert it into C# and make relevant changes to suit your needs :)

    Topic: Fill/Retrieve data from PDF Form Fields using VB.Net From an Excel File

    Link: http://www.siddharthrout.com/index.php/2018/08/28/fillretrieve-data-from-pdf-form-fields-using-vb-net-from-an-excel-file/