Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JODF
>
Tutorial
> Create table of contents
The following example shows you how to create table of contents.
import com.independentsoft.office.odf.ChapterIndexEntry; import com.independentsoft.office.odf.Heading; import com.independentsoft.office.odf.IndexBody; import com.independentsoft.office.odf.IndexTitle; import com.independentsoft.office.odf.IndexTitleTemplate; import com.independentsoft.office.odf.PageNumberIndexEntry; import com.independentsoft.office.odf.Paragraph; import com.independentsoft.office.odf.TabStopIndexEntry; import com.independentsoft.office.odf.TabStopIndexEntryType; import com.independentsoft.office.odf.TableOfContents; import com.independentsoft.office.odf.TableOfContentsEntryTemplate; import com.independentsoft.office.odf.TableOfContentsSource; import com.independentsoft.office.odf.TextDocument; import com.independentsoft.office.odf.TextIndexEntry; import com.independentsoft.office.odf.styles.Break; import com.independentsoft.office.odf.styles.ParagraphStyle; public class Example { public static void main(String[] args) { try { TextDocument doc = new TextDocument(); Paragraph tocParagraph = new Paragraph(); tocParagraph.add("Table of Contents"); Paragraph p1 = new Paragraph(); p1.add("First chapter"); p1.addTab(); p1.add("2"); Paragraph p2 = new Paragraph(); p2.add("Second chapter"); p2.addTab(); p2.add("3"); IndexTitle indexTitle = new IndexTitle(); indexTitle.getContent().add(tocParagraph); IndexBody indexBody = new IndexBody(); indexBody.getContent().add(indexTitle); indexBody.getContent().add(new Paragraph()); //empty paragraph indexBody.getContent().add(p1); indexBody.getContent().add(p2); IndexTitleTemplate indexTitleTemplate = new IndexTitleTemplate(); indexTitleTemplate.setValue("Table of Contents"); TabStopIndexEntry tabStopIndexEntry = new TabStopIndexEntry(); tabStopIndexEntry.setLeaderCharacter("."); tabStopIndexEntry.setType(TabStopIndexEntryType.RIGHT); TableOfContentsEntryTemplate entry1 = new TableOfContentsEntryTemplate(); entry1.setOutlineLevel(1); entry1.getContent().add(new ChapterIndexEntry()); entry1.getContent().add(new TextIndexEntry()); entry1.getContent().add(tabStopIndexEntry); entry1.getContent().add(new PageNumberIndexEntry()); TableOfContentsSource tocSource = new TableOfContentsSource(); tocSource.setOutlineLevel(10); tocSource.setIndexTitleTemplate(indexTitleTemplate); tocSource.getEntryTemplates().add(entry1); TableOfContents toc = new TableOfContents(); toc.setProtected(true); toc.setIndexBody(indexBody); toc.setSource(tocSource); Heading heading1 = new Heading(); heading1.setLevel(1); heading1.add("First chapter"); Heading heading2 = new Heading(); heading2.setLevel(1); heading2.add("Second chapter"); ParagraphStyle style1 = new ParagraphStyle("MyStyle"); style1.getParagraphProperties().setBreakAfter(Break.PAGE); Paragraph pageBreakParagraph = new Paragraph(); pageBreakParagraph.setStyle("MyStyle"); doc.getAutomaticStyles().getStyles().add(style1); doc.getBody().add(toc); doc.getBody().add(pageBreakParagraph); doc.getBody().add(heading1); doc.getBody().add(pageBreakParagraph); doc.getBody().add(heading2); doc.save("c:\\test\\output.odt", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*