How to Add Checkbox Component in PDF using C#

In this article, we will discuss to add checkbox component in PDF using C#. Additionally, it teaches you how to use annotation library and create a straightforward C# application to insert checkbox in PDF using C#. Checkboxes are useful for creating interactive PDF forms. They allow users to select one or more options by clicking on a box, making it easy to gather data and information. Let’s check all steps of adding checkbox in PDF with a sample code example.

Steps to Add Checkbox Component in PDF using C#

  1. Setup GroupDocs.Annotation for .NET package from the NuGet
  2. Add reference to the GroupDocs.Annotation namespace to create checkbox in PDF
  3. Create an Annotator object with input PDF path
  4. Initialize CheckboxComponent object and set some properties
  5. Call Add method of Annotator class and pass checkbox Component object to it
  6. Call Save method of Annotator class with resultant PDF path

The preceding process can be used on any platform where.NET is installed to create checkbox in PDF using C# without having to install any additional tools or software. There are no extra steps to follow on any common operating system like Windows, Linux or macOS to insert checkbox in PDF. Below code example explains how to initialize Annotator and CheckboxComponent objects, set properties for checkbox and save the PDF document to disk.

Code to Add Checkbox Component in PDF using C#

using GroupDocs.Annotation;
using GroupDocs.Annotation.Models;
using GroupDocs.Annotation.Models.FormatSpecificComponents.Pdf;
using System;
using System.Collections.Generic;
namespace AddCheckBoxComponentInPDFusingCSharp
{
internal class Program
{
static void Main(string[] args)
{
// Set License to avoid the limitations of Annotation library
License lic = new License();
lic.SetLicense(@"Conholdate.Annotator.lic");
// Create Annotator object with input PDF path
using (Annotator annotator = new Annotator("input.pdf"))
{
CheckBoxComponent checkbox = new CheckBoxComponent
{
Checked = true,
Box = new Rectangle(100, 100, 100, 100),
PenColor = 65535,
Style = BoxStyle.Star,
Replies = new List<Reply>
{
new Reply
{
Comment = "First comment",
RepliedOn = DateTime.Now
},
new Reply
{
Comment = "Second comment",
RepliedOn = DateTime.Now
}
}
};
// Add checkbox to Annotator
annotator.Add(checkbox);
//Save the resultant PDF
annotator.Save("result.pdf");
}
}
}
}

The process to put checkbox in PDF using C# has been discussed in the above section with all required steps. You can set different properties of CheckboxComponent class according to your requirement and place the checkbox at desired page and position. The annotation library also allows you to insert dropdown and button components into PDF.

In conclusion, adding checkboxes to a PDF form can make it more user-friendly and efficient. An article on adding button to PDF was previously added; for additional details, see the instruction how to add button component to PDF using C#.

 English