XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to HTML5 Viewer & Editor PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Field: Insert, Delete, Update Field
  |  
Home ›› XDoc.PDF ›› C# PDF: Insert, Delete, Update Field

C#.NET PDF SDK - Insert, Delete and Update Field in C#.NET


Online C# Tutorial to Insert, Delete and Update Fields in PDF Document with .NET PDF DLLs




A best C#.NET PDF document SDK library for PDF form field manipulation in Visual Studio .NET program


Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint


Evaluation components for inserting, deleting and updating PDF form in Visual C# .NET WinForms and ASP.NET web application


Free online C# source codes provide best ways to create PDF forms and delete PDF forms in C#.NET framework project


A professional PDF form creator supports to create fillable PDF form in C#.NET


An advanced PDF form maker allows users to create editable PDF form in C#.NET


Able to add text field to specified PDF file position in C#.NET class


Support to change font size in PDF form


Able to delete form fields from adobe PDF file


Field inserting, deletion and update is the most basic field edit function. By using RaterEdge .NET PDF package, you can add form fields to existing pdf files, delete or remove form field in PDF page and update PDF field in C# application. C# sample codes are provided below to help you finish these task.





C# add form fields to an existing PDF file


Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:

       

       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.

       

       If using x86, the platform target should be x86.




String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output.pdf";

List<BaseFormField> fields = new List<BaseFormField>();

//  add a radio button field with default setting
AFRadioButton field1 = new AFRadioButton("AF_RadioButton_01");
field1.PageIndex = 0;
field1.Position = new PointF(100F, 100F);
fields.Add(field1);

//  add a checkbox field with default setting
AFCheckBox field2 = new AFCheckBox("AF_CheckBox_01");
field2.PageIndex = 0;
field2.Position = new PointF(300F, 100F);
fields.Add(field2);

//  add a checkbox field with default setting
AFTextBox field3 = new AFTextBox("AF_TextBox_01");
field3.PageIndex = 0;
field3.Position = new PointF(100F, 300F);
fields.Add(field3);

//  add a list box field with default setting
AFListBox field4 = new AFListBox("AF_ListBox_01");
field4.PageIndex = 0;
field4.Position = new PointF(100F, 500F);
field4.Items = new String[4] { "Item 1", "Item 2", "Item 3", "Item 4" };
fields.Add(field4);

//  add a combo box field with default setting
AFComboBox field5 = new AFComboBox("AF_ComboBox_01");
field5.PageIndex = 0;
field5.Position = new PointF(300F, 500F);
field5.Items = new String[4] { "Item 1", "Item 2", "Item 3", "Item 4" };
fields.Add(field5);

//  add a button field with default setting
AFButton field6 = new AFButton("AF_Button_01");
field6.PageIndex = 0;
field6.Position = new PointF(100F, 700F);
fields.Add(field6);

//  add fields to the input file
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




C# retrieve all form fields from a PDF file





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

List<BaseFormField> fields = PDFFormHandler.GetFormFields(inputFilePath);
Console.WriteLine("Number of Fields: " + fields.Count);
if (fields.Count > 0)
{
    foreach (BaseFormField field in fields)
    {
        Console.WriteLine("Field");
        Console.WriteLine("  Name:     " + field.Name);
        Console.WriteLine("  Visible:  " + field.IsVisible);
        Console.WriteLine("  Page:     " + field.PageIndex);
        Console.WriteLine("  Position: " + field.Position.ToString());
        Console.WriteLine("  Size: " + field.Size.ToString());
    }
}




C# select a field in a page by position





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

//  select a field at position [110, 310] in page 1 (page index 0)
int pageIndex = 0;
PointF pos = new PointF(110, 310);
//  get the form field object
BaseFormField field = PDFFormHandler.GetFormField(inputFilePath, pageIndex, pos);
if (field != null)
{
    Console.WriteLine("Field " + field.Name + " in page " + field.PageIndex + " at " + field.Position.ToString());
}
else
{
    Console.WriteLine("Field " + field.Name + " does not exist");
}




C# select a field in a document by the name





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

