This short tutorial contains information on how to fill PDF form with Excel data using C#. You will learn all the steps to load the PDF and Excel files and then read data from an XLSX file to fill in the PDF form fields. This will enable you to write a program to auto fill PDF form from Excel using C# such that for each record in the Excel file a separate PDF file will be created.
Steps to Fill Out PDF Form with Excel Data using C#
- Establish the IDE environment to use Aspose.PDF and Aspose.Cells for .NET using the NuGet package manager
- Load the PDF file having form fields into the Document object that is to be filled with data from an Excel file
- Load the source Excel file having required data using the Workbook class object
- Iterate through all the rows in the source Excel file
- For each iteration, get the reference to the target field in the source PDF file
- Set the field value to the desired column in the current row of the Excel file
- Repeat the iteration for all the rows and save each PDF as a separate file on the disk
These steps guide us in filling PDF form from Excel using C# by providing details about the establishment of the IDE followed by API calls to load both the template PDF file and source Excel file. Once the Excel file is loaded, all the desired rows are iterated such that during each iteration data is fetched from different columns in the current row and filled in the target fields of the loaded PDF file. During the iteration, the PDF file is saved separately for each record in the Excel file.
Code to Autofill PDF from Excel using C#
This code demonstrates the process to fill PDF form with Excel data using C# by using two libraries simultaneously to work with PDF and Excel such that the Document class is used to load the PDF file and the Workbook class is used to load the Excel file. The Workbook class contains a worksheets collection and each worksheet has collection of cells that can be accessed to read/write data in it. On the other hand, the Document class has the options to access different form fields by name and then set its value as per the requirements.
This tutorial guides how using C# fill PDF form automatically. If you want to learn the process to find and replace text in a PDF, refer to the article on how to find and replace text in PDF using C#.