Support of setting content control (checkbox) checked/unchecked Symbol using Java

Hello,

using aspose library (20.8) I would like to change the checked/unchecked symbol in Check Box according to my preferences, is there such an option now?

Example code which I use

        documentBuilder.startTable();
        final Cell cell = documentBuilder.insertCell();
        StructuredDocumentTag checkBoxSDT = new StructuredDocumentTag(documentBuilder.getDocument(), SdtType.CHECKBOX, MarkupLevel.BLOCK);

     // checkBoxSDT.setPlaceholderName("Check box name"); // TODO: error
        checkBoxSDT.setColor(new Color(24,23,123));  // set hovered element border
        checkBoxSDT.setTitle("Some custom title");
     // checkBoxSDT.setCustomSymbol(....?)
        cell.appendChild(checkBoxSDT)
        documentBuilder.endTable();
        saveDocument(documentBuilder);

Also i want to mention that when I want to set the placeholder name I get the error:

java.lang.IllegalStateException: BuildingBlock with such Name does not exist in the document glossary.

– small update:
Also, I would like to get rid of the case where after adding a CheckBox it adds break lines in a paragraph (see attachment)
table_test_word_15_13_06.zip (17.4 KB)

@aolo23

You can remove the empty paragraph before the content control using following code example.

DocumentBuilder documentBuilder = new DocumentBuilder();

documentBuilder.startTable();
final Cell cell = documentBuilder.insertCell();
StructuredDocumentTag checkBoxSDT = new StructuredDocumentTag(documentBuilder.getDocument(), SdtType.CHECKBOX, MarkupLevel.BLOCK);

checkBoxSDT.setColor(new Color(24,23,123));  // set hovered element border
checkBoxSDT.setTitle("Some custom title");
cell.appendChild(checkBoxSDT);
documentBuilder.endTable();

cell.getFirstParagraph().remove();
documentBuilder.getDocument().save(MyDir + "out.docx");

You can use StructuredDocumentTag.Checked property to check/uncheck the checkbox.

The StructuredDocumentTag.PlaceholderName property is used to get or set the name of the BuildingBlock containing placeholder text. Note that BuildingBlock with this name Name has to be present in the GlossaryDocument otherwise InvalidOperationException will occur. Please check the code example shared here:
StructuredDocumentTag - Aspose.Words for Java - API Reference

When writing about changing the symbol, I was thinking about changing the definition of the symbol, not the selection state. Ms word provides the ability to change the symbol definition.
Please see symbols.png.
symbols.png (36.6 KB)

@aolo23

Please use DocumentBuilder.write method as shown below to insert the checkbox symbols into document. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.getFont().setName("Wingdings 2");

builder.write("\u0052");
builder.write("\u00A3");

doc.save(MyDir + "21.2.docx");
1 Like

Thanks for your reply but I didn’t necessarily want just insert / delete a symbol. The MsWord program has the Developer Tab with the “Controls” field. I choose the option “Check box content control”
Then when I select my checkbox I’m able to change the definition of checked/unchecked symbol.
The checkbox keep the ability to change the state after this edition

@aolo23

We suggest you please read the following article.
Working with Content Control SDT

To insert the checkbox (content control) , you can use the code example shared in my post here:

Could you please share what issue you are facing while using this code example? Please also share your input and expected output documents. We will then provide you code example according to your requirement.

I have read the proposed sources by you, but the class indicated does not provide methods that operate on objects in the way I would like.
In the attachment I enclose the expected result of the document and a photo showing the change of checkbox sign.checkbox_expected.zip (100.1 KB)

@aolo23

We have logged a new feature request as WORDSNET-21818 to set the checked and unchecked symbol of content control (checkbox). We will inform you via this forum thread once this feature is available. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-21818) have been fixed in this Aspose.Words for .NET 21.5 update and this Aspose.Words for Java 21.5 update.

1 Like

@tahir.manzoor
Please correct the Release Note because in its current state it is impossible to read them :slight_smile:

@aolo23

Thanks for pointing this issue. We will fix it soon.

        Document doc = new Document();
        StructuredDocumentTag checkBoxSDT = new StructuredDocumentTag(doc, SdtType.CHECKBOX, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(checkBoxSDT);
        checkBoxSDT.setCheckedSymbol(0xF0FE, "Wingdings");
        checkBoxSDT.setUncheckedSymbol(0x006F, "Wingdings");
        checkBoxSDT.setChecked(true);
        checkBoxSDT.setTitle("ssssss");
        doc.save("output.docx");

The new definition of a checkbox is applied only after you click on it for the first time.

@aolo23

We have logged this problem in our issue tracking system as WORDSNET-22241. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-22241) have been fixed in this Aspose.Words for .NET 21.6 update and this Aspose.Words for Java 21.6 update.