Skip to content

Instantly share code, notes, and snippets.

How to Fill PDF Form with Excel Data using C#. For more details: https://kb.aspose.com/pdf/net/how-to-fill-pdf-form-with-excel-data-using-csharp/
using System;
using Aspose.Cells;
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Forms;
namespace KBExample
{
class Program
{
static void Main(string[] args) // Main function to import Excel in PDF
{
// Initialize licenses
Aspose.Pdf.License licPdf = new Aspose.Pdf.License();
licPdf.SetLicense("Aspose.Total.lic");
Aspose.Cells.License licCells = new Aspose.Cells.License();
licCells.SetLicense("Aspose.Total.lic");
// Open PDF template document containing form fields
Document pdfDocument = new Document("PdfWithFormField.pdf");
// Open workbook containing data to be filled in the PDF form
Workbook workbook = new Workbook("InputWorkbook.xlsx");
for (int iRow = 1; iRow <= 15; iRow++)
{
// Get a field
TextBoxField textBoxField = pdfDocument.Form["textboxRollNo"] as TextBoxField;
// Modify field value
textBoxField.Value = workbook.Worksheets[0].Cells[iRow, 0].Value.ToString();
// Get another field
textBoxField = pdfDocument.Form["textboxName"] as TextBoxField;
// Modify field value
textBoxField.Value = workbook.Worksheets[0].Cells[iRow, 1].Value as string;
// Save the updated document as a separate PDF file
pdfDocument.Save($"{textBoxField.Value.Trim()}.pdf");
}
Console.WriteLine("Done");
}
}
}
@Impact14batch
Copy link

I have one suggestion give one demo below how this code will work so every one will easily understood

@shahzad-latif
Copy link

@Impact14batch

Thanks for your comment. Could you please elaborate a bit what exactly you mean? Please note that we already have a related topic which explains this code step by step: https://kb.aspose.com/pdf/net/how-to-fill-pdf-form-with-excel-data-using-csharp/. Please check the link I shared and share if that helps you understand the code or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment