How to import and export PDF form data in C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can import and export PDF form data using C# and VB.NET.
Steps to import and export PDF form data programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Parsing
- Use the following code snippet to import and export PDF form data.
C#
//Load an existing document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"../../Input/FormFill.pdf"); //Load an existing form PdfLoadedForm loadedForm = loadedDocument.Form; MemoryStream memoryStream = new MemoryStream(); //Export the existing PDF document to FDF file loadedForm.ExportData("Export.fdf", DataFormat.Fdf, @"AcroForm1"); //Close the document loadedDocument.Close(true); //Load an existing document PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(@"../../Input/Form.pdf"); //Load an existing form PdfLoadedForm loadedForm1 = loadedDocument1.Form; //Load the FDF file FileStream stream1 = new FileStream(@"Export.fdf", FileMode.Open); //Import the FDF stream loadedForm1.ImportDataFDF(stream1, true); loadedDocument1.Save(@"Output.pdf"); //Close the document loadedDocument1.Close(true);
VB.NET
'Load an existing document Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Input/FormFill.pdf") 'Load an existing form Dim loadedForm As PdfLoadedForm = loadedDocument.Form Dim memoryStream As MemoryStream = New MemoryStream 'Export the existing PDF document to FDF file loadedForm.ExportData("Export.fdf", DataFormat.Fdf, "AcroForm1") 'Close the document loadedDocument.Close(True) Dim loadedDocument1 As PdfLoadedDocument = New PdfLoadedDocument("../../Input/Form.pdf") Dim loadedForm1 As PdfLoadedForm = loadedDocument1.Form Dim stream1 As FileStream = New FileStream("Export.fdf", FileMode.Open) 'Import the FDF stream loadedForm1.ImportDataFDF(stream1, True) loadedDocument1.Save("Output.pdf") 'Close the document loadedDocument1.Close(True)
A complete working sample can be downloaded from ImportExportPDFSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with forms, where you will find other options like filling, removing form fields in PDF document, and adding actions to form fields.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An Online sample link to filling form fields into PDF document.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.