Example usage for com.lowagie.text Paragraph add

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

Introduction

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

Prototype

public boolean add(Object o) 

Source Link

Document

Adds an Object to the Paragraph.

Usage

From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java

License:Open Source License

private static boolean parseLine(WikiPDFContext context, String line, Paragraph main, Connection db,
        ArrayList<Integer> wikiListTodo, float cellWidth, PdfPCell cell) throws Exception {

    LOG.debug("PARSING LINE: " + line);

    // Context Objects
    Project project = context.getProject();
    WikiExportBean exportBean = context.getExportBean();
    HashMap<String, ImageInfo> imageList = context.getImageList();
    String fileLibrary = context.getFileLibrary();

    boolean needsCRLF = true;
    boolean bold = false;
    boolean italic = false;
    boolean bolditalic = false;
    StringBuffer subject = new StringBuffer();
    StringBuffer data = new StringBuffer();
    int linkL = 0;
    int linkR = 0;
    int attr = 0;
    int underlineAttr = 0;

    // parse characters
    for (int i = 0; i < line.length(); i++) {
        char c1 = line.charAt(i);
        String c = String.valueOf(c1);
        // False attr/links
        if (!"'".equals(c) && attr == 1) {
            data.append("'").append(c);
            attr = 0;/*from  ww  w .  j  a  v a 2s  .c om*/
            continue;
        }
        if (!"_".equals(c) && underlineAttr == 1) {
            data.append("_").append(c);
            underlineAttr = 0;
            continue;
        }
        if (!"[".equals(c) && linkL == 1) {
            data.append("[").append(c);
            linkL = 0;
            continue;
        }
        if (!"]".equals(c) && linkR == 1) {
            data.append("]").append(c);
            linkR = 0;
            continue;
        }
        // Links
        if ("[".equals(c)) {
            ++linkL;
            continue;
        }
        if ("]".equals(c)) {
            ++linkR;
            if (linkL == 2 && linkR == 2) {
                LOG.debug("main.add(new Chunk(data.toString()))");
                main.add(new Chunk(data.toString()));
                data.setLength(0);
                // Different type of links...
                String link = subject.toString().trim();
                if (link.startsWith("Image:") || link.startsWith("image:")) {
                    // @note From WikiImageLink.java
                    String image = link.substring(6);
                    String title = null;
                    int frame = -1;
                    int thumbnail = -1;
                    int left = -1;
                    int right = -1;
                    int center = -1;
                    int none = -1;
                    int imageLink = -1;
                    int alt = -1;
                    if (image.indexOf("|") > 0) {
                        // the image is first
                        image = image.substring(0, image.indexOf("|"));
                        // any directives are next
                        frame = link.indexOf("|frame");
                        thumbnail = link.indexOf("|thumb");
                        left = link.indexOf("|left");
                        right = link.indexOf("|right");
                        center = link.indexOf("|center");
                        none = link.indexOf("|none");
                        imageLink = link.indexOf("|link=");
                        alt = link.indexOf("|alt=");
                        // the optional caption is last
                        int last = link.lastIndexOf("|");
                        if (last > frame && last > thumbnail && last > left && last > right && last > center
                                && last > none && last > imageLink && last > alt) {
                            title = link.substring(last + 1);
                        }
                    }

                    //A picture, including alternate text:
                    //[[Image:Wiki.png|The logo for this Wiki]]

                    //You can put the image in a frame with a caption:
                    //[[Image:Wiki.png|frame|The logo for this Wiki]]

                    // Access some image details
                    int width = 0;
                    int height = 0;
                    ImageInfo imageInfo = imageList.get(image + (thumbnail > -1 ? "-TH" : ""));
                    if (imageInfo != null) {
                        width = imageInfo.getWidth();
                        height = imageInfo.getHeight();
                    }

                    // Alt
                    String altText = null;
                    if (alt > -1) {
                        int startIndex = alt + 4;
                        int endIndex = link.indexOf("|", startIndex);
                        if (endIndex == -1) {
                            endIndex = link.length();
                        }
                        altText = link.substring(startIndex, endIndex);
                    }

                    // Looks like the image needs a link (which is always last)
                    String url = null;
                    if (imageLink > -1) {
                        // Get the entered link
                        int startIndex = imageLink + 6;
                        int endIndex = link.length();
                        String href = link.substring(startIndex, endIndex);

                        // Treat as a wikiLink to validate and to create a proper url
                        WikiLink wikiLink = new WikiLink(project.getId(),
                                (altText != null ? href + " " + altText : href));
                        url = wikiLink.getUrl("");
                        if (!url.startsWith("http://") && !url.startsWith("https://")) {
                            // @todo Use a local link
                            // @todo Use an external link
                        }
                    }

                    // Determine if local or external image
                    if ((image.startsWith("http://") || image.startsWith("https://"))) {
                        // retrieve external image
                        String imageUrl = null;
                        try {
                            URL imageURL = new URL(image);
                            imageUrl = image;
                        } catch (Exception e) {

                        }
                    } else {
                        // local image
                        try {
                            // @todo image alignment and links
                            Image thisImage = Image.getInstance(
                                    getImageFilename(db, fileLibrary, project, image, (thumbnail > -1)));

                            LOG.debug("Drawing image for area: " + cellWidth);

                            if (cellWidth > 0f) {
                                LOG.debug(" Image is embedded in cell");
                                // Guess the size of the cell
                                float cellPixels = (500f * (cellWidth / 100f));
                                if (cellPixels > 0f && (float) width > cellPixels) {
                                    // Shrink image to fit the cell
                                    LOG.debug(" Scaling to fit");
                                    thisImage.scaleToFit(cellPixels, 500f);
                                } else {
                                    // Align image to left instead of scaling it to fit
                                    thisImage.setAlignment(Image.LEFT);
                                }
                                LOG.debug("cell.addElement(thisImage)");
                                cell.addElement(thisImage);
                            } else {
                                LOG.debug(" Image is inline");
                                if (width > 500) {
                                    LOG.debug(" Scaling to fit");
                                    thisImage.scaleToFit(500f, 500f);
                                }
                                LOG.debug("main.add(thisImage)");
                                main.add(thisImage);
                            }

                            //                thisImage.setAlignment();
                            //                thisImage.Alignment = Image.TEXTWRAP | Image.ALIGN_RIGHT;

                            //                main.add(thisImage);
                        } catch (FileNotFoundException fnfe) {
                            LOG.warn("WikiPDFUtils-> Image was not found in the FileLibrary ("
                                    + getImageFilename(db, fileLibrary, project, image, (thumbnail > -1))
                                    + ")... will continue.");
                        }
                    }

                    /*
                    if (frame > -1 || thumbnail > -1) {
                      sb.append("</div>");
                      sb.append("<div id=\"caption\" style=\"margin-bottom: 5px; margin-left: 5px; margin-right: 5px; text-align: left;\">");
                    }
                    if (thumbnail > -1) {
                      sb.append("<div style=\"float:right\"><a target=\"_blank\" href=\"ProjectManagementWiki.do?command=Img&pid=" + wiki.getProjectId() + "&subject=" + StringUtils.replace(StringUtils.jsEscape(image), "%20", "+") + "\"><img src=\"images/magnify-clip.png\" width=\"15\" height=\"11\" alt=\"Enlarge\" border=\"0\" /></a></div>");
                    }
                    if (frame > -1 || thumbnail > -1) {
                      if (title != null) {
                        sb.append(StringUtils.toHtml(title));
                      }
                      sb.append(
                          "  </div>\n" +
                              "</div>");
                    }
                    */
                    /*
                    if (none > -1) {
                      sb.append("<br clear=\"all\">");
                    }
                    */
                    if (i + 1 == line.length() && (right > -1 || left > -1) || none > -1) {
                        needsCRLF = false;
                    }
                } else {
                    // This is most likely a Wiki link
                    String title = subject.toString().trim();
                    if (link.indexOf("|") > 0) {
                        link = link.substring(0, link.indexOf("|")).trim();
                        title = title.substring(title.indexOf("|") + 1);
                    }
                    if (link.indexOf("http://") > -1 || link.indexOf("https://") > -1) {
                        String label = link;
                        if (link.indexOf(" ") > 0) {
                            label = link.substring(link.indexOf(" ") + 1);
                            link = link.substring(0, link.indexOf(" "));
                        }
                        Anchor anchor1 = new Anchor(label, FontFactory.getFont(FontFactory.HELVETICA, 12,
                                Font.UNDERLINE, new Color(0, 0, 255)));
                        anchor1.setReference(link);
                        anchor1.setName("top");
                        main.add(anchor1);
                    } else {
                        // Place a wiki link
                        if (exportBean.getFollowLinks()) {
                            // See if target link exists before creating a link to it
                            int linkedWikiId = -1;
                            if (StringUtils.hasText(title) && !title.startsWith("|")) {
                                Wiki subwiki = WikiList.queryBySubject(db, title, project.getId());
                                if (subwiki.getId() > -1) {
                                    linkedWikiId = subwiki.getId();
                                }
                            }
                            // Display the linked item
                            if (linkedWikiId > -1) {
                                // Display as an anchor
                                Anchor linkToWiki = new Anchor(title, FontFactory.getFont(FontFactory.HELVETICA,
                                        12, Font.NORMAL, new Color(0, 0, 255)));
                                linkToWiki.setReference("#" + title.toLowerCase());
                                LOG.debug("Link to: #" + title.toLowerCase());
                                main.add(linkToWiki);
                                LOG.debug(" main.add(linkToWiki)");
                                //                  main.add(new Chunk(title, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255))).setLocalGoto(link));
                                // Add this wiki to the to do list...
                                if (!wikiListTodo.contains(linkedWikiId)) {
                                    wikiListTodo.add(linkedWikiId);
                                }
                            } else {
                                // Display without the link
                                main.add(new Chunk(title, FontFactory.getFont(FontFactory.HELVETICA, 12,
                                        Font.NORMAL, new Color(0, 0, 255))));
                            }
                        } else {
                            // Not following links, so display... perhaps as an external link someday
                            main.add(new Chunk(title, FontFactory.getFont(FontFactory.HELVETICA, 12,
                                    Font.NORMAL, new Color(0, 0, 255))));
                        }
                    }
                }
                subject.setLength(0);
                linkL = 0;
                linkR = 0;
            }
            continue;
        }
        if (!"[".equals(c) && linkL == 2 && !"]".equals(c)) {
            subject.append(c);
            continue;
        }
        // Attribute properties
        if ("'".equals(c)) {
            ++attr;
            continue;
        }
        if (!"'".equals(c) && attr > 1) {
            if (attr == 2) {
                if (!italic) {
                    main.add(new Chunk(data.toString()));
                    data.setLength(0);
                    data.append(c);
                    italic = true;
                } else {
                    data.append(c);
                    main.add(new Chunk(data.toString(),
                            FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC, new Color(0, 0, 0))));
                    data.setLength(0);
                    italic = false;
                }
                attr = 0;
                continue;
            }
            if (attr == 3) {
                if (!bold) {
                    main.add(new Chunk(data.toString()));
                    data.setLength(0);
                    data.append(c);
                    bold = true;
                } else {
                    data.append(c);
                    main.add(new Chunk(data.toString(),
                            FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(0, 0, 0))));
                    data.setLength(0);
                    bold = false;
                }
                attr = 0;
                continue;
            }
            if (attr == 5) {
                if (!bolditalic) {
                    main.add(new Chunk(data.toString()));
                    data.setLength(0);
                    data.append(c);
                    bolditalic = true;
                } else {
                    data.append(c);
                    main.add(new Chunk(data.toString(), FontFactory.getFont(FontFactory.HELVETICA, 12,
                            Font.BOLDITALIC, new Color(0, 0, 0))));
                    data.setLength(0);
                    bolditalic = false;
                }
                attr = 0;
                continue;
            }
        }
        data.append(c);
    }
    for (int x = 0; x < linkR; x++) {
        data.append("]");
    }
    for (int x = 0; x < linkL; x++) {
        data.append("[");
    }
    if (attr == 1) {
        data.append("'");
    }
    if (italic) {
        main.add(new Chunk(data.toString(),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC, new Color(0, 0, 0))));
    } else if (bold) {
        main.add(new Chunk(data.toString(),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(0, 0, 0))));
    } else if (bolditalic) {
        main.add(new Chunk(data.toString(),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLDITALIC, new Color(0, 0, 0))));
    } else {
        main.add(new Chunk(data.toString()));
    }
    data.setLength(0);
    return needsCRLF;
}

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);// www .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();// w w w.  j ava2s.  c  o  m

    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));
                                }//from  w  ww. j a  v  a  2 s .co  m
                                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();
    // ?/*from  w  ww . j  a va  2 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.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  www . ja  v a2s .  c  om*/
}

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  ww  . j a va 2s  .  com*/
}

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 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, Date value, String dateFormat) throws Exception {
    Paragraph questionParagraph = new Paragraph();
    questionParagraph.setLeading(14, 0);
    questionParagraph.setIndentationLeft(18);
    questionParagraph.add(new Chunk(label.trim() + ": ", boldedFont));
    questionParagraph.add(new Chunk(DateValidator.getInstance().format(value, dateFormat), normalFont));
    document.add(questionParagraph);/* ww w  .j a v a2  s. c o m*/
}

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

License:Open Source License

private void writeCurrencyEntry(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(
            CurrencyValidator.getInstance().format(value, LocaleContextHolder.getLocale()), normalFont));
    document.add(questionParagraph);//from w  ww.  j  a  v a 2s. c o m
}