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#]

using System;
using System.Data;
using Aspose.Pdf;

class Program
{
    static void Main()
    {
        // Create a DataTable
        DataTable dt = new DataTable("Employee");
        dt.Columns.Add("Employee_ID", typeof(int));
        dt.Columns.Add("Employee_Name", typeof(string));
        dt.Columns.Add("Gender", typeof(string));

        // Add rows to the DataTable
        dt.Rows.Add(1, "John Smith", "Male");
        dt.Rows.Add(2, "Mary Miller", "Female");

        // Instantiate a PDF document
        Pdf pdf1 = new Pdf();
        Section sec1 = pdf1.Sections.Add();

        // Create a table
        Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
        sec1.Paragraphs.Add(tab1);
        tab1.ColumnWidths = "80 100 100";
        tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
        tab1.DefaultCellTextInfo.Alignment = AlignmentType.Center;

        // Import DataTable into the PDF table
        tab1.ImportDataTable(dt, true, 0, 0, 3, 3);

        // Set header row background color
        Row headerRow = tab1.Rows[0];
        foreach (Cell cell in headerRow.Cells)
        {
            cell.BackgroundColor = Aspose.Pdf.Color.Pink;
        }

        // Add CheckBox headers
        headerRow.Cells.Add("CheckBox1").BackgroundColor = Aspose.Pdf.Color.Yellow;
        headerRow.Cells[3].FitWidth = 80F;
        headerRow.Cells.Add("CheckBox2").BackgroundColor = Aspose.Pdf.Color.Yellow;
        headerRow.Cells[4].FitWidth = 80F;

        // Add CheckBox fields to rows
        for (int counter = 1; counter < tab1.Rows.Count; counter++)
        {
            // First checkbox
            Cell firstCheckBoxCell = new Cell(tab1.Rows[counter]);
            tab1.Rows[counter].Cells.Add(firstCheckBoxCell);

            FormField firstCheckBox = new FormField
            {
                FieldName = "First_CheckBox" + counter,
                FormFieldType = FormFieldType.CheckBox,
                FormWidth = 80F
            };
            firstCheckBoxCell.Paragraphs.Add(firstCheckBox);

            // Second checkbox
            Cell secondCheckBoxCell = new Cell(tab1.Rows[counter]);
            tab1.Rows[counter].Cells.Add(secondCheckBoxCell);

            FormField secondCheckBox = new FormField
            {
                FieldName = "Second_CheckBox" + counter,
                FormFieldType = FormFieldType.CheckBox,
                FormWidth = 80F
            };
            secondCheckBoxCell.Paragraphs.Add(secondCheckBox);
        }

        // Save the PDF
        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