Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri May 17, 2024 6:25 am

Hi Team ,

How to add specific number of character in TOC


Table Of Contents
Steps 4
1 . aaaaaaaaaaaaaaaaaaa 4
2 . aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 4
3 . aaaaaaaaaaaaaaaaaaaaaaaaa 4
4 . aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 4


My requrement : //
Table Of Contents
Steps 4
1 . aaaaaaaaaaaaaaaaaaa 4
2 . aaaaaaaaaaaaaaaaaaa 4
3 . aaaaaaaaaaaaaaaaaaa 4
4 . aaaaaaaaaaaaaaaaaaa 4




Thanks in advance

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Fri May 17, 2024 9:03 am

Hi,

Thank you for your inquiry.
Please refer to code below to add number. If there's still any issue, welcome to write back.

Code: Select all
    private static void updateDocTOC()
    {
        //Load document
        Document doc = new Document();
        doc.loadFromFile("Template_Toc.docx");

        // Create ListStyle
        ListStyle listStyle = new ListStyle(doc, ListType.Numbered);
        listStyle.setName("toclist");
        listStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic);
        doc.getListStyles().add(listStyle);

        //Loop through sections
        for (Object objSection : doc.getSections())
        {
            Section section= (Section) objSection;
            //Loop through content of section
            for (Object obj : section.getBody().getChildObjects())
            {
                //Find the structure document tag
                if (obj instanceof StructureDocumentTag)
                {
                    StructureDocumentTag tag = (StructureDocumentTag) obj;
                    //Find the paragraph where the TOC1 locates
                    for(Object cObj : tag.getChildObjects())
                    {
                        if (cObj instanceof Paragraph)
                        {
                            Paragraph para = (Paragraph) cObj;
                            System.out.println(para.getStyleName());
                            if (para.getStyleName().equals("TOC1"))
                            {
                                //Apply the new style for TOC1 paragraph
                                para.getListFormat().applyStyle(listStyle.getName());
                            }
                        }
                    }
                }
            }
        }

        //Save the Word file
        String output = "ChangeTOCStyle_out.docx";
        doc.saveToFile(output, FileFormat.Docx);
    }


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 460
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.Doc