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.

Tue Oct 03, 2023 6:47 am

Hi Team ,

I have added TOC [ paragraph.appendTOC(1, 3) ] .In toc all headings are in Bold .My requirement is only Heading1 should be in Bold and I want space after each line in the table of contents section. Please help us to resolve this issue


Thanks in advance

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

Tue Oct 03, 2023 9:10 am

Hello,

Thank you for your inquiry.
Please use the following code to implement that only Heading1 is bold.
Code: Select all
paragraph.appendTOC(1,3);

ParagraphStyle tocStyle = (ParagraphStyle) Style.createBuiltinStyle(BuiltinStyle.Toc_1, document);
tocStyle.getCharacterFormat().setBold(false);
document.getStyles().add(tocStyle);

for (int j = 0; j < section.getChildObjects().getCount(); j++) {
    DocumentObject cObj = section.getChildObjects().get(j);
    if (cObj instanceof Paragraph) {
        Paragraph para = (Paragraph) ((cObj instanceof Paragraph) ? cObj : null);
        if (para.getStyleName().equals("TOC2") || para.getStyleName().equals("TOC3")) {
            para.applyStyle(tocStyle.getName());
        }
    }
}
document.updateTableOfContents();

Also, do you mean not to display page numbers when you mention 'there are spaces after each line in the table of contents'? If so, please use the following code. If not, please provide a more detailed explanation. Thank you in advance.
Code: Select all
TableOfContent content = paragraph.appendTOC(1,3);
content.setRightAlignPageNumbers(false);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Tue Oct 03, 2023 10:30 am

Thank you Annika .

About Second query : I have to add empty paragraph OR space between Line1 ,Line2 and Line3[after each line in TOC section]

I. INTRODUCTION..........................5 //Line1
1.1 Description1.............................5 //Line2
1.2 Description2.........................5. //Line3[/size]


Like this:
I. INTRODUCTION..........................5 //Line1

1.1 Description1.............................5 //Line2

1.2 Description2.........................5. [/size][/size]


Thanks in advance

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

Wed Oct 04, 2023 12:44 pm

Hello,

Thanks for your more information.
Currently, I don’t find the directly solution for your scenario. However, you can set a larger value for lineSpace of TOC paragraph. I put the complete code below for your reference.
If you have any issues, just feel free to contact us.


Code: Select all
TableOfContent content = paragraph.appendTOC(1,3);
 ParagraphStyle tocStyle =  (ParagraphStyle) Style.createBuiltinStyle(BuiltinStyle.Toc_2, doc);
        tocStyle.getCharacterFormat().setBold(false);
        tocStyle.getParagraphFormat().setLineSpacingRule(LineSpacingRule.Exactly);
        tocStyle.getParagraphFormat().setLineSpacing(40f);
        doc.getStyles().add(tocStyle);

        ParagraphStyle tocStyleToc1 =  (ParagraphStyle) Style.createBuiltinStyle(BuiltinStyle.Toc_1, doc);
        tocStyleToc1.getCharacterFormat().setBold(true);
        tocStyleToc1.getParagraphFormat().setLineSpacingRule(LineSpacingRule.Exactly);
        tocStyleToc1.getParagraphFormat().setLineSpacing(40f);
        doc.getStyles().add(tocStyleToc1);

        for (int k = 0; k < section.getParagraphs().getCount(); k++) {
            Paragraph para = section.getParagraphs().get(k);
            if (para.getStyleName().equals("TOC2") || para.getStyleName().equals("TOC3")) {
                para.applyStyle(tocStyle.getName());

            }
            if (para.getStyleName().equals("TOC1")) {
                para.applyStyle(tocStyle.getName());
                para.applyStyle(tocStyleToc1.getName());
            }
        }
 doc.updateTableOfContents();


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc

cron