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 Dec 06, 2022 2:23 pm
Hello, how can i set my own Style to be a Header?
- I created TOC
- Created ParagraphStyle
- applied ParagraphStyle to paragraph
- updated TOC
- but TOC didn\t show my header
ParagraphStyle style = new ParagraphStyle(doc);
Section sec = doc.addSection;
Paragraph para = sec.addParagraph();
para.appendTOC(1,3);
Paragraph para2 = sec.addParagraph();
para2.appendText("header");
para2.applyStyle("style")
doc.updateTableOfContents();
-

Adamantin
-
- Posts: 3
- Joined: Tue Dec 06, 2022 2:09 pm
Wed Dec 07, 2022 10:08 am
Hi,
Thanks for your inquiry.
According to your description, you can refer to the following code to achieve your requirement, if you have further questions, just feel free to contact us.
- Code: Select all
Document doc = new Document();
Section sec = doc.addSection();
ParagraphStyle style = new ParagraphStyle(doc);
style.setName("style4toc");
style.getCharacterFormat().setBold(true);
style.getCharacterFormat().setFontName("Arial");
style.getCharacterFormat().setItalic(true);
style.applyBaseStyle(BuiltinStyle.Heading_1);
// save style to doc
doc.getStyles().add(style);
// add an empty paragraph
Paragraph paragraph1 = sec.addParagraph();
// create a paragraph and apply the style you created just now
Paragraph paragraph = sec.addParagraph();
paragraph.appendText("TOC");
paragraph.applyStyle("style4toc");
TableOfContent tableOfContent = paragraph1.appendTOC(1,3);
// add toc to doc
doc.setTOC(tableOfContent);
// update toc
doc.updateTableOfContents();
doc.saveToFile("sample.docx", FileFormat.Docx_2010);
Sincerely,
Triste
E-iceblue support team
-


Triste.Dai
-
- Posts: 1000
- Joined: Tue Nov 15, 2022 3:59 am