Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sun May 27, 2018 1:35 pm

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.

chintalpatel89
 
Posts: 16
Joined: Sun Dec 25, 2016 9:54 am

Mon May 28, 2018 3:42 am

Hello,

Thanks for your post.
Kindly note the following points.
1. I noticed that you set the "checkbox1.Checked = true;", while "checkbox2.Checked = false". If you want the checkbox2 to be checked, please also set the property "Checked" to be true.
2. Flattening a field has nothing to do with the checking status of the field, thus you don't need to flatten the filed, just comment out the statement.
3. The following code works well with the latets version(Spire.PDF Pack(Hot Fix) Version:4.5.3). Attched is the result with both textboxes checked.
Code: Select all
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 = "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 = true;
document.Form.Fields.Add(checkbox2);

document.SaveToFile("13956.pdf",FileFormat.PDF);



Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue May 29, 2018 8:34 am

Hello,

How is the issue now?
Your feedback would be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF