How to set text beside a checkbox control

I have a checkbox which is created using FormField as below:

Pdf pdf1 = new Pdf();

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.FormField checkBox = new Aspose.Pdf.FormField();

checkBox.FormFieldType = FormFieldType.CheckBox;

checkBox.FieldName = "Checkbox1";

checkBox.CheckBoxIsChecked = true;

checkBox.FormHeight = 20;

checkBox.FormWidth = 20;

sec1.Paragraphs.Add(checkBox);

I want to a text beside the checkbox to denote what the textbox is about.

How can I add text at the right of the checkbox juist after it.

Thanks

Hi Saikat,

Thanks for contacting support.

In order to accomplish this requirement, please try using the InlineParagraph property of the Segment object. Please try using the following code snippet to fulfill your requirements.

For your reference, I have also attached the resultant PDF file which is generated on my end.

C#

Pdf pdf1 = new Pdf();
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

// create a text object
Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text();

// Add text object to paragraphs collection of section
sec1.Paragraphs.Add(text1);

// Add sample text to segments collection of text object
text1.Segments.Add("CheckBox-1 ");

// Create segment 2 and add it to segements collection of text object
Aspose.Pdf.Generator.Segment seg2 = text1.Segments.Add();

Aspose.Pdf.Generator.FormField checkBox = new Aspose.Pdf.Generator.FormField();
checkBox.FormFieldType = FormFieldType.CheckBox;
checkBox.FieldName = "Checkbox1";
checkBox.CheckBoxIsChecked = true;
checkBox.FormHeight = 20;
checkBox.FormWidth = 20;

// add FormField as inline paragraph to segment object
seg2.InlineParagraph = checkBox;

pdf1.Save("c:/pdftest/FormField.pdf");

Hi,

I tried using the same codebase. I have a nested table with three Cells. Each cell should have a checkbox and text next to it. Although I can see the text getting displayed, the checkbox are not getting visible.Here is how I am using it:

Table tabChild = new Table(cellcheckBox1);

cellcheckBox1.Paragraphs.Add(tabChild);

tabChild.ColumnWidths = "60 60 60";

Row rowChild = tabChild.Rows.Add();

rowChild.DefaultRowCellPadding = new MarginInfo() { Right = 3F,Bottom=3F };

Cell chkAgree = rowChild.Cells.Add();

chkAgree.Alignment = AlignmentType.Left;

Text txtcellAccept = new Aspose.Pdf.Generator.Text();

txtcellAccept.TextInfo = tf1;

chkAgree.Paragraphs.Add(txtcellAccept);

txtcellAccept.Segments.Add("Agree");

Aspose.Pdf.Generator.Segment seg2 = txtcellAccept.Segments.Add();

FormField chk1 = new FormField();

chk1.FormFieldType = FormFieldType.CheckBox;

chk1.FieldName = "Agree";

chk1.FormHeight = 10;

chk1.FormWidth = 10;

seg2.InlineParagraph = chk1;

And the same way I have the other two checkboxes in the same row.

Please let me know if I am missing something.

Thanks,

If I use it wothout the table structure, the checkbox appears but the text is before the checkbox whereas I need it after the text box. Is it possible by any other means.

Saikat

Hi Saikat,

In order to display CheckBox first in table cell, first you need to add CheckBox as InLineParagraph to Segment object and then you can add the text segment to segments collection of Text object. Furthermore in order to make CheckBox visible, you may enable/set the border for CheckBox using IsBordered property of FormField. Please take a look over the following code snippet and have a look over the attached resultant PDF which is generated over my end.

C#

Pbf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();

// Create a table
Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();

// Add the table into the paragraphs collection of section
sec1.Paragraphs.Add(tab1);

// Set the column widths of the table
tab1.ColumnWidths = "100 200";

// Set the default cell border using BorderInfo instance
tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All);

// Add a row into the table
Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add();

// Add 1st cell in the row
row1.Cells.Add("left cell");

// Add 2nd cell in the row
Aspose.Pdf.Generator.Cell cell2 = row1.Cells.Add();
Aspose.Pdf.Generator.Table tabChild = new Aspose.Pdf.Generator.Table(cell2);
cell2.Paragraphs.Add(tabChild);
tabChild.ColumnWidths = "60 60 60";

Aspose.Pdf.Generator.Row rowChild = tabChild.Rows.Add();
rowChild.DefaultRowCellPadding = new Aspose.Pdf.Generator.MarginInfo
{
    Left = 3F, Right = 3F, Bottom = 3F
};

