Example usage for com.lowagie.text.rtf.field RtfTableOfContents RtfTableOfContents

List of usage examples for com.lowagie.text.rtf.field RtfTableOfContents RtfTableOfContents

Introduction

In this page you can find the example usage for com.lowagie.text.rtf.field RtfTableOfContents RtfTableOfContents.

Prototype

public RtfTableOfContents(String defaultText) 

Source Link

Document

Constructs a RtfTableOfContents.

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();/* www.j  ava  2  s. c  om*/

    // 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 + "'.");
    }
}