Add Checkbox in PDF document using C# with Aspose.PDF for .NET

Hi , I am using datatable to export to pdf.

after exporting the datatable I have to show checkboxes for the values in the last 2 columns of the table. I did check below URL for the same but it dint help much.

http://www.aspose.com/community/forums/77459/how-to-put-checkbox-in-pdf-file/showthread.aspx#77459

any help on this will be much appreciated.

Hello Krunal,

Thanks for using our products.

You can add CheckBox in paragraphs collection of table cell. As per your requirements, once the data has been properly imported from DataTable, you can add separate cells to table object and add CheckBox form field to these columns. Please take a look over the following code snippet in which first I have imported the data from DataTable and then I have added two columns manually and have added the separate check boxes inside them. I have also attached the resultant PDF that I have generated using Aspose.Pdf for .NET 5.0.0 over Windows XP SP3 using Visual Studio 2005 with .NET Framework 2.0

[C#]

DataTable dt = new DataTable("Employee");
dt.Columns.Add("Employee_ID", typeof(Int32));
dt.Columns.Add("Employee_Name", typeof(string));
dt.Columns.Add("Gender", typeof(string));

//Add 2 rows into the DataTable object programmatically
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = "John Smith";
dr[2] = "Male";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 2;
dr[1] = "Mary Miller";
dr[2] = "Female";
dt.Rows.Add(dr);

//Instantiate a Pdf instance
Pdf pdf1 = new Pdf();
//Create a section in the Pdf instance
Section sec1 = pdf1.Sections.Add();

//Create a Table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
//Add the Table object in the paragraphs collection of the section
sec1.Paragraphs.Add(tab1);
//Set column widths of the table
tab1.ColumnWidths = "80 100 100";
//Set default cell border of the table using BorderInfo object
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
// set the default text alignment information as center aligned
tab1.DefaultCellTextInfo.Alignment = AlignmentType.Center;

//Import data into the Table object from the DataTable created above
tab1.ImportDataTable(dt, true, 0,0, 3,3);
//Get 1st row from the table
Row Header_Row = tab1.Rows[0];

//Iterate through all cells in the row and set their background color to blue
foreach (Cell curCell in Header_Row.Cells)
curCell.BackgroundColor = new Aspose.Pdf.Color("Pink");

// add table cell to display title information for CheckBox column
Header_Row.Cells.Add("CheckBox1");
// set background color information for table cell
Header_Row.Cells[3].BackgroundColor = new Aspose.Pdf.Color("Yellow");
// specify the fix width information for table cell
Header_Row.Cells[3].FitWidth = 80F;

// add a new table cell with text information
Header_Row.Cells.Add("CheckBox2");
// set background color information for table cell
Header_Row.Cells[4].BackgroundColor = new Aspose.Pdf.Color("Yellow");
Header_Row.Cells[4].FitWidth = 80F;

// Now we need to add 2 cells to table object, that will contain CheckBox
for (int counter = 1; counter < tab1.Rows.Count; counter++)
{
// create a cell object over second row
Cell First_CheckBoxCell = new Cell(tab1.Rows[counter]);
// add the table cell to cells collection at row 1
tab1.Rows[counter].Cells.Add(First_CheckBoxCell);
// create a form field object
FormField First_CheckBox = new FormField();
// specify the name for form field object
First_CheckBox.FieldName = "First_CheckBox" + counter.ToString();
// set the form field object type as CheckBox
First_CheckBox.FormFieldType = FormFieldType.CheckBox;
// set the form field widht information
First_CheckBox.FormWidth = 80F;
// add form field to paragraphs collection of table cell
First_CheckBoxCell.Paragraphs.Add(First_CheckBox);

Cell Second_CheckBoxCell = new Cell(tab1.Rows[1]);
tab1.Rows[counter].Cells.Add(Second_CheckBoxCell);
FormField Second_CheckBox = new FormField();
Second_CheckBox.FieldName = "Second_CheckBox" + counter.ToString();
Second_CheckBox.FormFieldType = FormFieldType.CheckBox;
Second_CheckBox.FormWidth = 80F;
Second_CheckBoxCell.Paragraphs.Add(Second_CheckBox);
}

pdf1.Save(@"d:/pdftest/DataTable_CheckBox.pdf");

In case I have not properly understood your requirements or you have any further query, please feel free to contact. I would also recommend you to visit the following links for better understanding on