Aspose.Pdf.Generator.Cell chkAgree = rowChild.Cells.Add();

// Text textcellAccept = new Aspose.Pdf.Generator.Text();
chkAgree.Paragraphs.Add(txtcellAccept);

Aspose.Pdf.Generator.Segment seg2 = txtcellAccept.Segments.Add();
FormField chk1 = new FormField();
chk1.FormFieldType = FormFieldType.CheckBox;
chk1.FieldName = "Agree";
chk1.FormHeight = 10;
chk1.FormWidth = 10;
chk1.IsBordered = true;
chk1.Margin.Left = 5;
seg2.InlineParagraph = chk1;
txtcellAccept.Segments.Add(" Agree");

// Save Pdf
pdf1.Save("c:/pdftest/Table_Cell_With_CheckBox.pdf");

Thanks for the code snippet. Now the problem is, altough I am able to set the text right of the checkbox, there is absolutely no space between the checkbox and the text. Is there a way I can maintain some space between the text and the checkbox.

Hi Saikat,


Thanks for your inquiry. You can add blank spaces to maintain space between check box and text. As Nayyer included in his last reply. Hopefully it will serve the purpose.

txtcellAccept.Segments.Add(" Agree");

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the help. It works. However the text beside the checkbox is getting bottom aligned. Is there a way we can align the text centally.

Thanks,

Saikat

Hi Saikat

In order to display the text center aligned, please set the Vertical alignment property of table cell as Center aligned.

// set the vertical alignment for contents of table cell as Center Aligned
chkAgree.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Center;

The above suggested solution for vertical alignment of cell is not working, as we are adding the text in segment not directly in cell.. below is the code snippet for your review.. Text "Agree" is coming in bottom next to checkbox. Please refer the attached snapshot. Please provide any workable solution.

code snippet :

Cell cell1 = rowNo.Cells.Add();
cell1 .Alignment = AlignmentType.Left;
Text txt1 = new Aspose.Pdf.Generator.Text();
cell1.Paragraphs.Add(txt1);
cell1.VerticalAlignment = VerticalAlignmentType.Center; // As you suggested

Aspose.Pdf.Generator.Segment seg1 = txt1.Segments.Add();
FormField chkColonoscopy = new FormField();
chk1.FormFieldType = FormFieldType.CheckBox;
chk1.FieldName = "chk1";
chk1.FormHeight = 10;
chk1.FormWidth = 10;
chk1.IsBordered = true;
seg1.InlineParagraph = chk1;
txt1.Segments.Add(" Agree");

Hi Saikat,


Thanks for your inquiry. I’m afraid, I’m unable to notice alignment issue. Can you please share your sample PDF output and code snippet here? so we will look into it and will provide you more information accordingly.

Sorry for the inconvenience faced.

Best Regards,

Hi,

Thanks for your reply.

Code is same as mentioned above. I am attaching one snapshot for you.. If you take a close look to it, you will find that text alignment is not vertical center and it is appering in bottom next to checkbox.

Please let us know if you still not able to find the issue.

Hi Saikat,


I have tested the scenario using Aspose.Pdf for .NET 7.9.0 while using the code snippet shared against 455216 and as per my observations, the text besides checkbox is vertically center aligned. For your reference, I have also attached the resultant PDF which is generated over my end.

  • Can you please share which version of Aspose.Pdf for .NET you are using ?
  • Which operating system you are using ?
  • Which version of .NET Framework you are using ?
  • Which version of Visual Studio you are using ?

We apologize for your inconvenience.

Hi,

Thanks for your reply. It works fine for me now. I have requirement to check the checkbox based on boolean value from database.

Can you please advise me with code , how can we check the checkboxes in PDF.

Thanks in advance.

Hi Saikat,

In case you need to have a checkbox enabled when adding it to PDF document, please try using the following code line.

checkBox.CheckBoxIsChecked = true;

However if you need to enable/check a checkbox in existing PDF file, please try using the code snippet shared over Fill Form Fields in an Existing PDF File (Facades)

In the event of any further query, please feel free to contact.

Hi,


How can i add a check box that has FieldName in Arabic? I tried placing “Agree” with some Arabic word and it is throwing exception.

Kind Regards
Naveed Anjum

Hi Naveed,


Thanks for contacting support.

Please share the code snippet which you are using, so that we can test the scenario in our environment. Also please confirm if the error is occurring for all the documents or its appearing for certain files. We are sorry for this inconvenience.

