How to add StructuredDocumentTag DropDownList to a table

I am trying to add a SDT DropDownList to a table in the first column, so user can choose " " or “X”. I tried using the MarkupLevel.CELL but I get an error saying “cannot insert a node of this type at this location”. I have better luck using the MarkupLevel.INLINE but I’m only getting the DropDownList output in the last row of the table. Can you please tell me how to properly add the SDT to the table? I would like it to appear similar to the table in attachment GoodDropDownSampleA.docx. My current output is dropdownsdtinTable.docx.

I’m using Aspose.Words.for.Java version 14.12.0.

Thanks

My code is as follows:

public class DropDownSdtInTable {

    private void createJurSelectTable(ArrayList list, HashMap<String, String> jurMap)
            throws Exception {
        Document document = new Document();
        DocumentBuilder builder = new DocumentBuilder(document);

        // initialize table
        Table table = builder.startTable();

        // clear previous style format
        builder.getParagraphFormat().clearFormatting();
        builder.getParagraphFormat().setStyleName("Normal");

        // insert cell for 1st column of 1st row in table
        builder.insertCell();
        builder.getCellFormat().getShading()
                .setBackgroundPatternColor(new Color(217, 217, 217));
        builder.insertCell().getCellFormat()
                .setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().setTopPadding(5.0);
        builder.getCellFormat().setBottomPadding(5.0);
        //
        // insert cell for 2nd column of 1st row in table
        // write out 1st column heading
        builder.write(" Column 2 heading ");
        builder.getCellFormat().getShading()
                .setBackgroundPatternColor(new Color(217, 217, 217));
        builder.insertCell().getCellFormat()
                .setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().setTopPadding(5.0);
        builder.getCellFormat().setBottomPadding(5.0);

        // insert cell for 3rd column of 1st row in table
        // write out 2nd column heading
        builder.write(" Column 3 heading ");
        builder.getCellFormat().getShading()
                .setBackgroundPatternColor(new Color(217, 217, 217));
        builder.getCellFormat().setTopPadding(5.0);
        builder.getCellFormat().setBottomPadding(5.0);

        // end 1st row of table
        builder.endRow();

        StructuredDocumentTag xSdt = createXDropdown(document);

        // loop over jurisdiction List
        for (String jurisdiction : list) {
            String legacyId = jurMap.get(jurisdiction);
            createJurRow(builder, jurisdiction, legacyId, xSdt);
        }

        // ends table
        builder.endTable();
        document.save("C:" + File.separator + "temp" + File.separator
                \+ "dropdownsdtinTable.docx", SaveFormat.DOCX);
    }
    private void createJurRow(DocumentBuilder builder, String jurName,
                                String legacyId, StructuredDocumentTag xSdt) throws Exception {
        // clear formatting from 1st row
        builder.getCellFormat().clearFormatting();
        // inserting cell for 1st column
        builder.insertCell();
        builder.getCellFormat().setTopPadding(5.0);
        builder.getCellFormat().setBottomPadding(5.0);
        builder.insertNode(xSdt);
        builder.write("");

        // inserting cell for 2nd column
        builder.insertCell();
        builder.getCellFormat().setTopPadding(5.0);
        builder.getCellFormat().setBottomPadding(5.0);
        builder.write(jurName.trim());

        // inserting cell for 3nd column
        builder.insertCell();
        builder.getCellFormat().setTopPadding(5.0);
        builder.getCellFormat().setBottomPadding(5.0);
        if (legacyId != null) {
            builder.write(legacyId.trim());
        }

        builder.endRow();
    }

    private StructuredDocumentTag createXDropdown(Document document) {
        StructuredDocumentTag xSdt = null;
        // creates re-usable SDT 
        try {
            xSdt = new StructuredDocumentTag(document, SdtType.DROP_DOWN_LIST,
                    MarkupLevel.INLINE);
            xSdt.setTitle("Sel");
            xSdt.setTag("sel");
            xSdt.getListItems().add(new SdtListItem("X", "X"));
            xSdt.getListItems().add(new SdtListItem(" ", " "));
            xSdt.getListItems().setSelectedValue(xSdt.getListItems().get(0));
            xSdt.isShowingPlaceholderText(false);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return xSdt;
    }

    public static void main(String[] args) {
        ArrayList myList = new ArrayList();
        myList.add("1");
        myList.add("2");
        myList.add("3");

        HashMap<String, String> myMap = new HashMap<String, String>();
        myMap.put("1", "A");
        myMap.put("2", "B");
        myMap.put("3", "C");

        DropDownSdtInTable runit = new DropDownSdtInTable();

        try {
            runit.createJurSelectTable(myList, myMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("DONE ");
    }

}

Hi there,

Thanks for your inquiry. In your case, you need to clone the StructuredDocumentTag node as shown in following highlighted code snippet. Please use the highlighted code snippet in your createJurSelectTable method.

Hope this helps you. Please let us know if you have any more queries.

private void createJurSelectTable(ArrayList list, HashMap jurMap)
        throws Exception {
    Document document = new Document();
    DocumentBuilder builder = new DocumentBuilder(document);
    // initialize table
    Table table = builder.startTable();
    // clear previous style format
    builder.getParagraphFormat().clearFormatting();
    builder.getParagraphFormat().setStyleName("Normal");
    // insert cell for 1st column of 1st row in table
    builder.insertCell();
    builder.getCellFormat().getShading()
            .setBackgroundPatternColor(new Color(217, 217, 217));
    builder.insertCell().getCellFormat()
            .setVerticalAlignment(CellVerticalAlignment.CENTER);
    builder.getCellFormat().setTopPadding(5.0);
    builder.getCellFormat().setBottomPadding(5.0);
    //
    // insert cell for 2nd column of 1st row in table
    // write out 1st column heading
    builder.write(" Column 2 heading ");
    builder.getCellFormat().getShading()
            .setBackgroundPatternColor(new Color(217, 217, 217));
    builder.insertCell().getCellFormat()
            .setVerticalAlignment(CellVerticalAlignment.CENTER);
    builder.getCellFormat().setTopPadding(5.0);
    builder.getCellFormat().setBottomPadding(5.0);
    // insert cell for 3rd column of 1st row in table
    // write out 2nd column heading
    builder.write(" Column 3 heading ");
    builder.getCellFormat().getShading()
            .setBackgroundPatternColor(new Color(217, 217, 217));
    builder.getCellFormat().setTopPadding(5.0);
    builder.getCellFormat().setBottomPadding(5.0);
    // end 1st row of table
    builder.endRow();
    StructuredDocumentTag xSdt = createXDropdown(document);
    // loop over jurisdiction List
    for (String jurisdiction : list) {
        String legacyId = jurMap.get(jurisdiction);
        createJurRow(builder, jurisdiction, legacyId, (StructuredDocumentTag)xSdt.deepClone(true));
    }
    // ends table
    builder.endTable();
    document.save("C:" + File.separator + "temp" + File.separator
            + "dropdownsdtinTable.docx", SaveFormat.DOCX);
}

That worked perfectly! Thanks for the quick response.

Hi there,

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