Search in sources :

Example 1 with Chunk

use of com.lowagie.text.Chunk in project Openfire by igniterealtime.

the class ConversationUtils method buildPDFContent.

private ByteArrayOutputStream buildPDFContent(Conversation conversation, Map<String, Font> colorMap) {
    Font roomEvent = FontFactory.getFont(FontFactory.HELVETICA, 12f, Font.ITALIC, new Color(0xFF, 0x00, 0xFF));
    try {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new PDFEventListener());
        document.open();
        Paragraph p = new Paragraph(LocaleUtils.getLocalizedString("archive.search.pdf.title", MonitoringConstants.NAME), FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
        document.add(p);
        document.add(Chunk.NEWLINE);
        ConversationInfo coninfo = new ConversationUtils().getConversationInfo(conversation.getConversationID(), false);
        String participantsDetail;
        if (coninfo.getAllParticipants() == null) {
            participantsDetail = coninfo.getParticipant1() + ", " + coninfo.getParticipant2();
        } else {
            participantsDetail = String.valueOf(coninfo.getAllParticipants().length);
        }
        Paragraph chapterTitle = new Paragraph(LocaleUtils.getLocalizedString("archive.search.pdf.participants", MonitoringConstants.NAME) + " " + participantsDetail, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD));
        document.add(chapterTitle);
        Paragraph startDate = new Paragraph(LocaleUtils.getLocalizedString("archive.search.pdf.startdate", MonitoringConstants.NAME) + " " + coninfo.getDate(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD));
        document.add(startDate);
        Paragraph duration = new Paragraph(LocaleUtils.getLocalizedString("archive.search.pdf.duration", MonitoringConstants.NAME) + " " + coninfo.getDuration(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD));
        document.add(duration);
        Paragraph messageCount = new Paragraph(LocaleUtils.getLocalizedString("archive.search.pdf.messagecount", MonitoringConstants.NAME) + " " + conversation.getMessageCount(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD));
        document.add(messageCount);
        document.add(Chunk.NEWLINE);
        Paragraph messageParagraph;
        for (ArchivedMessage message : conversation.getMessages()) {
            String time = JiveGlobals.formatTime(message.getSentDate());
            String from = message.getFromJID().getNode();
            if (conversation.getRoom() != null) {
                from = message.getToJID().getResource();
            }
            String body = message.getBody();
            String prefix;
            if (!message.isRoomEvent()) {
                prefix = "[" + time + "] " + from + ":  ";
                Font font = colorMap.get(message.getFromJID().toString());
                if (font == null) {
                    font = colorMap.get(message.getFromJID().toBareJID());
                }
                if (font == null) {
                    font = FontFactory.getFont(FontFactory.HELVETICA, 12f, Font.BOLD, Color.BLACK);
                }
                messageParagraph = new Paragraph(new Chunk(prefix, font));
            } else {
                prefix = "[" + time + "] ";
                messageParagraph = new Paragraph(new Chunk(prefix, roomEvent));
            }
            messageParagraph.add(body);
            messageParagraph.add(" ");
            document.add(messageParagraph);
        }
        document.close();
        return baos;
    } catch (DocumentException e) {
        Log.error("error creating PDF document: " + e.getMessage(), e);
        return null;
    }
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) Color(java.awt.Color) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Font(com.lowagie.text.Font) Paragraph(com.lowagie.text.Paragraph) DocumentException(com.lowagie.text.DocumentException)

Example 2 with Chunk

use of com.lowagie.text.Chunk in project mdw-designer by CenturyLinkCloud.

the class ExportHelper method generateElementHtml.

