How to Start Convert PDF Read PDF Edit PDF PDF Report Builder 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 AcroForm Library
How to open, create, fill-in Adobe PDF file form data. 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
  • Fill in field data

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.







C# fill a field data in the page


By 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++;
}


By 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++;
}




C# fill fields data in a document object


If you want to combine more than two pdf files together, you can try the following C# source code.



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);