Add checkbox for each paragraph

Hi all,

I do appreciate if someone help me how I can add a check box next to each paragraph in my document?

Thanks in advance,
Marzie

Hi Marzie,

Thanks for your inquiry. In this case, you need to move the cursor to the end of paragraph and insert the check box. Please use following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection nodes = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph para : (Iterable)nodes)
{
    builder.moveToParagraph(nodes.indexOf(para), -1);
    builder.insertCheckBox("Some text", true, 0);
}
doc.save(MyDir + "Out.docx");

Dear Tahir,

Thanks a lot for your response. Yes I used yesterday the same code and it worked. But I have another question: is it possible to add the “checkbox” for each paragraph on the left side inside the margin area? I mean outside the content area?

Thanks on advance,
Marzie

Hi Marzie,

Thanks for your inquiry. Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Dear Tahir,

Thanks for your reply.
I have attached a sample
document. The question is that how to position the “checkBox” next to
each paragraph in the margin area of the page?

Thanks in advance,
Marzie

Hi Marzie,

Thanks for sharing the detail. Please use following code example to achieve your requirements. Hope this helps you. I have attached the input and output documents with this post for your kind reference.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
int i = 1;
for (Node paragraph : doc.getChildNodes(NodeType.PARAGRAPH, true).toArray())
{
    Shape shape = new Shape(doc, ShapeType.TEXT_BOX);
    shape.setWidth(40);
    shape.setHeight(40);
    shape.setWrapType(WrapType.SQUARE);
    shape.setLeft(-50);
    shape.setTop(40);
    shape.setStroked(false);
    shape.appendChild(new Paragraph(doc));
    ((Paragraph)paragraph).appendChild(shape);
    builder.moveTo(shape.getFirstParagraph());
    builder.insertCheckBox("checkbox" + i, false, 0);
    i++;
}
doc.save(MyDir + "Out.docx");

Dear Tahir,

Thanks a lot for your response.
So, I have to use shape node in order to get this requirement. The question is that can I use “checkbox” (builder.insertCheckBox()) and then set the alignment for that?
I mean the problem is that I have to use “doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS, “password”)” to protect the document but only form fields can be editable. But by using this code the shape which is of type “checkbox” is not editable by this code. and the users cannot mark it or unmark it. What can I do for this problem?

Thanks in advance,
Marzie

Hi Marzie,

Thanks for your inquiry.
FormField class represents a single form field. Microsoft Word provides the following form fields: check box, text input and dropdown (combobox).

FormField is an inline-node and can only be a child of Paragraph. In your case, I suggest you please insert the contents of your document into table which have two columns. In first column, insert the check boxes and in second columns insert the contents. Hope this helps you.

Dear Tahir,

Thanks a lot for your response. It really helped. I do appreciate.

Best regards,
Marzie

Hi Marzie,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.