---
title: "C# PDF Form API for data extraction | Nutrient Document Converter Services"
canonical_url: "https://www.nutrient.io/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/csharp-fill-pdf-form-with-xfdf-data/"
md_url: "https://www.nutrient.io/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/csharp-fill-pdf-form-with-xfdf-data.md"
last_updated: "2026-06-09T10:28:13.461Z"
description: "Use our C# PDF Form API to extract and import data from PDF forms without server setup. Start your free trial today!"
---

# Fill PDF forms with XFDF data

This code uses the `OpenService` and `CloseService` methods from yjr PDF Converter API [`DocumentConverterServiceClient`](https://www.nutrient.io/guides/document-converter/document-converter-services/knowledge-base/open-and-close-documentconverterserviceclient/) sample code and fills a PDF form with data from an XFDF file:

```csharp

        /// <summary>
        /// Fill the PDF form <paramref name="templateFileName"/> with data from the XFDF file <paramref name="sourceFile"/>
        /// </summary>
        /// <param name="sourceFile">XFDF data file</param>
        /// <param name="templateFileName">PDF Form template</param>
        /// <param name="serviceURL">Document Converter Services end point</param>
        /// <param name="targetFolder">Destination folder</param>
        static void FillPDFForm(string sourceFile, string templateFileName, string serviceURL, string targetFolder)
        {
            DocumentConverterServiceClient client = null;

            try
            {
                // ** Determine the source file and read it into a byte array.
                Console.WriteLine($"Reading {sourceFile}");
                byte[] dataFile = File.ReadAllBytes(sourceFile);

                // ** Determine the template file and read it into a byte array.
                Console.WriteLine($"Reading {templateFileName}");

                byte[] templateFile = File.ReadAllBytes(templateFileName);

                // ** Open the service and configure the bindings.
                client = OpenService(serviceURL, 255);

                //** Set the absolute minimum open options.
                OpenOptions openOptions = new OpenOptions();
                openOptions.OriginalFileName = Path.GetFileName(sourceFile);
                openOptions.FileExtension = Path.GetExtension(sourceFile);

                // ** Create converter specific settings object and specify template data.
                ConverterSpecificSettings_PdfFormsDataImporter pdfFormsDataImporterSettings = new ConverterSpecificSettings_PdfFormsDataImporter();
                pdfFormsDataImporterSettings.PdfTemplateData = templateFile;

                // ** Set the absolute minimum conversion settings. Also, use the converter specific settings defined above.
                ConversionSettings conversionSettings = new ConversionSettings();
                conversionSettings.Format = OutputFormat.PDF;
                conversionSettings.Fidelity = ConversionFidelities.Full;
                conversionSettings.Quality = ConversionQuality.OptimizeForPrint;
                conversionSettings.ConverterSpecificSettings = pdfFormsDataImporterSettings;
                conversionSettings.PDFProfile = PDFProfile.PDF_A2B;

                // ** Carry out the import.
                Console.WriteLine("Importing data file '" + Path.GetFileName(sourceFile) +
                                  "' into template file '" + Path.GetFileName(templateFileName) + "'");
                byte[] convFile = client.Convert(dataFile, openOptions, conversionSettings);

                // ** Write the result back to the file system with a PDF extension.
                string destinationFileName = Path.GetFullPath(Path.Combine(targetFolder, $"{Path.GetFileNameWithoutExtension(sourceFile)}.{conversionSettings.Format}"));
                using (FileStream fs = File.Create(destinationFileName))
                {
                    fs.Write(convFile, 0, convFile.Length);
                    fs.Close();
                }

                Console.WriteLine($"Result saved to {destinationFileName}");

                // ** Open the generated PDF file in a PDF reader.
                Console.WriteLine("Launching file in PDF Reader");
                Process.Start(destinationFileName);
            }
            catch (FaultException<WebServiceFaultException> ex)
            {
                Console.WriteLine("FaultException occurred: ExceptionType: " +
                                 ex.Detail.ExceptionType.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                CloseService(client);
            }

        }

```
---

## Related pages

- [Extract and import PDF form data with C#](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/csharp-server.md)
- [Easily extract and import PDF form data in .NET](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/dotnet-core-server.md)
- [Extract PDF form data with C#](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/csharp.md)
- [Effortlessly extract and import PDF form data](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/dotnet-core.md)
- [PDF form API and server](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms.md)
- [Easily extract and import PDF form data with Java](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/java-server.md)
- [Streamline PDF form data extraction with Java](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/java.md)
- [PHP Fill PDF Form API](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/php.md)
- [JavaScript Fill PDF Form API](/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/javascript.md)