//  select a field with name "AF_RadioButton_01"
String fieldName = @"AF_RadioButton_01";
//  get the form field object
BaseFormField field = PDFFormHandler.SelectFormField(inputFilePath, fieldName);
if (field != null)
{
    Console.WriteLine("Field " + field.Name + " in page " + field.PageIndex + " at " + field.Position.ToString());
}
else
{
    Console.WriteLine("Field " + field.Name + " does not exist");
}




C# delete a form field in the page





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

//  delete a field at position [110, 310] in page 1 (page index 0)
int pageIndex = 0;
PointF pos = new PointF(110, 310);
//  remove the field and output the new document
int errCode = PDFFormHandler.RemoveFormField(inputFilePath, pageIndex, pos, outputFilePath);
if (errCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}   



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

//  remove a field by name and output the new document
String fieldName = "AF_RadioButton_01";
int errCode = PDFFormHandler.RemoveFormField(inputFilePath, fieldName, outputFilePath);
if (errCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}




C# add a Radio Button Field to PDF document





String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output0.pdf";

List<BaseFormField> fields = new List<BaseFormField>();
//  create a Radio Button Field object
AFRadioButton tbField = new AFRadioButton("AF_RadioButton_01");
//  in first page (page index 0)
tbField.PageIndex = 0;
//  position of top left corner of the field: [100 pixels, 250 pixels] (in 96 dpi)
tbField.Position = new System.Drawing.PointF(100, 250);
//  size of the field: width 400 pixels, height 100 pixels (in 96 dpi)
tbField.Size = new System.Drawing.SizeF(400, 100);
//  set field visible
tbField.IsVisible = true;
//  set the label of the Radio Button field
tbField.Text = "Radio Button";
tbField.SetTextFont(PSType1Font.Helvetica_Oblique, 12);
tbField.TextColor = System.Drawing.Color.Black;
//  set the alignment of the radio button and label text in the field region
tbField.HorizontalAlignment = HorizontalAlignment.Left;
tbField.VerticalAlignment = VerticalAlignment.Center;

fields.Add(tbField); 

//  add fields
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




C# add a Check Box Field to PDF document





String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output1.pdf";

List<BaseFormField> fields = new List<BaseFormField>();

//  create a Check Box Field object
AFCheckBox tbField = new AFCheckBox("AF_CheckBox_01");
//  in first page (page index 0)
tbField.PageIndex = 0;
//  position of top left corner of the field: [150 pixels, 300 pixels] (in 96 dpi)
tbField.Position = new System.Drawing.PointF(150, 300);
//  size of the field: width 300 pixels, height 80 pixels (in 96 dpi)
tbField.Size = new System.Drawing.SizeF(300, 80);
//  set field visible
tbField.IsVisible = true;
//  set the label of the Check Box field
tbField.Text = "Check Box";
tbField.SetTextFont(PSType1Font.Helvetica, 16);
tbField.TextColor = System.Drawing.Color.Black;
//  set the alignment of the checkbox and label text in the field region
tbField.HorizontalAlignment = HorizontalAlignment.Center;
tbField.VerticalAlignment = VerticalAlignment.Center;

//  set the initial state of the CheckBox field to ON
tbField.IsChecked = true;

fields.Add(tbField);

//  add fields
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




C# add a Text Box Field to PDF document





String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output2.pdf";

List<BaseFormField> fields = new List<BaseFormField>();

//  create a Text Box Field object
AFTextBox tbField = new AFTextBox("AF_TextBox_01");
//  in first page (page index 0)
tbField.PageIndex = 0;
//  position of top left corner of the field: [150 pixels, 300 pixels] (in 96 dpi)
tbField.Position = new System.Drawing.PointF(150, 300);
//  size of the field: width 400 pixels, height 300 pixels (in 96 dpi)
tbField.Size = new System.Drawing.SizeF(400, 300);
//  set field visible
tbField.IsVisible = true;
//  background of the text box: Light Gray
tbField.BackgroundColor = System.Drawing.Color.LightGray;
//  initial content and font and font size of the content in the text box
tbField.Text = "";
tbField.SetTextFont(PSType1Font.Helvetica, 12);
tbField.TextColor = System.Drawing.Color.Black;
//  multi-line flag: true
tbField.IsMultiLine = true;
//  readonly flag: false
tbField.IsReadOnly = false;
//  set border of the field: 3 pixels in width, lightblue
tbField.BorderWidth = 3;
tbField.BorderColor = System.Drawing.Color.LightBlue;

