Example usage for com.lowagie.text.html HtmlTags DIV

List of usage examples for com.lowagie.text.html HtmlTags DIV

Introduction

In this page you can find the example usage for com.lowagie.text.html HtmlTags DIV.

Prototype

String DIV

To view the source code for com.lowagie.text.html HtmlTags DIV.

Click Source Link

Document

The DIV tag.

Usage

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

License:Open Source License

/**
 * Adds the given section to the RTF document.
 * @param section Section to add.//ww w .ja va 2s. c o  m
 * @param prefix Current index (for example: 3.1.1).
 * @param index Local index.
 * @param parent Parent element.
 * @throws DocumentException
 */
private void addSection(ProjectReportSectionDTO section, StringBuilder prefix, int index, Object parent)
        throws DocumentException, IOException {

    // Adding the title to the document
    final TextElementArray thisSection;
    if (parent instanceof Document) {
        // Style
        final Paragraph paragraph = new Paragraph(section.getName());
        paragraph.getFont().setSize(16);
        paragraph.getFont().setStyle(Font.BOLD);

        // New chapter
        final Chapter chapter = new Chapter(paragraph, index);
        thisSection = chapter;

    } else if (parent instanceof Chapter) {
        // Style
        final Paragraph paragraph = new Paragraph(section.getName());
        paragraph.getFont().setSize(14);
        paragraph.getFont().setStyle(Font.BOLD);

        // New section
        final Section chapterSection = ((Chapter) parent).addSection(paragraph);
        thisSection = chapterSection;

    } else if (parent instanceof TextElementArray) {
        // Style
        final Paragraph paragraph = new Paragraph(prefix.toString() + ' ' + section.getName());
        paragraph.getFont().setSize(12);
        paragraph.getFont().setStyle(Font.BOLD);

        // New paragraph
        ((TextElementArray) parent).add(paragraph);
        thisSection = (TextElementArray) parent;

    } else
        thisSection = null;

    // Adding the content of this section
    int subIndex = 1;
    final int prefixLength = prefix.length();

    final StyleSheet stylesheet = new StyleSheet();
    stylesheet.loadTagStyle(HtmlTags.PARAGRAPH, "margin", "0");
    stylesheet.loadTagStyle(HtmlTags.PARAGRAPH, "padding", "0");
    stylesheet.loadTagStyle(HtmlTags.DIV, "margin", "0");
    stylesheet.loadTagStyle(HtmlTags.DIV, "padding", "0");

    for (final ProjectReportContent child : section.getChildren()) {

        if (child instanceof ProjectReportSectionDTO) {
            prefix.append(index).append('.');

            addSection((ProjectReportSectionDTO) child, prefix, subIndex, thisSection);
            subIndex++;

            prefix.setLength(prefixLength);

        } else if (child instanceof RichTextElementDTO) {

            final String value = ((RichTextElementDTO) child).getText();
            if (value != null && !"".equals(value)) {

                // HTML parsing.
                final List<Element> elements = HTMLWorker.parseToList(new StringReader(value), stylesheet);

                for (final Element element : elements)
                    thisSection.add(element);
            }
        }
    }

    // Adding the chapter to the document
    if (thisSection instanceof Chapter && parent instanceof Document)
        ((Document) parent).add((Chapter) thisSection);
}