Category / Section
How to add PDF form fields inside the grid cell
2 mins read
This article describes about to create pdf fields inside the grid cell. We can add the form fields inside grid with the help of PdfGrid BeginCellLayout event handler.
The following assemblies are needed to be add the reference of your application.
Syncfusion assemblies:
- Syncfusion.Pdf.Base.dll
- Syncfusion.Compression.Base.dll
The same has been explained in the following code example.
C#:
//Initial declaration PdfDocument document; PdfForm form; PdfPage tempPage; private void button1_Click(object sender, EventArgs e) { //Create a new PDF document. document = new PdfDocument(); //Add a page. PdfPage page = document.Pages.Add(); form = document.Form; //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); // Add column pdfGrid.Columns.Add(3); //Add the grid row for (int i = 0; i < 2; i++) { PdfGridRow row = pdfGrid.Rows.Add(); for (int j = 0; j < 3; j++) { if (i == 0 && j == 0) row.Cells[j].Value = "TextBox Field"; if (i == 0 && j == 1) row.Cells[j].Value = "CheckBox Field"; if (i == 0 && j == 2) row.Cells[j].Value = "ComboBox Field"; } //Sets row height row.Height = 10; } pdfGrid.BeginCellLayout += pdfGrid_BeginCellLayout; pdfGrid.BeginPageLayout += pdfGrid_BeginPageLayout; //Draw grid to the page of PDF document. pdfGrid.Draw(page, new PointF(10, 10)); //Save the document. document.Save("FormField.pdf"); //close the document document.Close(true); } //Begin Page Layout Event Handler private void pdfGrid_BeginPageLayout(object sender, BeginPageLayoutEventArgs e) { tempPage = e.Page; } //Raises the before cell layout. private void pdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { if (args.RowIndex == 1 && args.CellIndex == 0) { //Add textbox field PdfTextBoxField textbox = new PdfTextBoxField(tempPage, "Name"); textbox.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); textbox.Bounds = new RectangleF(args.Bounds.X , args.Bounds.Y, args.Bounds.Width, args.Bounds.Height); textbox.Text = "TextBox"; form.Fields.Add(textbox); } else if (args.RowIndex == 1 && args.CellIndex == 1) { //Add checkbox field PdfCheckBoxField checkField = new PdfCheckBoxField(tempPage, "UG"); checkField.Bounds = new RectangleF(args.Bounds.X +2, args.Bounds.Y, args.Bounds.Width / 2, args.Bounds.Height - 1); checkField.Checked = true; //Sets checkbox style checkField.Style = PdfCheckBoxStyle.Cross; form.Fields.Add(checkField); } else if(args.RowIndex == 1 && args.CellIndex == 2) { //Add combobox filed PdfComboBoxField comboBox = new PdfComboBoxField(tempPage, "JobTitle"); //Sets the combo box properties. comboBox.Bounds = new RectangleF(args.Bounds.X , args.Bounds.Y, args.Bounds.Width / 2, args.Bounds.Height - 1); comboBox.BorderWidth = 1; comboBox.BorderColor = new PdfColor(Color.Gray); //Sets tooltip. comboBox.ToolTip = "Job Title"; //Adds list items. comboBox.Items.Add(new PdfListFieldItem("Development", "accounts")); comboBox.Items.Add(new PdfListFieldItem("Support", "advertise")); comboBox.Items.Add(new PdfListFieldItem("Documentation", "agri")); form.Fields.Add(comboBox); } }
Sample:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/GridSample-1145017819.zip