fields.Add(tbField);

//  add fields
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




C# add a List Box Field to PDF document





String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output3.pdf";

List<BaseFormField> fields = new List<BaseFormField>();

//  create a List Box Field object
AFListBox tbField = new AFListBox("AF_ListBox_01");
//  in first page (page index 0)
tbField.PageIndex = 0;
//  position of top left corner of the field: [150 pixels, 300 pixels] (in 96 dpi)
tbField.Position = new System.Drawing.PointF(150, 300);
//  size of the field: width 150 pixels, height 60 pixels (in 96 dpi)
tbField.Size = new System.Drawing.SizeF(150, 60);
//  set field visible
tbField.IsVisible = true;
//  set option items in the list box
tbField.Items = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };
//  set properties of the item text
tbField.SetTextFont(PSType1Font.Helvetica, 12);
tbField.TextColor = System.Drawing.Color.Black;
//  set border of the field: 2 pixels in width, black
tbField.BorderWidth = 2;
tbField.BorderColor = System.Drawing.Color.Black;
//  multi-selection flag: false
tbField.IsMultiSelect = false;

fields.Add(tbField);

//  add fields
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




C# add a Combo Box Field to PDF document





String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output4.pdf";

List<BaseFormField> fields = new List<BaseFormField>();

//  create a Combo Box Field object
AFComboBox tbField = new AFComboBox("AF_ComboBox_01");
//  in first page (page index 0)
tbField.PageIndex = 0;
//  position of top left corner of the field: [150 pixels, 300 pixels] (in 96 dpi)
tbField.Position = new System.Drawing.PointF(150, 300);
//  size of the field: width 150 pixels, height 30 pixels (in 96 dpi)
tbField.Size = new System.Drawing.SizeF(150, 30);
//  set field visible
tbField.IsVisible = true;
//  set field background color: lightgray
tbField.BackgroundColor = System.Drawing.Color.LightGray;
//  set option items in the combo box
tbField.Items = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };
//  set properties of the item text
tbField.SetTextFont(PSType1Font.Helvetica, 12);
tbField.TextColor = System.Drawing.Color.Black;
//  set border of the field: 2 pixels in width, black
tbField.BorderWidth = 2;
tbField.BorderColor = System.Drawing.Color.Black;

//  set inital selected index: 2 (the 3rd item)
tbField.SelectedIndex = 2;

fields.Add(tbField);

//  add fields
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




C# add a Button Field to PDF document





String inputFilePath = Program.RootPath + "\\" + "empty.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output5.pdf";

List<BaseFormField> fields = new List<BaseFormField>();

//  create a Push Button Field object
AFButton tbField = new AFButton("AF_Button_01");
//  in first page (page index 0)
tbField.PageIndex = 0;
//  position of top left corner of the field: [150 pixels, 300 pixels] (in 96 dpi)
tbField.Position = new System.Drawing.PointF(150, 300);
//  size of the field: width 120 pixels, height 30 pixels (in 96 dpi)
tbField.Size = new System.Drawing.SizeF(120, 30);
//  set field visible
tbField.IsVisible = true;
//  set field background color: gray
tbField.BackgroundColor = System.Drawing.Color.Gray;
//  set label in the push button
tbField.Text = "CLICK";
tbField.SetTextFont(PSType1Font.Helvetica, 16);
tbField.TextColor = System.Drawing.Color.Black;
//  set border of the field: 1 pixel in width, drakgray
tbField.BorderColor = System.Drawing.Color.DarkGray;
tbField.BorderWidth = 1;

fields.Add(tbField);

//  add fields
PDFFormHandler.AddFormFields(inputFilePath, fields, outputFilePath);




.NET Class Namespace Required



In order to run the sample code, the following steps would be necessary.


Add necessary references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XImage.Raster.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XDoc.PDF.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Basic;


  using RasterEdge.XDoc.PDF;