Last active
May 3, 2023 05:31
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
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
I have one suggestion give one demo below how this code will work so every one will easily understood