private Object generateElementHtml(Element element, int depth, Font font) {
    String tag = element.getName();
    Object myself;
    Object av;
    Font vFont = font;
    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();
                vFont = 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)
                    vFont = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
                else if (htmlattr.italic)
                    vFont = FontFactory.getFont(family, size, Font.ITALIC, color);
                else if (htmlattr.bold)
                    vFont = FontFactory.getFont(family, size, Font.BOLD);
            }
            myself = new Chunk(content, vFont);
        } catch (BadLocationException e) {
            e.printStackTrace();
            myself = null;
        }
    } else if (element instanceof HTMLDocument.BlockElement) {
        HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
        HtmlAttr htmlattr = printAttributesHtml(be);
        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)
                vFont = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
            else if (htmlattr.italic)
                vFont = FontFactory.getFont(family, size, Font.ITALIC, color);
            else if (htmlattr.bold)
                vFont = FontFactory.getFont(family, size, Font.BOLD, Color.blue);
        }
        if ("html".equalsIgnoreCase(tag)) {
            myself = generateElementChildrenHtml(element, depth + 1, vFont);
        } else if ("head".equalsIgnoreCase(tag)) {
            myself = null;
        } else if ("body".equalsIgnoreCase(tag)) {
            myself = generateElementChildrenHtml(element, depth + 1, vFont);
        } else if ("p".equalsIgnoreCase(tag) || "p-implied".equalsIgnoreCase(tag)) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            Paragraph paragraph = new Paragraph();
            paragraph.setFirstLineIndent(0F);
            for (Object child : children) {
                if (!(child instanceof Chunk))
                    paragraph.add(child);
            }
            if (align != null)
                paragraph.setAlignment(align);
            myself = paragraph;
        } else if ("h1".equalsIgnoreCase(tag) || "h2".equalsIgnoreCase(tag) || "h3".equalsIgnoreCase(tag)) {
            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 ("ul".equalsIgnoreCase(tag)) {
            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 ("ol".equalsIgnoreCase(tag)) {
            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 ("li".equalsIgnoreCase(tag)) {
            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 ("table".equalsIgnoreCase(tag)) {
            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 ("tr".equalsIgnoreCase(tag)) {
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            myself = children;
        } else if ("td".equalsIgnoreCase(tag)) {
            Cell cell = new Cell();
            List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
            for (Object child : children) {
                cell.add(child);
            }
            myself = cell;
        } else if ("div".equalsIgnoreCase(tag)) {
            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 {
        // could be BidiElement - not sure what it is
        myself = null;
    }
    return myself;
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Font(com.lowagie.text.Font) List(java.util.List) ArrayList(java.util.ArrayList) Cell(com.lowagie.text.Cell) DocxTable(com.centurylink.mdw.designer.utils.DocxBuilder.DocxTable) Table(com.lowagie.text.Table) BadElementException(com.lowagie.text.BadElementException) Color(java.awt.Color) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) ListItem(com.lowagie.text.ListItem) BadLocationException(javax.swing.text.BadLocationException)

Example 3 with Chunk

use of com.lowagie.text.Chunk in project mdw-designer by CenturyLinkCloud.

the class ExportHelper method printVariablesPdf.

private void printVariablesPdf(Chapter chapter, List<VariableVO> variables, int parentLevel) {
    Paragraph sTitle = new Paragraph("Process Variables", sectionFont);
    Section section = chapter.addSection(sTitle, parentLevel == 0 ? 0 : (parentLevel + 1));
    com.lowagie.text.List list = new com.lowagie.text.List(false, false, 10.0f);
    for (VariableVO var : variables) {
        Phrase phrase = new Phrase();
        phrase.add(new Chunk(var.getVariableName(), fixedWidthFont));
        String v = var.getVariableType();
        if (var.getDescription() != null)
            v += " (" + var.getDescription() + ")";
        phrase.add(new Chunk(": " + v + "\n", normalFont));
        list.add(new ListItem(phrase));
    }
    section.add(list);
}
Also used : VariableVO(com.centurylink.mdw.model.value.variable.VariableVO) List(java.util.List) ArrayList(java.util.ArrayList) Phrase(com.lowagie.text.Phrase) ListItem(com.lowagie.text.ListItem) Chunk(com.lowagie.text.Chunk) Section(com.lowagie.text.Section) Paragraph(com.lowagie.text.Paragraph)

Example 4 with Chunk

use of com.lowagie.text.Chunk in project drools by kiegroup.

the class EndPage method createContents.

public static List createContents(java.util.List<DrlRuleParser> rules) {
    List index = new List(true);
    for (DrlRuleParser drlRuleData : rules) {
        Chunk chunk = new Chunk(drlRuleData.getName());
        // chunk.setLocalGoto( item.getName() );
        ListItem listItem = new ListItem(chunk);
        index.add(listItem);
    }
    return index;
}
Also used : List(com.lowagie.text.List) ListItem(com.lowagie.text.ListItem) Chunk(com.lowagie.text.Chunk) DrlRuleParser(org.drools.verifier.misc.DrlRuleParser)

Example 5 with Chunk

use of com.lowagie.text.Chunk in project charts by vaadin.

the class PdfExportDemo method createHeaderCell.

private PdfPCell createHeaderCell(String caption) throws BadElementException {
    Chunk chunk = new Chunk(caption, captionFont);
    Paragraph p = new Paragraph(chunk);
    p.add(Chunk.NEWLINE);
    p.add(Chunk.NEWLINE);
    PdfPCell cell = new PdfPCell(p);
    cell.setBorder(0);
    cell.setBorderWidthBottom(1);
    cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    return cell;
}
Also used : PdfPCell(com.lowagie.text.pdf.PdfPCell) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph)

Aggregations

Chunk (com.lowagie.text.Chunk)13 Paragraph (com.lowagie.text.Paragraph)11 DocumentException (com.lowagie.text.DocumentException)5 ListItem (com.lowagie.text.ListItem)5 Color (java.awt.Color)5 List (java.util.List)5 Cell (com.lowagie.text.Cell)4 Section (com.lowagie.text.Section)4 ArrayList (java.util.ArrayList)4 Document (com.lowagie.text.Document)3 Font (com.lowagie.text.Font)3 Phrase (com.lowagie.text.Phrase)3 Table (com.lowagie.text.Table)3 PdfPCell (com.lowagie.text.pdf.PdfPCell)3 ExpressionException (cbit.vcell.parser.ExpressionException)2 DocxTable (com.centurylink.mdw.designer.utils.DocxBuilder.DocxTable)2 BadElementException (com.lowagie.text.BadElementException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BadLocationException (javax.swing.text.BadLocationException)2 HTMLDocument (javax.swing.text.html.HTMLDocument)2