Hi,
I would like to apply customized styles to automatically generated table of contents with aspose.word (java), could you please advise how to achieve it? Existing posts regarding this matter doesn’t seem to work.
Thanks.
Hi,
I would like to apply customized styles to automatically generated table of contents with aspose.word (java), could you please advise how to achieve it? Existing posts regarding this matter doesn’t seem to work.
Thanks.
Hi Yiding,
Thanks for your query. Please use the following code snippet for your requirement. Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document(MYDir + "in(3).docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert TOC, which uses custom styles.
builder.insertTableOfContents("\\h \\z \\t \"My Heading 1;1;My Heading 2;2\"");
// Update TOC.
doc.updateFields();
doc.updatePageLayout();
// Save the result.
doc.save(MYDir + "AsposeOut.docx");
Document doc = new Document(MYDir + "in.docx");
Style toc1 = doc.getStyles().get("My Heading 1");
Style[] tocStyles = { toc1 };
for (Style style : tocStyles)
{
// change spacing.
style.getParagraphFormat().setSpaceAfterAuto(false);
style.getParagraphFormat().setSpaceAfter(10);
}
doc.save(MYDir + "AsposeOut.docx");
thanks Tahir for your reply, in addition to setting the fonts, I’m having a hard time figuring out about the alignment of the paragraph header in the toc, could you please help? here’s what I’m trying to achieve:
we are using Roman numbers to label each paragraph, so the automatically generated toc looks like this:
I. Paragraph1…
II. Paragraph2…
III. Paragraph3…
IV. Paragraph4…
I would like to have the titles aligned disregarding the length of the title number:
I. Paragraph1…
II. Paragraph2…
III. Paragraph3…
IV. Paragraph4…
Could you please share some ideas on how to make this happen?
Thanks!
Hi Yiding,
Thanks for sharing the details. It would be great if you please share your document along with code for investigation purposes.