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(Phrase phrase) 

Source Link

Document

Constructs a Paragraph with a certain Phrase.

Usage

From source file:FirstPdf.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. ja  va 2  s . co  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);

}

From source file:FirstPdf.java

private static void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
        paragraph.add(new Paragraph(" "));
    }/*from  w  ww.  jav a 2s . c  om*/
}

From source file:ITextHelloWorld.java

License:Open Source License

public ITextHelloWorld() throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
    document.open();//from  w  w w . j a  va  2s. co m
    document.add(new Paragraph("Hello World"));
    document.close();

}

From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java

License:Open Source License

public void xml2pdf(InputSource xml, OutputStream pdf) {

    try {/*from   w  w w. j a va2s .c o m*/
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);

        PdfWriter writer = PdfWriter.getInstance(document, pdf);
        //writer.setViewerPreferences(PdfWriter.HideMenubar| PdfWriter.HideToolbar);
        writer.setViewerPreferences(PdfWriter.PageModeUseThumbs | PdfWriter.PageModeUseOutlines);
        //PdfOutline root = writer.getDirectContent().getRootOutline();
        //new PdfOutline(root, new PDFAction("http://nina.acad.bg/qmrf"), "a bookmark");

        //writer.addFileAttachment(arg0, arg1, arg2, arg3)

        if (docBuilder == null)
            docBuilder = docBuilderFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new SimpleErrorHandler(getClass().getName()));
        QMRFSchemaResolver resolver = new QMRFSchemaResolver("http://ambit.acad.bg/qmrf/qmrf.dtd", null);
        resolver.setIgnoreSystemID(true);
        docBuilder.setEntityResolver(resolver);

        org.w3c.dom.Document doc = null;
        try {
            doc = docBuilder.parse(xml);
        } catch (Exception x) {
            document.addCreationDate();
            document.addCreator(getClass().getName());
            document.open();
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        document.addCreationDate();
        document.addCreator(getClass().getName());
        document.addKeywords(replaceTags(findNodeValue("keywords", doc)));
        document.addTitle(replaceTags(findNodeValue("QSAR_title", doc)));

        try {
            NodeList info = doc.getElementsByTagName("QMRF");
            for (int i = 0; i < info.getLength(); i++)
                document.addSubject(findAttributeValue("name", info.item(i)) + '.'
                        + findAttributeValue("version", info.item(i)));
        } catch (Exception x) {
            document.addSubject("QMRF");
        }
        try {
            document.addAuthor(listNodeAttributes(doc, "qmrf_authors", "author_ref", "author", att_author,
                    new Boolean(true)));
        } catch (Exception x) {
            document.addAuthor(getClass().getName());
        }

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        try {
            headerTable(document, doc);
        } catch (Exception x) {
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        PdfOutline root = writer.getDirectContent().getRootOutline();

        for (int i = 0; i < subchapters.length; i++)

            try {
                int align = Paragraph.ALIGN_LEFT;
                if (Mode.chapter == (Mode) subchapters[i][2]) {
                    document.add(new Paragraph(new Chunk('\n')));
                    PdfPTable table = new PdfPTable(1);

                    table.setWidthPercentage(100);

                    StringBuffer b = new StringBuffer();
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                    b.append('.');
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                    String bookmark = b.toString();

                    Chunk title = new Chunk(bookmark);
                    title.setLocalDestination(bookmark);
                    title.setFont(bfont);

                    PdfDestination destination = new PdfDestination(PdfDestination.FITH);
                    PdfOutline outline = new PdfOutline(root, destination, bookmark);

                    Paragraph p = new Paragraph(title);

                    PdfPCell cell = new PdfPCell(p);
                    cell.setBackgroundColor(chapterColor);
                    table.addCell(cell);
                    document.add(table);
                    float pos = writer.getVerticalPosition(false);
                    if (pos < 90)
                        document.newPage();
                } else {
                    Phrase phrase = new Phrase();
                    switch ((Mode) subchapters[i][2]) {
                    case title: {
                        StringBuffer b = new StringBuffer();
                        String cn = findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter,
                                doc);
                        if (cn == null)
                            break;

                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                        b.append('.');
                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                        String subchapterBookmark = b.toString();
                        b.append(':');

                        Chunk title = new Chunk(b.toString());
                        title.setLocalDestination(subchapterBookmark);
                        title.setFont(bfont);
                        phrase.add(title);

                        PdfDestination destination = new PdfDestination(PdfDestination.FITBH);
                        PdfOutline outline = new PdfOutline(root, destination, subchapterBookmark);

                        break;

                    }
                    case text: {
                        createNodePhrase(subchapters[i][0].toString(), doc, phrase, font);
                        align = Paragraph.ALIGN_JUSTIFIED;
                        break;
                    }
                    case answer: {
                        String a = findAnswer(subchapters[i][0].toString(), doc);
                        if (a != null) {
                            Chunk answer = new Chunk(a);
                            answer.setFont(font);
                            phrase.add(answer);
                        }
                        break;
                    }
                    case dataset: {
                        StringBuffer b = new StringBuffer();
                        b.append(findDataAvailable(subchapters[i][0].toString(), doc));
                        Chunk dataset = new Chunk(b.toString());
                        dataset.setFont(font);
                        phrase.add(dataset);
                        break;
                    }
                    case attachments: {
                        PdfPTable table = getAttachmentsAsTable(doc, "attachment_training_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Training set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_validation_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Test set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_documents");
                        if (table != null) {
                            phrase.add(new Paragraph("Supporting information"));
                            phrase.add(table);
                        }
                        break;
                        /*
                        StringBuffer b = new StringBuffer();
                        b.append("Training set(s)\n");
                         b.append(listAttachments(doc,"attachment_training_data"));
                         b.append("Test set(s)\n");
                         b.append(listAttachments(doc,"attachment_validation_data"));
                         b.append("Supporting information\n");
                         b.append(listAttachments(doc,"attachment_documents"));
                        Chunk attachments = new Chunk(b.toString());
                        attachments.setFont(font);
                        phrase.add(attachments);
                                
                        break;
                        */
                    }
                    case reference: {
                        try {
                            String value = listNodeAttributes(doc, subchapters[i][0].toString(),
                                    subchapters[i][3].toString(), subchapters[i][4].toString(),
                                    (String[]) subchapters[i][5], (Boolean) subchapters[i][6]);

                            Chunk reference = new Chunk(value);
                            reference.setFont(font);
                            align = Paragraph.ALIGN_JUSTIFIED;
                            phrase.add(reference);
                        } catch (Exception x) {

                        }
                        break;

                    }
                    }

                    Paragraph p = new Paragraph(phrase);
                    p.setAlignment(align);
                    document.add(p);
                    float pos = writer.getVerticalPosition(false);
                    /*
                    cb.moveTo(0, pos);
                    cb.lineTo(PageSize.A4.width(), pos);
                    cb.stroke();
                    */
                    if (pos < 90)
                        document.newPage();
                }
            } catch (Exception x) {
                x.printStackTrace();
            }

        document.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
}

From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java

License:Open Source License

protected PdfPTable getAttachmentsAsTable(org.w3c.dom.Document doc, String attachments) {
    NodeList nodes = doc.getElementsByTagName(attachments);
    if (nodes.getLength() > 0) {

        float[] widths = { 3f, 3f };
        PdfPTable table = new PdfPTable(widths);
        table.setWidthPercentage(100);//from   w ww  .  jav  a  2  s . co m
        for (int i = 0; i < nodes.getLength(); i++)
            if (nodes.item(i) instanceof org.w3c.dom.Element) {
                NodeList attachment = nodes.item(i).getChildNodes();
                if (attachment.getLength() == 0)
                    ;//b.append("N/A\n");
                else
                    for (int j = 0; j < attachment.getLength(); j++)
                        if (attachment.item(j) instanceof org.w3c.dom.Element) {
                            org.w3c.dom.Element e = ((org.w3c.dom.Element) attachment.item(j));

                            Paragraph p = new Paragraph(org.apache.commons.lang.StringEscapeUtils
                                    .unescapeXml(e.getAttribute("description")));
                            PdfPCell cell = new PdfPCell(p);
                            cell.setBackgroundColor(Color.white);
                            table.addCell(cell);

                            String c = org.apache.commons.lang.StringEscapeUtils
                                    .unescapeXml(e.getAttribute("url"));

                            PdfAction action = new PdfAction(c);
                            p = new Paragraph();
                            p.add(new Chunk(c).setAction(action));
                            cell = new PdfPCell(p);
                            cell.setBackgroundColor(Color.white);
                            table.addCell(cell);

                        }
            }
        return table;
    } else
        return null;

}

From source file:ambit.io.PDFWriter.java

License:Open Source License

@Override
protected void writeHeader() throws IOException {

    PdfPCell cell = new PdfPCell(new Paragraph("Structure"));
    cell.setBackgroundColor(Color.white);
    table.addCell(cell);/*from   www.j  a v  a2 s . c o  m*/

    cell = new PdfPCell(new Paragraph("Properties"));
    cell.setBackgroundColor(Color.white);
    table.addCell(cell);

}

From source file:ambit.io.PDFWriter.java

License:Open Source License

public void writeMolecule(IMolecule molecule) {
    Object value;/*  w w w . ja  v  a  2 s  .co m*/

    try {

        //give it a chance to create a header just before the first write
        if (!writingStarted) {
            if (header == null)
                setHeader(molecule.getProperties());
            writeHeader();
            writingStarted = true;
        }

        Paragraph p = new Paragraph("");
        String s;
        for (int i = 0; i < header.size(); i++) {
            StringBuffer b = new StringBuffer();
            b.append(header.getList().get(i));
            b.append(" = ");
            value = molecule.getProperty(header.getList().get(i));
            if (i == smilesIndex) {

                if (value == null) //no SMILES available
                    try {
                        value = ""; //sg.createSMILES(molecule);
                    } catch (Exception x) {
                        logger.error("Error while createSMILES\t", x.getMessage());
                        value = "";
                    }
            }
            if (value != null) {
                if (value instanceof Number) {
                    s = value.toString();
                } else {
                    s = value.toString();

                }

                b.append(s);
                p.add(new Paragraph(b.toString()));
            }

        }
        BufferedImage image = imageTools.getImage(molecule);
        image.flush();

        Image png_struc = Image.getInstance(image, Color.white);
        png_struc.setAlignment(Image.LEFT);
        png_struc.scalePercent(100);

        PdfPCell cell = new PdfPCell();
        cell.setMinimumHeight(cell.height());
        cell.addElement(png_struc);
        table.addCell(cell);

        cell = new PdfPCell(p);
        cell.setBackgroundColor(Color.white);
        table.addCell(cell);

    } catch (Exception x) {
        logger.error("ERROR while writing Molecule: ", x.getMessage());
        logger.debug(x);
        x.printStackTrace();
    }

}

From source file:ambit2.core.io.PDFWriter.java

License:Open Source License

public void writeMolecule(IMolecule molecule) {
    Object value;//from w  w  w  .  j a  v  a  2  s.co m

    try {

        //give it a chance to create a header just before the first write
        if (!writingStarted) {
            if (header == null)
                setHeader(molecule.getProperties());
            writeHeader();
            writingStarted = true;
        }

        Paragraph p = new Paragraph("");
        String s;
        for (int i = 0; i < header.size(); i++) {
            StringBuffer b = new StringBuffer();
            b.append(header.get(i));
            b.append(" = ");
            value = molecule.getProperty(header.get(i));
            if (i == smilesIndex) {

                if (value == null) //no SMILES available
                    try {
                        value = ""; //sg.createSMILES(molecule);
                    } catch (Exception x) {
                        logger.log(Level.WARNING, "Error while createSMILES\t", x);
                        value = "";
                    }
            }
            if (value != null) {
                if (value instanceof Number) {
                    s = value.toString();
                } else {
                    s = value.toString();

                }

                b.append(s);
                p.add(new Paragraph(b.toString()));
            }

        }
        BufferedImage image = imageTools.getImage(molecule);
        image.flush();

        Image png_struc = Image.getInstance(image, Color.white);
        png_struc.setAlignment(Image.LEFT);
        png_struc.scalePercent(100);

        PdfPCell cell = new PdfPCell();
        cell.setMinimumHeight(cell.height());
        cell.addElement(png_struc);
        table.addCell(cell);

        cell = new PdfPCell(p);
        cell.setBackgroundColor(Color.white);
        table.addCell(cell);

    } catch (Exception x) {
        logger.log(Level.SEVERE, "Error while writing molecule", x);
    }

}

From source file:ambit2.data.qmrf.Qmrf_Xml_Pdf.java

License:Open Source License

public void xml2pdf(InputSource xml, OutputStream pdf) {

    try {/*from ww w  .  ja va2  s  . com*/
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);

        PdfWriter writer = PdfWriter.getInstance(document, pdf);
        //writer.setViewerPreferences(PdfWriter.HideMenubar| PdfWriter.HideToolbar);
        writer.setViewerPreferences(PdfWriter.PageModeUseThumbs | PdfWriter.PageModeUseOutlines);
        //PdfOutline root = writer.getDirectContent().getRootOutline();
        //new PdfOutline(root, new PDFAction("http://nina.acad.bg/qmrf"), "a bookmark");

        //writer.addFileAttachment(arg0, arg1, arg2, arg3)

        if (docBuilder == null)
            docBuilder = docBuilderFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new SimpleErrorHandler(getClass().getName()));
        QMRFSchemaResolver resolver = new QMRFSchemaResolver("http://ambit2.acad.bg/qmrf/qmrf.dtd", null);
        resolver.setIgnoreSystemID(true);
        docBuilder.setEntityResolver(resolver);

        org.w3c.dom.Document doc = null;
        try {
            doc = docBuilder.parse(xml);
        } catch (Exception x) {
            document.addCreationDate();
            document.addCreator(getClass().getName());
            document.open();
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        document.addCreationDate();
        document.addCreator(getClass().getName());
        document.addKeywords(replaceTags(findNodeValue("keywords", doc)));
        document.addTitle(replaceTags(findNodeValue("QSAR_title", doc)));

        try {
            NodeList info = doc.getElementsByTagName("QMRF");
            for (int i = 0; i < info.getLength(); i++)
                document.addSubject(findAttributeValue("name", info.item(i)) + '.'
                        + findAttributeValue("version", info.item(i)));
        } catch (Exception x) {
            document.addSubject("QMRF");
        }
        try {
            document.addAuthor(listNodeAttributes(doc, "qmrf_authors", "author_ref", "author", att_author,
                    new Boolean(true)));
        } catch (Exception x) {
            document.addAuthor(getClass().getName());
        }

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        try {
            headerTable(document, doc);
        } catch (Exception x) {
            document.add(new Paragraph(new Chunk(x.getMessage())));
            document.close();
            return;
        }

        PdfOutline root = writer.getDirectContent().getRootOutline();

        for (int i = 0; i < subchapters.length; i++)

            try {
                int align = Paragraph.ALIGN_LEFT;
                if (Mode.chapter == (Mode) subchapters[i][2]) {
                    document.add(new Paragraph(new Chunk('\n')));
                    PdfPTable table = new PdfPTable(1);

                    table.setWidthPercentage(100);

                    StringBuffer b = new StringBuffer();
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                    b.append('.');
                    b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                    String bookmark = b.toString();

                    Chunk title = new Chunk(bookmark);
                    title.setLocalDestination(bookmark);
                    title.setFont(bfont);

                    PdfDestination destination = new PdfDestination(PdfDestination.FITH);
                    PdfOutline outline = new PdfOutline(root, destination, bookmark);

                    Paragraph p = new Paragraph(title);

                    PdfPCell cell = new PdfPCell(p);
                    cell.setBackgroundColor(chapterColor);
                    table.addCell(cell);
                    document.add(table);
                    float pos = writer.getVerticalPosition(false);
                    if (pos < 90)
                        document.newPage();
                } else {
                    Phrase phrase = new Phrase();
                    switch ((Mode) subchapters[i][2]) {
                    case title: {
                        StringBuffer b = new StringBuffer();
                        String cn = findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter,
                                doc);
                        if (cn == null)
                            break;

                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc));
                        b.append('.');
                        b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc));

                        String subchapterBookmark = b.toString();
                        b.append(':');

                        Chunk title = new Chunk(b.toString());
                        title.setLocalDestination(subchapterBookmark);
                        title.setFont(bfont);
                        phrase.add(title);

                        PdfDestination destination = new PdfDestination(PdfDestination.FITBH);
                        PdfOutline outline = new PdfOutline(root, destination, subchapterBookmark);

                        break;

                    }
                    case text: {
                        createNodePhrase(subchapters[i][0].toString(), doc, phrase, font);
                        align = Paragraph.ALIGN_JUSTIFIED;
                        break;
                    }
                    case answer: {
                        String a = findAnswer(subchapters[i][0].toString(), doc);
                        if (a != null) {
                            Chunk answer = new Chunk(a);
                            answer.setFont(font);
                            phrase.add(answer);
                        }
                        break;
                    }
                    case dataset: {
                        StringBuffer b = new StringBuffer();
                        b.append(findDataAvailable(subchapters[i][0].toString(), doc));
                        Chunk dataset = new Chunk(b.toString());
                        dataset.setFont(font);
                        phrase.add(dataset);
                        break;
                    }
                    case attachments: {
                        PdfPTable table = getAttachmentsAsTable(doc, "attachment_training_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Training set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_validation_data");
                        if (table != null) {
                            phrase.add(new Paragraph("Test set(s)"));
                            phrase.add(table);
                        }
                        table = getAttachmentsAsTable(doc, "attachment_documents");
                        if (table != null) {
                            phrase.add(new Paragraph("Supporting information"));
                            phrase.add(table);
                        }
                        break;
                        /*
                        StringBuffer b = new StringBuffer();
                        b.append("Training set(s)\n");
                         b.append(listAttachments(doc,"attachment_training_data"));
                         b.append("Test set(s)\n");
                         b.append(listAttachments(doc,"attachment_validation_data"));
                         b.append("Supporting information\n");
                         b.append(listAttachments(doc,"attachment_documents"));
                        Chunk attachments = new Chunk(b.toString());
                        attachments.setFont(font);
                        phrase.add(attachments);
                                
                        break;
                        */
                    }
                    case reference: {
                        try {
                            String value = listNodeAttributes(doc, subchapters[i][0].toString(),
                                    subchapters[i][3].toString(), subchapters[i][4].toString(),
                                    (String[]) subchapters[i][5], (Boolean) subchapters[i][6]);

                            Chunk reference = new Chunk(value);
                            reference.setFont(font);
                            align = Paragraph.ALIGN_JUSTIFIED;
                            phrase.add(reference);
                        } catch (Exception x) {

                        }
                        break;

                    }
                    }

                    Paragraph p = new Paragraph(phrase);
                    p.setAlignment(align);
                    document.add(p);
                    float pos = writer.getVerticalPosition(false);
                    /*
                    cb.moveTo(0, pos);
                    cb.lineTo(PageSize.A4.width(), pos);
                    cb.stroke();
                    */
                    if (pos < 90)
                        document.newPage();
                }
            } catch (Exception x) {
                x.printStackTrace();
            }

        document.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
}

From source file:ambit2.db.reporters.PDFReporter.java

protected void writeHeader(Document writer) throws IOException {
    String[] tops = new String[] { "Structure", "Properties" };
    for (String top : tops) {
        Chunk chunk = new Chunk(top);
        chunk.setFont(font);/*from  ww w .  j  ava  2s  .co  m*/
        PdfPCell cell = new PdfPCell(new Paragraph(chunk));
        cell.setBackgroundColor(Color.white);
        table.addCell(cell);
    }

}