PM > Install-Package XDoc.PDF

How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Form Library
How to add, fill-in Adobe PDF file form field data value using C#. Open source demo code included


Online C# Tutorial to Automatically Fill in Field Data to PDF with C#.NET Library





In this tutorial, you learn how to fill in PDF AcroForm fields' data programmatically using C#

  • Find a form field, and fill in field data in PDF
  • Fill in data in PDF with existing file path, stream object or byte array
  • Support all AcroForms fields, including check box, radio button, text box, list box, combo box

How to fill-in AcroForm field data on PDF programmatically using C#

  1. Download XDoc.PDF Form C# library
  2. Install C# library to fill-in PDF AcroForm data programmatically
  3. Step by Step Tutorial








  • A best PDF document SDK library for .NET framework enable users the ability to fill in PDF forms in Visual C# .NET class
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • Robust .NET components and dlls for filling in PDF form online in ASP.NET WebForm application
  • A professional PDF form filler control able to be integrated in Visual Studio .NET WinForm and fill in PDF form use C# language
  • Free online C# sample code can help users to fill in PDF form field automatically in .NET
  • Support to fill in form field in specified position of adobe PDF file
  • Able to fill out all PDF form field in C#.NET


RasterEdge XDoc.PDF SDK package provides PDF field processing features for your C# project. On this C# tutorial, you will learn how to fill-in field data to PDF automatically in your C#.NET application. Following C# sample code can help you have a quick evaluation of it.

Not all PDF forms are fillable. Sometimes PDF form authors don't convert their PDF to interactive fillable forms. Or, they intentionally design a form that you can fill in only by hand. These non-interactive forms are called flat forms. These flat forms are not included in PDF specification. And they will not be supported in XDoc.PDF C# library.







How to fill in PDF forms using C#.NET?


To fill in a PDF form field data, you need locate and find the field on the PDF document. You can either find the field through page position or by field name.

The following C# source code will show how to locate a AcroForm field by page position.



String inputFilePath = Program.RootPath + "\\" + "1_AF.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";

List<BaseFormField> fields = PDFFormHandler.GetFormFields(inputFilePath);

