PdfDocument document = new PdfDocument();
document.AllowCreateForm = (document.Form == null) ? true : false;
document.Pages.Add();
PdfPageBase page = document.Pages[0];
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f);
PdfBrush brush = PdfBrushes.Black;
float x = 10;
float y = 10;
float tempX = 0;
float tempY = 0;
////string text = "Textbox: ";
string text = "Checkbox: ";
page.Canvas.DrawString(text, font, brush, x, y);
tempX = font.MeasureString(text).Width + 15;
tempY = font.MeasureString(text).Height + 15;
PdfCheckBoxField checkbox1 = new PdfCheckBoxField(page, "Checkbox1");
checkbox1.Bounds = new RectangleF(0, 0, 50, 20);
checkbox1.BorderWidth = 2.75f;
checkbox1.BorderStyle = PdfBorderStyle.Underline;
checkbox1.Location = new PointF(35f, 50f);
checkbox1.Style = PdfCheckBoxStyle.Diamond;
checkbox1.Checked = true;
document.Form.Fields.Add(checkbox1);
PdfCheckBoxField checkbox2 = new PdfCheckBoxField(page, "Checkbox2");
checkbox2.Bounds = new RectangleF(50, 50, 50, 20);
checkbox2.BorderWidth = 2.75f;
checkbox2.BorderStyle = PdfBorderStyle.Underline;
checkbox2.Location = new PointF(500f, 500f);
checkbox2.Style = PdfCheckBoxStyle.Diamond;
checkbox2.Checked = false;
document.Form.Fields.Add(checkbox2);
//checkbox1.Flatten = true; ----> when this line is enabled then only checkbox1 is saved as checked in file created but the next line after this throws error as An unhandled exception of type 'System.NullReferenceException' occurred in Spire.Pdf.dll
Additional information: Object reference not set to an instance of an object.
document.SaveToFile(("C:/Files/") + "checkbox.pdf", FileFormat.PDF);
Can you please help me over here? Also, to save checkbox as checked do I need to flatten that field? If I dont it is not getting saved as checked. Let me know if I have done something wrong.