Articles in this section
Category / Section

How to add PDF form fields inside the grid cell

3 mins read

This article describes how to create PDF fields inside a grid cell. We can add form fields inside a grid with the help of the PdfGrid BeginCellLayout event handler.

The following assemblies need to be added as references to 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 columns
            pdfGrid.Columns.Add(3);
 
            // Add grid rows
            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";
                }
                // Set row height
                row.Height = 10;
            }
 
            pdfGrid.BeginCellLayout += pdfGrid_BeginCellLayout;
            pdfGrid.BeginPageLayout += pdfGrid_BeginPageLayout;
 
            // Draw the grid on the page of the 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;
        }
 
        // Raise the before cell layout event.
        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;
                // Set checkbox style
                checkField.Style = PdfCheckBoxStyle.Cross;
                form.Fields.Add(checkField);
            }
            else if (args.RowIndex == 1 && args.CellIndex == 2)
            {
                // Add combobox field
                PdfComboBoxField comboBox = new PdfComboBoxField(tempPage, "JobTitle"); // Set 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);
                // Set tooltip.
                comboBox.ToolTip = "Job Title";
                // Add 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

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion&reg; assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion&reg; license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about How to create Excel exploded pie chart in C#, VB.NET.

You can refer to our PDF feature tour page to learn about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied