Example usage for com.lowagie.text Paragraph Paragraph

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

Introduction

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

Prototype

public Paragraph() 

Source Link

Document

Constructs a Paragraph.

Usage

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

License:Apache License

private void printElementHtml(Element element, Object parent, int depth, Font font, int parentLevel) {
    String tag = element.getName();
    Object av;/*ww  w  . j a v a 2  s . c o m*/
    if (element instanceof HTMLDocument.RunElement) {
        HTMLDocument.RunElement re = (HTMLDocument.RunElement) element;
        int start = re.getStartOffset();
        int end = re.getEndOffset();
        try {
            String content = re.getDocument().getText(start, end - start);
            printAttributesHtml(re);
            av = re.getAttribute(CSS.Attribute.FONT_SIZE);
            String fontsize = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.FONT_FAMILY);
            String fontfamily = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.COLOR);
            String fontcolor = av == null ? null : av.toString();
            if (fontcolor != null || fontsize != null || fontfamily != null) {
                if (fontfamily == null)
                    fontfamily = font.getFamilyname();
                float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 9);
                int style = font.getStyle();
                Color color;
                if (fontcolor != null) {
                    color = Color.decode(fontcolor);
                } else
                    color = font.getColor();
                font = FontFactory.getFont(fontfamily, size, style, color);
            }
            if (parent instanceof Paragraph) {
                ((Paragraph) parent).add(new Chunk(content, font));
            } else {
                System.err.println("chunk with parent "
                        + (parent == null ? "null" : parent.getClass().getName()) + ": " + content);
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    } else if (element instanceof HTMLDocument.BlockElement) {
        HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
        printAttributesHtml(be);
        av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN);
        String align = av == null ? null : av.toString();
        if (tag.equalsIgnoreCase("html")) {
            printElementChildrenHtml(element, parent, depth + 1, font, parentLevel);
        } else if (tag.equalsIgnoreCase("head")) {
            // do nothing
        } else if (tag.equalsIgnoreCase("body")) {
            printElementChildrenHtml(element, parent, depth + 1, font, parentLevel);
        } else if (tag.equalsIgnoreCase("p")) {
            if (parent instanceof Section) {
                Paragraph paragraph = new Paragraph();
                if (align != null) {
                    paragraph.setAlignment(align);
                }
                printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
                ((Section) parent).add(paragraph);
            } else {
                System.err.println("p with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("h1") || tag.equalsIgnoreCase("h2") || tag.equalsIgnoreCase("h3")) {
            if (parent instanceof Section) {
                Paragraph title = new Paragraph();
                printElementChildrenHtml(element, title, depth + 1, subSectionFont, parentLevel);
                ((Section) parent).addSection(title, parentLevel == 0 ? 0 : (parentLevel + 1));
            } else {
                System.err
                        .println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("ul")) {
            if (parent instanceof Section) {
                com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f);
                printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel);
                ((Section) parent).add(list);
            } else {
                System.err
                        .println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("ol")) {
            if (parent instanceof Section) {
                com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f);
                printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel);
                ((Section) parent).add(list);
            } else {
                System.err
                        .println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
            }
        } else if (tag.equalsIgnoreCase("li")) {
            ListItem li = new ListItem();
            li.setSpacingAfter(0.0f);
            printElementChildrenHtml(element, li, depth + 1, normalFont, parentLevel);
            ((com.lowagie.text.List) parent).add(li);
        } else if (tag.equalsIgnoreCase("p-implied")) {
            if (parent instanceof ListItem) {
                Paragraph paragraph = new Paragraph();
                printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
                ((ListItem) parent).add(paragraph);
            } else if (parent instanceof Cell) {
                Paragraph paragraph = new Paragraph();
                printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
                ((Cell) parent).add(paragraph);
            }
        } else if (tag.equalsIgnoreCase("table")) {
            try {
                Table table = new Table(3);
                table.setBorderWidth(1);
                table.setBorderColor(new Color(0, 128, 128));
                table.setPadding(1.0f);
                table.setSpacing(0.5f);
                Cell c = new Cell("header");
                c.setHeader(true);
                c.setColspan(3);
                table.addCell(c);
                table.endHeaders();
                printElementChildrenHtml(element, table, depth + 1, normalFont, parentLevel); // TODO
                ((Section) parent).add(table);
            } catch (BadElementException e) {
                e.printStackTrace();
            }
        } else if (tag.equalsIgnoreCase("tr")) {
            printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel); // TODO
        } else if (tag.equalsIgnoreCase("td")) {
            Cell cell = new Cell();
            printElementChildrenHtml(element, cell, depth + 1, normalFont, parentLevel); // TODO
            ((Table) parent).addCell(cell);
        } else {
            System.err.println("Unknown element " + element.getName());
            printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel);
        }
    } else {
        return; // could be BidiElement - not sure what it is
    }
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

License:Apache License

private Object generateElementHtml(Element element, int depth, Font font) {
    String tag = element.getName();
    Object myself;/*from w  w  w  . j  a  v a 2 s.c om*/
    Object av;
    if (element instanceof HTMLDocument.RunElement) {
        HTMLDocument.RunElement re = (HTMLDocument.RunElement) element;
        int start = re.getStartOffset();
        int end = re.getEndOffset();
        try {
            String content = re.getDocument().getText(start, end - start);
            HtmlAttr htmlattr = printAttributesHtml(re);
            av = re.getAttribute(CSS.Attribute.FONT_SIZE);
            String fontsize = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.FONT_FAMILY);
            String fontfamily = av == null ? null : av.toString();
            av = re.getAttribute(CSS.Attribute.COLOR);
            String fontcolor = av == null ? null : av.toString();
            if (fontcolor != null || fontsize != null || fontfamily != null) {
                if (fontfamily == null)
                    fontfamily = font.getFamilyname();
                if (fontsize != null && fontsize.endsWith("pt"))
                    fontsize = fontsize.substring(0, fontsize.indexOf("pt"));
                float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 8);
                int style = font.getStyle();
                Color color;
                if (fontcolor != null) {
                    color = Color.decode(fontcolor);
                } else
                    color = font.getColor();
                font = FontFactory.getFont(fontfamily, size, style, color);
            } else if (htmlattr.bold || htmlattr.italic) {
                String family = font.getFamilyname();
                float size = font.getSize();
                Color color = font.getColor();
                if (htmlattr.bold && htmlattr.italic)
                    font = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
                else if (htmlattr.italic)
                    font = FontFactory.getFont(family, size, Font.ITALIC, color);
                else if (htmlattr.bold)
                    font = FontFactory.getFont(family, size, Font.BOLD);
            }
            myself = new Chunk(content, font);
        } catch (BadLocationException e) {
            e.printStackTrace();
            myself = null;
        }
    } else if (element instanceof HTMLDocument.BlockElement) {
        HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
        HtmlAttr htmlattr = printAttributesHtml(be);
        if (htmlattr.bold) {
            System.out.println("+++BOLD!!!");
        }
        av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN);
        String align = av == null ? null : av.toString();
        if (htmlattr.bold || htmlattr.italic) {
            String family = font.getFamilyname();
            float size = font.getSize();
            Color color = font.getColor();
            if (htmlattr.bold && htmlattr.italic)
                font = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
            else if (htmlattr.italic)
                font = FontFactory.getFont(family, size, Font.ITALIC, color);
            else if (htmlattr.bold)
                font = FontFactory.getFont(family, size, Font.BOLD, Color.blue);
        }
        if (tag.equalsIgnoreCase("html")) {
            myself = generateElementChildrenHtml(element, depth + 1, font);
        } else if (tag.equalsIgnoreCase("head")) {
            myself = null;
        } else if (tag.equalsIgnoreCase("body")) {
            myself = generateElementChildrenHtml(element, depth + 1, font);
        } else if (tag.equalsIgnoreCase("p") || tag.equalsIgnoreCase("p-implied")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            Paragraph paragraph = new Paragraph();
            paragraph.setFirstLineIndent(0F);
            for (Object child : children) {
                if (child instanceof Chunk) {
                    Chunk chunk = (Chunk) child;
                    /*if (!chunk.getContent().equals("\n"))*/ paragraph.add(chunk);
                } else
                    paragraph.add(child);
            }
            if (align != null)
                paragraph.setAlignment(align);
            myself = paragraph;
        } else if (tag.equalsIgnoreCase("h1") || tag.equalsIgnoreCase("h2") || tag.equalsIgnoreCase("h3")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, subSectionFont);
            Paragraph title = new Paragraph();
            for (Object child : children) {
                title.add(child);
            }
            myself = new TempSectionPdf(title);
        } else if (tag.equalsIgnoreCase("ul")) {
            com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f);
            list.setIndentationLeft(25.0f);
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                list.add(child);
            }
            myself = list;
        } else if (tag.equalsIgnoreCase("ol")) {
            com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f);
            list.setIndentationLeft(25.0f);
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                list.add(child);
            }
            myself = list;
        } else if (tag.equalsIgnoreCase("li")) {
            ListItem li = new ListItem();
            li.setSpacingAfter(0.0f);
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                li.add(child);
            }
            myself = li;
        } else if (tag.equalsIgnoreCase("table")) {
            List<Object> rows = generateElementChildrenHtml(element, depth + 1, normalFont);
            try {
                int ncols = 0;
                for (Object row : rows) {
                    if (row instanceof List<?>) {
                        int n = ((List<?>) row).size();
                        if (n > ncols)
                            ncols = n;
                    }
                }
                Table table = new Table(2);
                table.setBorderWidth(1);
                table.setBorderColor(new Color(0, 128, 128));
                table.setPadding(1.0f);
                table.setSpacing(0.5f);
                Cell c = new Cell("header");
                c.setHeader(true);
                c.setColspan(ncols);
                table.addCell(c);
                table.endHeaders();
                for (Object row : rows) {
                    if (row instanceof List<?>) {
                        for (Object cell : (List<?>) row) {
                            if (cell instanceof Cell)
                                table.addCell((Cell) cell);
                        }
                    }
                }
                myself = table;
            } catch (BadElementException e) {
                e.printStackTrace();
                myself = null;
            }
        } else if (tag.equalsIgnoreCase("tr")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            myself = children;
        } else if (tag.equalsIgnoreCase("td")) {
            Cell cell = new Cell();
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                cell.add(child);
            }
            myself = cell;
        } else if (tag.equalsIgnoreCase("div")) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            Paragraph paragraph = new Paragraph();
            paragraph.setFirstLineIndent(0F);
            for (Object child : children) {
                paragraph.add(child);
            }
            if (align != null)
                paragraph.setAlignment(align);
            myself = paragraph;
        } else {
            System.err.println("Unknown element " + element.getName());
            myself = null;
        }
    } else {
        myself = null; // could be BidiElement - not sure what it is
    }
    return myself;
}

From source file:com.exam.server.ConvertPDF.java

private static void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);//from   w  w w  .j a v  a2 s .c  om
    // Lets write a big header
    preface.add(new Paragraph("Title of the document", catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Report generated by:  Ashutosh , " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    //                preface.add(new Paragraph(
    //                      "Report generated by: " + System.getProperty("user.name","") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    //                      smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));

    addEmptyLine(preface, 8);

    preface.add(new Paragraph(
            "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));

    document.add(preface);
    // Start a new page
    document.newPage();
}

From source file:com.geek.tutorial.itext.text.Paragraph_Example.java

License:Open Source License

public Paragraph_Example() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("paragraph_example.pdf"));
    document.open();//from ww  w  . j a  v a2  s.  com

    Font font = new Font(Font.COURIER, 10, Font.BOLD);
    font.setColor(new Color(0x92, 0x90, 0x83));
    Chunk chunk = new Chunk("testing text element ", font);
    chunk.setBackground(new Color(0xff, 0xe4, 0x00));

    Phrase phrase = new Phrase(20, "This is initial text. ");

    for (int i = 0; i < 10; i++) {
        phrase.add(chunk);
    }

    Paragraph paragraph = new Paragraph(); // 1
    paragraph.add(phrase); // 2

    document.add(paragraph); // 3   

    document.add(paragraph); // 4   

    document.close();
}

From source file:com.gp.cong.logisoft.reports.BookingCoverSheetPdfCreater.java

public Paragraph getContainers(String simpleRequest, SearchBookingReportDTO searchBookingReportDTO,
        MessageResources messageResources) {
    if (simpleRequest.equals("simpleRequest")) {
        Paragraph bkgParagraph1 = new Paragraph();
        List chargesList = searchBookingReportDTO.getChargesList();
        BookingfclUnits bookingfclUnits = new BookingfclUnits();
        String container = "";
        Map<String, String> summaryMap = new HashMap<String, String>();
        if (chargesList != null && chargesList.size() > 0) {
            boolean isFirst = true;
            for (int i = 0; i < chargesList.size(); i++) {
                bookingfclUnits = (BookingfclUnits) chargesList.get(i);
                if (bookingfclUnits.getApproveBl() != null
                        && bookingfclUnits.getApproveBl().equalsIgnoreCase("Yes")) {
                    if (bookingfclUnits.getPrint() != null
                            && !bookingfclUnits.getPrint().equalsIgnoreCase("on")) {
                        String temp = bookingfclUnits.getUnitType().getCodedesc().toString() + "-"
                                + bookingfclUnits.getSpecialEquipmentUnit() + "-"
                                + bookingfclUnits.getStandardCharge();
                        String newTemp = bookingfclUnits.getUnitType().getCodedesc();
                        if (null == summaryMap.get(temp)) {
                            String tempArray[] = newTemp.split("=");
                            //                                if (!isFirst) {
                            //                                    bkgParagraph1.add(new Phrase(" \n"));
                            //                                }
                            if (tempArray[1].equalsIgnoreCase(messageResources.getMessage("container40HC"))) {
                                container = bookingfclUnits.getNumbers() + " X " + "40" + "'" + "HC ";
                                bkgParagraph1.add(new Chunk(container, blackBoldFont2));
                                if ("Y".equalsIgnoreCase(bookingfclUnits.getOutOfGauge())) {
                                    bkgParagraph1.add(new Phrase(", "));
                                    bkgParagraph1.add(new Chunk(OUT_OF_GAUGE, GreenFont8));
                                }/*www .j  ava 2  s.com*/
                                if (null != bookingfclUnits.getSpecialEquipment()
                                        && !bookingfclUnits.getSpecialEquipment().equals("")) {
                                    bkgParagraph1.add(new Phrase(", "));
                                    bkgParagraph1
                                            .add(new Chunk(bookingfclUnits.getSpecialEquipment(), GreenFont8));
                                }
                                isFirst = false;
                            } else if (tempArray[1]
                                    .equalsIgnoreCase(messageResources.getMessage("container40NOR"))) {
                                container = bookingfclUnits.getNumbers() + " X " + "40" + "'" + "NOR ";
                                bkgParagraph1.add(new Chunk(container, blackBoldFont2));
                                if ("Y".equalsIgnoreCase(bookingfclUnits.getOutOfGauge())) {
                                    bkgParagraph1.add(new Phrase(", "));
                                    bkgParagraph1.add(new Chunk(OUT_OF_GAUGE, GreenFont8));
                                }
                                if (null != bookingfclUnits.getSpecialEquipment()
                                        && !bookingfclUnits.getSpecialEquipment().equals("")) {
                                    bkgParagraph1.add(new Phrase(", "));
                                    bkgParagraph1
                                            .add(new Chunk(bookingfclUnits.getSpecialEquipment(), GreenFont8));
                                }
                                isFirst = false;
                            } else {
                                container = bookingfclUnits.getNumbers() + " X " + tempArray[1] + "' ";
                                bkgParagraph1.add(new Chunk(container, blackBoldFont2));
                                if ("Y".equalsIgnoreCase(bookingfclUnits.getOutOfGauge())) {
                                    bkgParagraph1.add(new Phrase(", "));
                                    bkgParagraph1.add(new Chunk(OUT_OF_GAUGE, GreenFont8));
                                }
                                if (null != bookingfclUnits.getSpecialEquipment()
                                        && !bookingfclUnits.getSpecialEquipment().equals("")) {
                                    bkgParagraph1.add(new Phrase(", "));
                                    bkgParagraph1
                                            .add(new Chunk(bookingfclUnits.getSpecialEquipment(), GreenFont8));
                                }
                                isFirst = false;
                            }
                        }
                        summaryMap.put(temp, temp);
                    }
                }
            }
        }
        return (bkgParagraph1);
    }
    return new Paragraph();
}

From source file:com.itext.test.FirstPdf.java

private static void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // ?/*  ww w . j a v a2  s  .c o m*/
    addEmptyLine(preface, 1);
    // 
    preface.add(new Paragraph("Title of the document", catFont));
    //?
    addEmptyLine(preface, 1);
    // ?: Report generated by: _name, _date
    preface.add(new Paragraph(
            "Report generated by: " + System.getProperty("user.name") + ", " + new java.util.Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));
    //?8
    addEmptyLine(preface, 8);
    //??
    preface.add(new Paragraph(
            "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));
    //?document
    document.add(preface);
    //
    document.newPage();
}

From source file:com.itext.test.FirstPdf.java

private static void addContent(Document document) throws DocumentException {
    ////ww  w .j  a v  a  2s  . c om
    //   Anchor anchor = new Anchor();
    //
    //anchor.setName("First Chapter");
    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(), 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"));

    //List
    createList(subCatPart);
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // Table
    createTable(subCatPart);

    // document
    document.add(catPart);

    // Next section

    // Second parameter is the number of the chapter
    catPart = new Chapter(new Paragraph("a"), 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);

}

From source file:com.jd.survey.web.pdf.StatisticsPdf.java

License:Open Source License

private void writeEntry(Document document, String label, String value) throws Exception {
    Paragraph questionParagraph = new Paragraph();
    questionParagraph.setLeading(14, 0);
    questionParagraph.setIndentationLeft(18);
    questionParagraph.add(new Chunk(label.trim() + ": ", boldedFont));
    questionParagraph.add(new Chunk(value == null ? "" : value.trim(), normalFont));
    document.add(questionParagraph);//from  w  w w. j  a  v  a2 s .  co  m
}

From source file:com.jd.survey.web.pdf.StatisticsPdf.java

License:Open Source License

private void writeEntry(Document document, String label) throws Exception {
    Paragraph questionParagraph = new Paragraph();
    questionParagraph.setLeading(14, 0);
    questionParagraph.setIndentationLeft(18);
    questionParagraph.add(new Chunk(label.trim(), boldedFont));
    document.add(questionParagraph);/*from   w w w  . j a  v  a2s.  c  o m*/
}

From source file:com.jd.survey.web.pdf.StatisticsPdf.java

License:Open Source License

private void writeEntry(Document document, String label, double value) throws Exception {
    Paragraph questionParagraph = new Paragraph();
    questionParagraph.setLeading(14, 0);
    questionParagraph.setIndentationLeft(18);
    questionParagraph.add(new Chunk(label.trim() + ": ", boldedFont));
    questionParagraph.add(new Chunk(
            BigDecimalValidator.getInstance().format(value, LocaleContextHolder.getLocale()), normalFont));
    document.add(questionParagraph);/*  w  w w . j  av a  2s .com*/
}