List formatting on headers

I am trying to apply list formatting to my headers so that the first header will be in the format of 1, 2, 3 and the second header will have the list format of 1.1, 1.2, 1.3

I’ve tried a number of things but all I get is the header 2 participating in the same list numbering as header one. So if I have an output for header one - 1. and then following that header 2 output, header 2 output will be 2.

List list = doc.getLists().add(ListTemplate.OUTLINE_LEGAL);
doc.getStyles().forEach(s -> {
    if (s.getName().equals("Heading 1")) {

        ListLevel level1 = list.getListLevels().get(1);
        s.getListFormat().setList(list);
        level1.setLinkedStyle(s);
        level1.setNumberStyle(NumberStyle.ARABIC);
        level1.setNumberFormat("\u0000");
        level1.setNumberPosition(0);
        s.getParagraphFormat().setLeftIndent(0);
        try {
            level1.getFont().setBold(true);
            level1.getFont().setItalic(false);
            level1.getFont().setSize(14);
            level1.getFont().setName("Arial");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (s.getName().equals("Heading 2")) {
        ListLevel level = list.getListLevels().get(2);
        s.getListFormat().setList(list);
        level.setNumberFormat("\u0000.\u0001");
        level.isLegal(true);
        level.setLinkedStyle(s);
        level.setRestartAfterLevel(0);
        level.setNumberPosition(0);
        try {
            level.getFont().setBold(true);
            level.getFont().setItalic(false);
            level.getFont().setSize(12);
            level.getFont().setName("Arial");
        } catch (Exception e) {
            e.printStackTrace();
        }
        level.setTextPosition(.25);
        s.getParagraphFormat().setLeftIndent(0);
    }
});

Hi Daniel,

Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you. If you still face problem, please share your input and expected output documents here for our reference. We will then provide you more information about your query along with code.

Document doc = new Document(MyDir + "in.docx");
List list = doc.getLists().add(ListTemplate.OUTLINE_LEGAL);
ListLevel level1 = list.getListLevels().get(0);
level1.setNumberStyle(NumberStyle.ARABIC);
level1.setNumberFormat("\u0000");
ListLevel level2 = list.getListLevels().get(1);
level2.setNumberStyle(NumberStyle.ARABIC);
level2.setNumberFormat("\u0000.\u0001");
level2.isLegal(true);
for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.getParagraphFormat().getStyleName().equals("Heading 1"))
    {
        para.getListFormat().setList(list);
        para.getListFormat().setListLevelNumber(0);
    }
    else if (para.getParagraphFormat().getStyleName().equals("Heading 2"))
    {
        para.getListFormat().setList(list);
        para.getListFormat().setListLevelNumber(1);
    }
}
doc.save(MyDir + "Out.docx");

thanks. that worked.
What I was trying to do was apply a style to a header and have that take affect on the document.
I think the issue is that the document was already created and content was already written (document was created from an html file) so the header styles were not applied.
Is there a way to “re-apply” a header to the file so that all content with that header updates to the updated header style?

Hi Daniel,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Your input Html which you used to create Word document
  • Please attach the expected output Word file that shows the desired behavior.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.