int cnt = 0;
foreach (BaseFormField field in fields)
{
    float x = field.Position.X + field.Size.Width / 2F;
    float y = field.Position.Y + field.Size.Height / 2F;

    if (field is AFCheckBox)
    {   //  fill a CheckBox field, set state to ON
        AFCheckBoxInput input = new AFCheckBoxInput(true);
        PDFFormHandler.FillFormField(inputFilePath, 0, new PointF(x, y), input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFRadioButton)
    {   //  fill a RadioButton field, set state to ON 
        AFRadioButtonInput input = new AFRadioButtonInput(true);
        PDFFormHandler.FillFormField(inputFilePath, 0, new PointF(x, y), input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFTextBox)
    {   //  fill a TextBox field, change content to "Hello World"
        AFTextBoxInput input = new AFTextBoxInput("Hello World");
        PDFFormHandler.FillFormField(inputFilePath, 0, new PointF(x, y), input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFListBox)
    {   //  fill a ListBox field, selete the 3rd item (with index value 2)
        AFListBoxInput input = new AFListBoxInput(new int[1] { 2 });
        PDFFormHandler.FillFormField(inputFilePath, 0, new PointF(x, y), input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFComboBox)
    {   //  fill a BomboBox field, selete the 3rd item (with index value 2)
        AFComboBoxInput input = new AFComboBoxInput(2);
        PDFFormHandler.FillFormField(inputFilePath, 0, new PointF(x, y), input, outputFilePath + cnt.ToString() + ".pdf");
    }

    cnt++;
}


Here is the C# example source code which will explain how to locate a AcroForm field by its name.



String inputFilePath = Program.RootPath + "\\" + "1_AF.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";

List<BaseFormField> fields = PDFFormHandler.GetFormFields(inputFilePath);

int cnt = 0;
foreach (BaseFormField field in fields)
{
    if (field is AFCheckBox)
    {   //  fill a CheckBox field, set state to ON
        AFCheckBoxInput input = new AFCheckBoxInput(true);
        PDFFormHandler.FillFormField(inputFilePath, field.Name, input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFRadioButton)
    {   //  fill a RadioButton field, set state to ON 
        AFRadioButtonInput input = new AFRadioButtonInput(true);
        PDFFormHandler.FillFormField(inputFilePath, field.Name, input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFTextBox)
    {   //  fill a TextBox field, change content to "Hello World"
        AFTextBoxInput input = new AFTextBoxInput("Hello World!");
        PDFFormHandler.FillFormField(inputFilePath, field.Name, input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFListBox)
    {   //  fill a ListBox field, selete the 3rd item (with index value 2)
        AFListBoxInput input = new AFListBoxInput(new int[1] { 2 });
        PDFFormHandler.FillFormField(inputFilePath, field.Name, input, outputFilePath + cnt.ToString() + ".pdf");
    }
    else if (field is AFComboBox)
    {   //  fill a BomboBox field, selete the 3rd item (with index value 2)
        AFComboBoxInput input = new AFComboBoxInput(2);
        PDFFormHandler.FillFormField(inputFilePath, field.Name, input, outputFilePath + cnt.ToString() + ".pdf");
    }

    cnt++;
}




How to fill in form fields data in a PDF document object, Stream object, or byte array using C#


You can fill in PDF form data on a PDFDocument object directly. And you can construct the PDFDocument object from an existing PDF file, Stream object, or byte array.



String inputFilePath = Program.RootPath + "\\" + "1_AF.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);

List<BaseFormField> fields = PDFFormHandler.GetFormFields(doc);

int cnt = 0;
foreach (BaseFormField field in fields)
{
    if (field is AFCheckBox)
    {   //  fill a CheckBox field, set state to ON
        AFCheckBoxInput input = new AFCheckBoxInput(true);
        PDFFormHandler.FillFormField(doc, field.Name, input);
    }
    else if (field is AFRadioButton)
    {   //  fill a RadioButton field, set state to ON 
        AFRadioButtonInput input = new AFRadioButtonInput(true);
        PDFFormHandler.FillFormField(doc, field.Name, input);
    }
    else if (field is AFTextBox)
    {   //  fill a TextBox field, change content to "Hello World"
        AFTextBoxInput input = new AFTextBoxInput("Hello World!");
        PDFFormHandler.FillFormField(doc, field.Name, input);
    }
    else if (field is AFListBox)
    {   //  fill a ListBox field, selete the 3rd item (with index value 2)
        AFListBoxInput input = new AFListBoxInput(new int[1] { 2 });
        PDFFormHandler.FillFormField(doc, field.Name, input);
    }
    else if (field is AFComboBox)
    {   //  fill a BomboBox field, selete the 3rd item (with index value 2)
        AFComboBoxInput input = new AFComboBoxInput(2);
        PDFFormHandler.FillFormField(doc, field.Name, input);
    }

    cnt++;
}

doc.Save(outputFilePath);








Common Asked Questions

How do you fill out a fillable PDF form?

You can manually fill in each form fields in PDF reader application. Or import data from text file or Excel file to PDF form fields using Acrobat or other PDF editor software on Windows or MacOS. Using RasterEdge XDoc.PDF C# library, you can automatically fill in large number of data into multiple PDF documents using C# codes in ASP.NET, WinForms applications.

Why can't I fill in form fields in PDF?

You need check the PDF form fields and make sure the fields are set to "Allowed". If they are not allowed, you need PDF owner permission to change it. Using C# PDF library, you can apply the fields permission and auto-fill in form fields data in C# class.

How to auto fill data in a PDF file?

Some PDF editors support automatically fill in large number of data into PDF form fields. Using C# PDF library, you can quickly import large number of data from data source and fill them into PDF form fields in multiple PDF files in C# application.

How do I import data into a fillable PDF?

You can import data into PDF form fields using PDF editor software, such as Acrobat, Foxit PDF. Using C# PDF library, you can batch import data into PDF form fields using C# in ASP.NET Core, MVC, Web Forms, WinForms applications.