Example usage for com.lowagie.text Chapter Chapter

List of usage examples for com.lowagie.text Chapter Chapter

Introduction

In this page you can find the example usage for com.lowagie.text Chapter Chapter.

Prototype

public Chapter(String title, int number) 

Source Link

Document

Constructs a new Chapter.

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.//from w w  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);
}

From source file:org.silverpeas.components.almanach.control.AlmanachPdfGenerator.java

License:Open Source License

static public void buildPdf(String name, AlmanachSessionController almanach, String mode)
        throws AlmanachRuntimeException {
    try {/*from   w  w  w  .  j a  v a2 s  .  c o m*/

        String fileName = FileRepositoryManager.getTemporaryPath(almanach.getSpaceId(),
                almanach.getComponentId()) + name;
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);

        // we add some meta information to the document
        document.addAuthor(almanach.getSettings().getString("author", ""));
        document.addSubject(almanach.getSettings().getString("subject", ""));
        document.addCreationDate();

        PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();

        try {
            Calendar currentDay = Calendar.getInstance();
            currentDay.setTime(almanach.getCurrentDay());
            String sHeader = almanach.getString("events");
            if (mode.equals(PDF_MONTH_ALLDAYS) || mode.equals(PDF_MONTH_EVENTSONLY)) {
                sHeader += " " + almanach.getString("GML.mois" + currentDay.get(Calendar.MONTH));
            }
            sHeader += " " + currentDay.get(Calendar.YEAR);
            HeaderFooter header = new HeaderFooter(new Phrase(sHeader), false);
            HeaderFooter footer = new HeaderFooter(new Phrase("Page "), true);
            footer.setAlignment(Element.ALIGN_CENTER);

            document.setHeader(header);
            document.setFooter(footer);

            createFirstPage(almanach, document);
            document.newPage();

            Font titleFont = new Font(Font.HELVETICA, 24, Font.NORMAL, new Color(255, 255, 255));
            Paragraph cTitle = new Paragraph(almanach.getString("Almanach") + " "
                    + almanach.getString("GML.mois" + currentDay.get(Calendar.MONTH)) + " "
                    + currentDay.get(Calendar.YEAR), titleFont);
            Chapter chapter = new Chapter(cTitle, 1);

            // Collection<EventDetail> events =
            // almanach.getListRecurrentEvent(mode.equals(PDF_YEAR_EVENTSONLY));
            AlmanachCalendarView almanachView;
            if (PDF_YEAR_EVENTSONLY.equals(mode)) {
                almanachView = almanach.getYearlyAlmanachCalendarView();
            } else {
                almanachView = almanach.getMonthlyAlmanachCalendarView();
            }

            List<DisplayableEventOccurrence> occurrences = almanachView.getEvents();
            generateAlmanach(chapter, almanach, occurrences, mode);

            document.add(chapter);
        } catch (Exception ex) {
            throw new AlmanachRuntimeException("PdfGenerator.generate", AlmanachRuntimeException.WARNING,
                    "AlmanachRuntimeException.EX_PROBLEM_TO_GENERATE_PDF", ex);
        }

        document.close();

    } catch (Exception e) {
        throw new AlmanachRuntimeException("PdfGenerator.generate", AlmanachRuntimeException.WARNING,
                "AlmanachRuntimeException.EX_PROBLEM_TO_GENERATE_PDF", e);
    }

}

From source file:org.tpspencer.tal.mvc.document.DocumentWriterImpl.java

License:Apache License

/**
 * Call to start a new chapter/* w w w. j av a  2 s .  c o m*/
 * 
 * @param key The key for the chapter title
 * @param substitutions The substitutions
 */
public void startChapter(String key, String resources, AppElement element) {
    if (resources != null)
        chapterResources = ResourceBundle.getBundle(resources);

    String text = getText(key, element);

    Anchor anchor = new Anchor(text, h1Font);
    anchor.setName(text);

    chapter = new Chapter(new Paragraph(anchor), ++chapterNumber);
}

From source file:questions.importpages.HelloWorldImportedPages.java

/**
 * Generates a PDF file with bookmarks./*  w  ww .  j a  va2s. c o m*/
 * 
 * @param filename
 *            the filename of the PDF file.
 */
private static void createPdf(String filename) {
    // we create a document with multiple pages and bookmarks
    Document document = new Document(PageSize.A4);
    try {
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.add(new Paragraph(
                "In this document, we are going to say hello to different beings in different languages."));
        document.newPage();
        Paragraph hello = new Paragraph(
                "(English:) hello, " + "(Esperanto:) he, alo, saluton, (Latin:) heu, ave, "
                        + "(French:) all\u00f4, (Italian:) ciao, (German:) hallo, he, heda, holla, "
                        + "(Portuguese:) al\u00f4, ol\u00e1, hei, psiu, bom d\u00eda, (Dutch:) hallo, dag, "
                        + "(Spanish:) ola, eh, (Catalan:) au, bah, eh, ep, "
                        + "(Swedish:) hej, hejsan(Danish:) hallo, dav, davs, goddag, hej, "
                        + "(Norwegian:) hei; morn, (Papiamento:) halo; hallo; k\u00ed tal, "
                        + "(Faeroese:) hall\u00f3, hoyr, (Turkish:) alo, merhaba, (Albanian:) tungjatjeta");
        Chapter universe = new Chapter("To the Universe:", 1);
        Section section;
        section = universe.addSection("to the World:");
        section.add(hello);
        section = universe.addSection("to the Sun:");
        section.add(hello);
        section = universe.addSection("to the Moon:");
        section.add(hello);
        section = universe.addSection("to the Stars:");
        section.add(hello);
        document.add(universe);
        Chapter people = new Chapter("To the People:", 2);
        section = people.addSection("to mothers and fathers:");
        section.add(hello);
        section = people.addSection("to brothers and sisters:");
        section.add(hello);
        section = people.addSection("to wives and husbands:");
        section.add(hello);
        section = people.addSection("to sons and daughters:");
        section.add(hello);
        section = people.addSection("to complete strangers:");
        section.add(hello);
        document.add(people);
        document.setPageSize(PageSize.A4.rotate());
        Chapter animals = new Chapter("To the Animals:", 3);
        section = animals.addSection("to cats and dogs:");
        section.add(hello);
        section = animals.addSection("to birds and bees:");
        section.add(hello);
        section = animals.addSection("to farm animals and wild animals:");
        section.add(hello);
        section = animals.addSection("to bugs and beatles:");
        section.add(hello);
        section = animals.addSection("to fish and shellfish:");
        section.add(hello);
        document.add(animals);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:sg.edu.nus.util.ReportWriter.java

private static void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("First Chapter", catFont);
    anchor.setName("First Chapter");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategory 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragraph 1"));
    subCatPart.add(new Paragraph("Paragraph 2"));
    subCatPart.add(new Paragraph("Paragraph 3"));

    // Add a  little list
    createList(subCatPart);/*from   w w  w  . j  a  v  a  2s .  c o m*/

    // Add a small table
    createTable(subCatPart);
    // Now a small table

    // Now add all this to the document
    document.add(catPart);

    // Next section
    anchor = new Anchor("Second Chapter", catFont);
    anchor.setName("Second Chapter");

    // Second parameter is the number of the chapter
    catPart = new Chapter(new Paragraph(anchor), 1);

    subPara = new Paragraph("Subcategory", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("This is a very important message"));

    // Now add all this to the document
    document.add(catPart);

}