Example usage for com.lowagie.text.rtf RtfWriter2 setAutogenerateTOCEntries

List of usage examples for com.lowagie.text.rtf RtfWriter2 setAutogenerateTOCEntries

Introduction

In this page you can find the example usage for com.lowagie.text.rtf RtfWriter2 setAutogenerateTOCEntries.

Prototype

public void setAutogenerateTOCEntries(boolean autogenerate) 

Source Link

Document

Whether to automagically generate table of contents entries when adding Chapters or Sections.

Usage

From source file:org.sigmah.server.endpoint.export.sigmah.exporter.ProjectReportExporter.java

License:Open Source License

@Override
public void export(OutputStream output) throws ExportException {

    loadReport();/*from   w w w .ja  v  a  2 s  .  co m*/

    // Label displayed instead of the Table of Contents during the export.
    final String tocLabel = localize("projectReportTableOfContents");

    if (report != null) {
        final ProjectReportVersion version = report.getCurrentVersion();

        final ProjectReportDTO reportDTO = GetProjectReportHandler.toDTO(report, version);

        // Generating the RTF
        try {
            final Document document = new Document();
            final RtfWriter2 writer = RtfWriter2.getInstance(document, output);

            writer.setAutogenerateTOCEntries(true);

            document.open();

            // Title
            final Paragraph titleParagraph = new Paragraph(report.getName());
            titleParagraph.getFont().setSize(24);
            titleParagraph.getFont().setStyle(Font.BOLD);
            document.add(titleParagraph);

            document.add(new Paragraph()); // Empty paragraph

            // Table of contents
            final Paragraph tocParagraph = new Paragraph();
            final RtfTableOfContents toc = new RtfTableOfContents(tocLabel);
            tocParagraph.add(toc);
            document.add(tocParagraph);

            // Sections
            final List<ProjectReportSectionDTO> sections = reportDTO.getSections();
            final StringBuilder prefix = new StringBuilder();

            for (int index = 0; index < sections.size(); index++) {
                final ProjectReportSectionDTO section = sections.get(index);

                prefix.append(index + 1).append('.');
                addSection(section, prefix, index + 1, document);

                prefix.setLength(0);
            }

            document.close();

        } catch (DocumentException ex) {
            LOG.error("An error occured while generating the RTF.", ex);

        } catch (IOException e) {
            LOG.debug("An error occured while converting HTML to RTF.");
        }

    } else {
        final String idAsString = requireParameter(ExportUtils.PARAM_EXPORT_PROJECT_ID);

        LOG.error("[export] No project report is identified by '" + idAsString + "'.");
        throw new ExportException("[export] No project report is identified by '" + idAsString + "'.");
    }
}