Word how to set checkbox checked

I have two doubts about word,:

  1. Why I get cell text value is not corrent? The test case shows me that the first line is missing.

  1. How do I set the first checkbox in cell checked?

Here is my codes:

String path = "C:\\Users\\z_jia\\Desktop\\a\\capa.docx";
Document doc = new Document(path);

Table table = (Table) doc.getChildNodes(NodeType.TABLE, true).get(0);
int count = table.getRows().getCount();
Cell cell = table.getRows().get(count - 1).getCells().get(1);
String s = cell.getText();
StructuredDocumentTag sdtCheckBox = (StructuredDocumentTag) cell.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
//sdtCheckBox is null
// sdtCheckBox.setChecked(true);
doc.save("C:\\Users\\z_jia\\Desktop\\a\\capaoutput.docx");

capa.docx (64.9 KB)

@GusGus

  1. Please try using Node.toString method:
Document doc = new Document("C:\\Temp\\in.docx");
    
Table table = (Table) doc.getChildNodes(NodeType.TABLE, true).get(0);
int count = table.getRows().getCount();
Cell cell = table.getRows().get(count - 1).getCells().get(1);
String s = cell.toString(SaveFormat.TEXT);
System.out.println(s);
  1. Checkboes in your document are not actual checkboxxes, they are represented using special symbol, i.e. simple text. So you can only replace this symbol with another that emulates the checked checkbox.

The result is same:

@GusGus The returned text is correct for the last row. If you need to get text of the pre-last row. use the following:

Cell cell = table.getRows().get(count - 2).getCells().get(1);

Thanks. It’s my bad :sweat_smile:

1 Like