Hi Shahbaz,


I am using below code to generate the PDF. It is not throwing exception but it is also not adding Arabic text.

Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();
//Create a table
Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();
//Add the table into the paragraphs collection of section
sec1.Paragraphs.Add(tab1);
//Set the column widths of the table
tab1.ColumnWidths = “100 200”;
//Set the default cell border using BorderInfo instance
tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All);
//Add a row into the table
Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add();
//Add 1st cell in the row
row1.Cells.Add(“left cell”);
//Add 2nd cell in the row
Aspose.Pdf.Generator.Cell cell2 = row1.Cells.Add();

Aspose.Pdf.Generator.Table tabChild = new Aspose.Pdf.Generator.Table(cell2);
cell2.Paragraphs.Add(tabChild);
tabChild.ColumnWidths = “60 60 60”;

Aspose.Pdf.Generator.Row rowChild = tabChild.Rows.Add();
rowChild.DefaultRowCellPadding = new Aspose.Pdf.Generator.MarginInfo() { Left = 3F, Right = 3F, Bottom = 3F };

Aspose.Pdf.Generator.Cell chkAgree = rowChild.Cells.Add();
// chkAgree.Alignment = Aspose.Pdf.Generator.AlignmentType.Left;
Text txtcellAccept = new Aspose.Pdf.Generator.Text();
chkAgree.Paragraphs.Add(txtcellAccept);

Aspose.Pdf.Generator.Segment seg2 = txtcellAccept.Segments.Add();
FormField chk1 = new FormField();
chk1.FormFieldType = FormFieldType.CheckBox;
chk1.FieldName = “ذكر”;
chk1.FormHeight = 10;
chk1.FormWidth = 10;
// show/enable the border of CheckBox
chk1.IsBordered = true;
chk1.Margin.Left = 5;
seg2.InlineParagraph = chk1;
txtcellAccept.Segments.Add(" ذكر");

pdf1.Save(“D:/AsposeTest/CheckboxField.pdf”);

Hi Naveed,

Thanks for sharing your source code. Please note that it is recommended to use the new generator (Aspose.PDF) as it is more efficient and improved. It can be used for both creating a new PDF from scratch and editing existing PDF documents. Please check the following code snippet to add a checkbox field with Arabic text; hopefully, it will help you to accomplish the task.

Document pdf1 = new Document();
Aspose.PDF.Page sec1 = pdf1.Pages.Add();

// Create a table
Aspose.PDF.Table tab1 = new Aspose.PDF.Table();

// Add the table into the paragraphs collection of section
sec1.Paragraphs.Add(tab1);

// Set the column widths of the table
tab1.ColumnWidths = "100 200";

// Set the default cell border using BorderInfo instance
tab1.DefaultCellBorder = new Aspose.PDF.BorderInfo(Aspose.PDF.BorderSide.All);

// Add a row into the table
Aspose.PDF.Row row1 = tab1.Rows.Add();

// Add 1st cell in the row
row1.Cells.Add("left cell");

// Add 2nd cell in the row
Aspose.PDF.Cell cell2 = row1.Cells.Add();

Aspose.PDF.Table tabChild = new Aspose.PDF.Table();
cell2.Paragraphs.Add(tabChild);
tabChild.ColumnWidths = "15 60 60";

Aspose.PDF.Row rowChild = tabChild.Rows.Add();
rowChild.DefaultCellPadding = new Aspose.PDF.MarginInfo
{ Left = 3F, Right = 3F, Bottom = 3F, Top = 3F };

Aspose.PDF.Cell chkAgree = rowChild.Cells.Add();
CheckboxField chk1 = new CheckboxField(pdf1);
chk1.PartialName = "ذكر";
chk1.Height = 10;
chk1.Width = 10;
chkAgree.Paragraphs.Add(chk1);

Aspose.PDF.Cell chkAgreetext = rowChild.Cells.Add();
chkAgreetext.DefaultCellTextState.HorizontalAlignment = HorizontalAlignment.Left;
TextFragment txtcellAccept = new Aspose.PDF.Text.TextFragment();
chkAgreetext.Paragraphs.Add(txtcellAccept);
TextSegment segment = new TextSegment("ذكر");
txtcellAccept.Segments.Add(segment);

pdf1.Save("E:/Data/CheckboxField.pdf");

Please feel free to contact us for any further assistance.

Best Regards,