Example usage for com.lowagie.text Chunk Chunk

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

Introduction

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

Prototype

public Chunk(DrawInterface separator, float tabPosition, boolean newline) 

Source Link

Document

Creates a tab Chunk.

Usage

From source file:net.bull.javamelody.internal.web.pdf.PdfSessionInformationsReport.java

License:Apache License

private void writeCountry(SessionInformations session) throws IOException, BadElementException {
    final String country = session.getCountry();
    if (country == null) {
        addCell("");
    } else {//w  ww . j a  va2  s  . co m
        final String fileName = "flags/" + country + ".gif";
        final Image image = getImageByFileName(fileName);
        if (image == null) {
            addCell(country);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }
}

From source file:net.bull.javamelody.internal.web.pdf.PdfSessionInformationsReport.java

License:Apache License

private void writeBrowserAndOs(SessionInformations session) throws IOException, BadElementException {
    final String browser = session.getBrowser();
    if (browser == null) {
        addCell("");
    } else {/*from w w w.j a va2 s .  c  o m*/
        final String browserIconName = HtmlSessionInformationsReport.getBrowserIconName(browser);
        final String fileName = "browsers/" + browserIconName;
        final Image image = getImageByFileName(fileName);
        if (image == null) {
            addCell(browser);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }

    final String os = session.getOs();
    if (os == null) {
        addCell("");
    } else {
        final String osIconName = HtmlSessionInformationsReport.getOSIconName(os);
        final String fileName = "servers/" + osIconName;
        final Image image = getImageByFileName(fileName);
        if (image == null) {
            addCell(os);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }
}

From source file:net.bull.javamelody.internal.web.pdf.PdfThreadInformationsReport.java

License:Apache License

private void writeThreadInformations(ThreadInformations threadInformations)
        throws DocumentException, IOException {
    final PdfPCell defaultCell = getDefaultCell();
    defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
    addCell(threadInformations.getName());
    defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    if (threadInformations.isDaemon()) {
        addCell(getString("oui"));
    } else {/*w ww  .ja  v  a 2s  . c  o m*/
        addCell(getString("non"));
    }
    defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    addCell(integerFormat.format(threadInformations.getPriority()));
    defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
    final PdfPCell cell = new PdfPCell();
    final Paragraph paragraph = new Paragraph(getDefaultCell().getLeading() + cellFont.getSize());
    paragraph.add(new Chunk(
            getImage("bullets/" + HtmlThreadInformationsReport.getStateIcon(threadInformations)), 0, -1));
    paragraph.add(new Phrase(String.valueOf(threadInformations.getState()), cellFont));
    cell.addElement(paragraph);
    addCell(cell);
    if (stackTraceEnabled) {
        addCell(threadInformations.getExecutedMethod());
    }
    if (cpuTimeEnabled) {
        defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        addCell(integerFormat.format(threadInformations.getCpuTimeMillis()));
        addCell(integerFormat.format(threadInformations.getUserTimeMillis()));
    }
}

From source file:net.bull.javamelody.PdfCoreReport.java

License:Apache License

private void writeGraphs(Collection<JRobin> jrobins, Map<String, byte[]> mySmallGraphs)
        throws IOException, DocumentException {
    if (collector.isStopped()) {
        // pas de graphs, ils seraient en erreur sans timer
        // mais un message d'avertissement  la place
        final String message = getString("collect_server_misusage");
        final Paragraph jrobinParagraph = new Paragraph(message, PdfFonts.BOLD.getFont());
        jrobinParagraph.setAlignment(Element.ALIGN_CENTER);
        addToDocument(jrobinParagraph);/* w w w.ja v  a  2s  . co  m*/
        return;
    }
    final Paragraph jrobinParagraph = new Paragraph("",
            FontFactory.getFont(FontFactory.HELVETICA, 9f, Font.NORMAL));
    jrobinParagraph.setAlignment(Element.ALIGN_CENTER);
    jrobinParagraph.add(new Phrase("\n\n\n\n"));
    int i = 0;
    if (mySmallGraphs != null) {
        // si les graphiques ont t prinitialiss (en Swing) alors on les utilise
        for (final byte[] imageData : mySmallGraphs.values()) {
            if (i % 3 == 0 && i != 0) {
                // un retour aprs httpSessions et avant activeThreads pour l'alignement
                jrobinParagraph.add(new Phrase("\n\n\n\n\n"));
            }
            final Image image = Image.getInstance(imageData);
            image.scalePercent(50);
            jrobinParagraph.add(new Phrase(new Chunk(image, 0, 0)));
            jrobinParagraph.add(new Phrase(" "));
            i++;
        }
    } else {
        if (jrobins.isEmpty()) {
            return;
        }
        for (final JRobin jrobin : jrobins) {
            if (i % 3 == 0 && i != 0) {
                // un retour aprs httpSessions et avant activeThreads pour l'alignement
                jrobinParagraph.add(new Phrase("\n\n\n\n\n"));
            }
            final Image image = Image.getInstance(jrobin.graph(range, SMALL_GRAPH_WIDTH, SMALL_GRAPH_HEIGHT));
            image.scalePercent(50);
            jrobinParagraph.add(new Phrase(new Chunk(image, 0, 0)));
            jrobinParagraph.add(new Phrase(" "));
            i++;
        }
    }
    jrobinParagraph.add(new Phrase("\n"));
    addToDocument(jrobinParagraph);
}

From source file:net.bull.javamelody.PdfJavaInformationsReport.java

License:Apache License

private void writeSummary(JavaInformations javaInformations) throws BadElementException, IOException {
    addCell(getString("Host") + ':');
    currentTable.addCell(new Phrase(javaInformations.getHost(), boldCellFont));
    addCell(getString("memoire_utilisee") + ':');
    final MemoryInformations memoryInformations = javaInformations.getMemoryInformations();
    final long usedMemory = memoryInformations.getUsedMemory();
    final long maxMemory = memoryInformations.getMaxMemory();
    final Phrase memoryPhrase = new Phrase(
            integerFormat.format(usedMemory / 1024 / 1024) + ' ' + getString("Mo") + DIVIDE
                    + integerFormat.format(maxMemory / 1024 / 1024) + ' ' + getString("Mo") + BAR_SEPARATOR,
            cellFont);/*from w  w w  .j  a v  a 2 s.co  m*/
    final Image memoryImage = Image
            .getInstance(Bar.toBarWithAlert(memoryInformations.getUsedMemoryPercentage()), null);
    memoryImage.scalePercent(50);
    memoryPhrase.add(new Chunk(memoryImage, 0, 0));
    currentTable.addCell(memoryPhrase);
    if (javaInformations.getSessionCount() >= 0) {
        addCell(getString("nb_sessions_http") + ':');
        addCell(integerFormat.format(javaInformations.getSessionCount()));
    }
    addCell(getString("nb_threads_actifs") + "\n(" + getString("Requetes_http_en_cours") + "):");
    addCell(integerFormat.format(javaInformations.getActiveThreadCount()));
    if (!noDatabase) {
        addCell(getString("nb_connexions_actives") + ':');
        addCell(integerFormat.format(javaInformations.getActiveConnectionCount()));
        addCell(getString("nb_connexions_utilisees") + "\n(" + getString("ouvertes") + "):");
        final int usedConnectionCount = javaInformations.getUsedConnectionCount();
        final int maxConnectionCount = javaInformations.getMaxConnectionCount();
        if (maxConnectionCount <= 0) {
            addCell(integerFormat.format(usedConnectionCount));
        } else {
            final Phrase usedConnectionCountPhrase = new Phrase(integerFormat.format(usedConnectionCount)
                    + DIVIDE + integerFormat.format(maxConnectionCount) + BAR_SEPARATOR, cellFont);
            final Image usedConnectionCountImage = Image
                    .getInstance(Bar.toBarWithAlert(javaInformations.getUsedConnectionPercentage()), null);
            usedConnectionCountImage.scalePercent(50);
            usedConnectionCountPhrase.add(new Chunk(usedConnectionCountImage, 0, 0));
            currentTable.addCell(usedConnectionCountPhrase);
        }
    }
    if (javaInformations.getSystemLoadAverage() >= 0) {
        addCell(getString("Charge_systeme") + ':');
        addCell(decimalFormat.format(javaInformations.getSystemLoadAverage()));
    }
}

From source file:net.bull.javamelody.PdfJavaInformationsReport.java

License:Apache License

private void writeDetails(JavaInformations javaInformations) throws BadElementException, IOException {
    addCell(getString("OS") + ':');
    final Phrase osPhrase = new Phrase("", cellFont);
    final String osIconName = HtmlJavaInformationsReport.getOSIconName(javaInformations.getOS());
    final String separator = "   ";
    if (osIconName != null) {
        final Image osImage = PdfDocumentFactory.getImage("servers/" + osIconName);
        osImage.scalePercent(40);/*ww  w. j a  v  a2s.  c om*/
        osPhrase.add(new Chunk(osImage, 0, 0));
        osPhrase.add(separator);
    }
    osPhrase.add(javaInformations.getOS() + " (" + javaInformations.getAvailableProcessors() + ' '
            + getString("coeurs") + ')');
    currentTable.addCell(osPhrase);
    addCell(getString("Java") + ':');
    addCell(javaInformations.getJavaVersion());
    addCell(getString("JVM") + ':');
    final Phrase jvmVersionPhrase = new Phrase(javaInformations.getJvmVersion(), cellFont);
    if (javaInformations.getJvmVersion().contains("Client")) {
        jvmVersionPhrase.add(separator);
        final Image alertImage = PdfDocumentFactory.getImage("alert.png");
        alertImage.scalePercent(50);
        jvmVersionPhrase.add(new Chunk(alertImage, 0, -2));
    }
    currentTable.addCell(jvmVersionPhrase);
    addCell(getString("PID") + ':');
    addCell(javaInformations.getPID());
    if (javaInformations.getUnixOpenFileDescriptorCount() >= 0) {
        writeFileDescriptorCounts(javaInformations);
    }
    final String serverInfo = javaInformations.getServerInfo();
    if (serverInfo != null) {
        writeServerInfo(serverInfo);
        addCell(getString("Contexte_webapp") + ':');
        addCell(javaInformations.getContextPath());
    }
    addCell(getString("Demarrage") + ':');
    addCell(I18N.createDateAndTimeFormat().format(javaInformations.getStartDate()));
    addCell(getString("Arguments_JVM") + ':');
    addCell(javaInformations.getJvmArguments());
    if (javaInformations.getSessionCount() >= 0) {
        addCell(getString("httpSessionsMeanAge") + ':');
        addCell(integerFormat.format(javaInformations.getSessionMeanAgeInMinutes()));
    }
    writeTomcatInformations(javaInformations.getTomcatInformationsList());
    addCell(getString("Gestion_memoire") + ':');
    writeMemoryInformations(javaInformations.getMemoryInformations());
    if (javaInformations.getFreeDiskSpaceInTemp() >= 0) {
        // on considre que l'espace libre sur le disque dur est celui sur la partition du rpertoire temporaire
        addCell(getString("Free_disk_space") + ':');
        addCell(integerFormat.format(javaInformations.getFreeDiskSpaceInTemp() / 1024 / 1024) + ' '
                + getString("Mo"));
    }
    writeDatabaseVersionAndDataSourceDetails(javaInformations);
    if (javaInformations.isDependenciesEnabled()) {
        addCell(getString("Dependencies") + ':');
        addCell(getFormattedString("nb_dependencies", javaInformations.getDependenciesList().size()) + " ;\n"
                + javaInformations.getDependencies());
    }
    addCell("");
    addCell("");
}

From source file:net.bull.javamelody.PdfSessionInformationsReport.java

License:Apache License

private void writeCountry(SessionInformations session) throws IOException, BadElementException {
    final String country = session.getCountry();
    if (country == null) {
        addCell("");
    } else {//from   w w w .java2s .  c om
        final Image image = getCountryImage(country);
        if (image == null) {
            addCell(country);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

License:LGPL

/**
 *
 *//*from www .java2 s.c  om*/
protected void exportImage(JRPrintImage printImage) throws DocumentException, IOException, JRException {
    if (printImage.getMode() == JRElement.MODE_OPAQUE) {
        pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(),
                printImage.getBackcolor().getBlue());
        pdfContentByte.rectangle(printImage.getX() + getOffsetX(),
                jasperPrint.getPageHeight() - printImage.getY() - getOffsetY(), printImage.getWidth(),
                -printImage.getHeight());
        pdfContentByte.fill();
    }

    int topPadding = printImage.getLineBox().getTopPadding().intValue();
    int leftPadding = printImage.getLineBox().getLeftPadding().intValue();
    int bottomPadding = printImage.getLineBox().getBottomPadding().intValue();
    int rightPadding = printImage.getLineBox().getRightPadding().intValue();

    int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding;
    availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth;

    int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
    availableImageHeight = (availableImageHeight < 0) ? 0 : availableImageHeight;

    JRRenderable renderer = printImage.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        if (renderer.getType() == JRRenderable.TYPE_IMAGE) {
            // Image renderers are all asked for their image data at some point. 
            // Better to test and replace the renderer now, in case of lazy load error.
            renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, printImage.getOnErrorType());
        }
    } else {
        renderer = null;
    }

    if (renderer != null) {
        int xoffset = 0;
        int yoffset = 0;

        Chunk chunk = null;

        float scaledWidth = availableImageWidth;
        float scaledHeight = availableImageHeight;

        if (renderer.getType() == JRRenderable.TYPE_IMAGE) {
            com.lowagie.text.Image image = null;

            float xalignFactor = getXAlignFactor(printImage);
            float yalignFactor = getYAlignFactor(printImage);

            switch (printImage.getScaleImage()) {
            case JRImage.SCALE_IMAGE_CLIP: {
                // Image load might fail, from given image data. 
                // Better to test and replace the renderer now, in case of lazy load error.
                renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer,
                        printImage.getOnErrorType());
                if (renderer == null) {
                    break;
                }

                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

                Dimension2D dimension = renderer.getDimension();
                if (dimension != null) {
                    normalWidth = (int) dimension.getWidth();
                    normalHeight = (int) dimension.getHeight();
                }

                xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));

                int minWidth = Math.min(normalWidth, availableImageWidth);
                int minHeight = Math.min(normalHeight, availableImageHeight);

                BufferedImage bi = new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_ARGB);

                Graphics2D g = bi.createGraphics();
                if (printImage.getMode() == JRElement.MODE_OPAQUE) {
                    g.setColor(printImage.getBackcolor());
                    g.fillRect(0, 0, minWidth, minHeight);
                }
                renderer.render(g, new java.awt.Rectangle((xoffset > 0 ? 0 : xoffset),
                        (yoffset > 0 ? 0 : yoffset), normalWidth, normalHeight));
                g.dispose();

                xoffset = (xoffset < 0 ? 0 : xoffset);
                yoffset = (yoffset < 0 ? 0 : yoffset);

                //awtImage = bi.getSubimage(0, 0, minWidth, minHeight);

                //image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor());
                image = com.lowagie.text.Image.getInstance(bi, null);

                break;
            }
            case JRImage.SCALE_IMAGE_FILL_FRAME: {
                if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) {
                    image = (com.lowagie.text.Image) loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData());
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                JRImageRenderer.getInstance(renderer.getImageData()),
                                printImage.getOnErrorType());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage();
                        image = com.lowagie.text.Image.getInstance(awtImage, null);
                    }

                    if (printImage.isUsingCache()) {
                        loadedImagesMap.put(renderer, image);
                    }
                }

                image.scaleAbsolute(availableImageWidth, availableImageHeight);
                break;
            }
            case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
            default: {
                if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) {
                    image = (com.lowagie.text.Image) loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData());
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                JRImageRenderer.getInstance(renderer.getImageData()),
                                printImage.getOnErrorType());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage();
                        image = com.lowagie.text.Image.getInstance(awtImage, null);
                    }

                    if (printImage.isUsingCache()) {
                        loadedImagesMap.put(renderer, image);
                    }
                }

                image.scaleToFit(availableImageWidth, availableImageHeight);

                xoffset = (int) (xalignFactor * (availableImageWidth - image.plainWidth()));
                yoffset = (int) (yalignFactor * (availableImageHeight - image.plainHeight()));

                xoffset = (xoffset < 0 ? 0 : xoffset);
                yoffset = (yoffset < 0 ? 0 : yoffset);

                break;
            }
            }

            if (image != null) {
                chunk = new Chunk(image, 0, 0);

                scaledWidth = image.scaledWidth();
                scaledHeight = image.scaledHeight();
            }
        } else {
            double normalWidth = availableImageWidth;
            double normalHeight = availableImageHeight;

            double displayWidth = availableImageWidth;
            double displayHeight = availableImageHeight;

            double ratioX = 1f;
            double ratioY = 1f;

            Rectangle2D clip = null;

            Dimension2D dimension = renderer.getDimension();
            if (dimension != null) {
                normalWidth = dimension.getWidth();
                normalHeight = dimension.getHeight();
                displayWidth = normalWidth;
                displayHeight = normalHeight;

                float xalignFactor = getXAlignFactor(printImage);
                float yalignFactor = getYAlignFactor(printImage);

                switch (printImage.getScaleImage()) {
                case JRImage.SCALE_IMAGE_CLIP: {
                    xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                    yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
                    clip = new Rectangle2D.Double(-xoffset, -yoffset, availableImageWidth,
                            availableImageHeight);
                    break;
                }
                case JRImage.SCALE_IMAGE_FILL_FRAME: {
                    ratioX = availableImageWidth / normalWidth;
                    ratioY = availableImageHeight / normalHeight;
                    normalWidth *= ratioX;
                    normalHeight *= ratioY;
                    xoffset = 0;
                    yoffset = 0;
                    break;
                }
                case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
                default: {
                    ratioX = availableImageWidth / normalWidth;
                    ratioY = availableImageHeight / normalHeight;
                    ratioX = ratioX < ratioY ? ratioX : ratioY;
                    ratioY = ratioX;
                    normalWidth *= ratioX;
                    normalHeight *= ratioY;
                    xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                    yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
                    break;
                }
                }
            }

            PdfTemplate template = pdfContentByte.createTemplate((float) displayWidth, (float) displayHeight);

            Graphics2D g = forceSvgShapes
                    ? template.createGraphicsShapes((float) displayWidth, (float) displayHeight)
                    : template.createGraphics(availableImageWidth, availableImageHeight, new LocalFontMapper());

            if (clip != null) {
                g.setClip(clip);
            }

            if (printImage.getMode() == JRElement.MODE_OPAQUE) {
                g.setColor(printImage.getBackcolor());
                g.fillRect(0, 0, (int) displayWidth, (int) displayHeight);
            }

            Rectangle2D rectangle = new Rectangle2D.Double(0, 0, displayWidth, displayHeight);

            renderer.render(g, rectangle);
            g.dispose();

            pdfContentByte.saveState();
            pdfContentByte.addTemplate(template, (float) ratioX, 0f, 0f, (float) ratioY,
                    printImage.getX() + getOffsetX() + xoffset, jasperPrint.getPageHeight() - printImage.getY()
                            - getOffsetY() - (int) normalHeight - yoffset);
            pdfContentByte.restoreState();

            Image image = getPxImage();
            image.scaleAbsolute(availableImageWidth, availableImageHeight);
            chunk = new Chunk(image, 0, 0);
        }

        /*
        image.setAbsolutePosition(
           printImage.getX() + offsetX + borderOffset,
           jasperPrint.getPageHeight() - printImage.getY() - offsetY - image.scaledHeight() - borderOffset
           );
                
        pdfContentByte.addImage(image);
        */

        if (chunk != null) {
            setAnchor(chunk, printImage, printImage);
            setHyperlinkInfo(chunk, printImage);

            ColumnText colText = new ColumnText(pdfContentByte);
            int upperY = jasperPrint.getPageHeight() - printImage.getY() - topPadding - getOffsetY() - yoffset;
            int lowerX = printImage.getX() + leftPadding + getOffsetX() + xoffset;
            colText.setSimpleColumn(new Phrase(chunk), lowerX, upperY - scaledHeight, lowerX + scaledWidth,
                    upperY, scaledHeight, Element.ALIGN_LEFT);

            colText.go();
        }
    }

    if (printImage.getLineBox().getTopPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getLeftPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getBottomPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getRightPen().getLineWidth().floatValue() <= 0f) {
        if (printImage.getLinePen().getLineWidth().floatValue() > 0f) {
            exportPen(printImage.getLinePen(), printImage);
        }
    } else {
        /*   */
        exportBox(printImage.getLineBox(), printImage);
    }
}

From source file:nl.knaw.dans.common.lang.pdf.ExtendedHtmlWorker.java

License:Apache License

public void startElement(String tag, HashMap h) {
    if (!tagsSupported.containsKey(tag))
        return;//  w  ww  . j a va 2s. c  o  m
    try {
        style.applyStyle(tag, h);
        String follow = (String) FactoryProperties.followTags.get(tag);
        if (follow != null) {
            HashMap prop = new HashMap();
            prop.put(follow, null);
            cprops.addToChain(follow, prop);
            return;
        }
        FactoryProperties.insertStyle(h);
        if (tag.equals("a")) {
            cprops.addToChain(tag, h);
            if (currentParagraph == null)
                currentParagraph = new Paragraph();
            stack.push(currentParagraph);
            currentParagraph = new Paragraph();
            return;
        }
        if (tag.equals("br")) {
            if (currentParagraph == null)
                currentParagraph = new Paragraph();
            currentParagraph.add(factoryProperties.createChunk("\n", cprops));
            return;
        }
        if (tag.equals("font") || tag.equals("span")) {
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals("img")) {
            String src = (String) h.get("src");
            if (src == null)
                return;
            cprops.addToChain(tag, h);
            Image img = null;
            if (interfaceProps != null) {
                ImageProvider ip = (ImageProvider) interfaceProps.get("img_provider");
                if (ip != null)
                    img = ip.getImage(src, h, cprops, document);
                if (img == null) {
                    HashMap images = (HashMap) interfaceProps.get("img_static");
                    if (images != null) {
                        Image tim = (Image) images.get(src);
                        if (tim != null)
                            img = Image.getInstance(tim);
                    } else {
                        if (!src.startsWith("http")) { // relative src references only
                            String baseurl = (String) interfaceProps.get("img_baseurl");
                            if (baseurl != null) {
                                src = baseurl + src;
                                img = Image.getInstance(src);
                            }
                        }
                    }
                }
            }
            if (img == null) {
                if (!src.startsWith("http")) {
                    String path = cprops.getProperty("image_path");
                    if (path == null)
                        path = "";
                    src = new File(path, src).getPath();
                }
                img = Image.getInstance(src);
            }
            String align = (String) h.get("align");
            String width = (String) h.get("width");
            String height = (String) h.get("height");
            String before = cprops.getProperty("before");
            String after = cprops.getProperty("after");
            if (before != null)
                img.setSpacingBefore(Float.parseFloat(before));
            if (after != null)
                img.setSpacingAfter(Float.parseFloat(after));
            float wp = lengthParse(width, (int) img.getWidth());
            float lp = lengthParse(height, (int) img.getHeight());
            if (wp > 0 && lp > 0)
                img.scalePercent(wp > lp ? lp : wp);
            else if (wp > 0)
                img.scalePercent(wp);
            else if (lp > 0)
                img.scalePercent(lp);
            img.setWidthPercentage(0);
            if (align != null) {
                endElement("p");
                int ralign = Image.MIDDLE;
                if (align.equalsIgnoreCase("left"))
                    ralign = Image.LEFT;
                else if (align.equalsIgnoreCase("right"))
                    ralign = Image.RIGHT;
                img.setAlignment(ralign);
                Img i = null;
                boolean skip = false;
                if (interfaceProps != null) {
                    i = (Img) interfaceProps.get("img_interface");
                    if (i != null)
                        skip = i.process(img, h, cprops, document);
                }
                if (!skip)
                    document.add(img);
                cprops.removeChain(tag);
            } else {
                cprops.removeChain(tag);
                if (currentParagraph == null)
                    currentParagraph = FactoryProperties.createParagraph(cprops);
                currentParagraph.add(new Chunk(img, 0, 0));
            }
            return;
        }
        endElement("p");
        if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5")
                || tag.equals("h6")) {
            if (!h.containsKey("size")) {
                int v = 7 - Integer.parseInt(tag.substring(1));
                h.put("size", Integer.toString(v));
            }
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals("ul")) {
            if (pendingLI)
                endElement("li");
            skipText = true;
            cprops.addToChain(tag, h);
            com.lowagie.text.List list = new com.lowagie.text.List(false, 10);
            list.setListSymbol("\u2022");
            String indent = ((String) h.get("indent"));
            if (indent != null && indent.matches("[0-9]+")) {
                list.setSymbolIndent(Integer.valueOf(indent));
            }
            stack.push(list);
            return;
        }
        if (tag.equals("ol")) {
            if (pendingLI)
                endElement("li");
            skipText = true;
            cprops.addToChain(tag, h);
            com.lowagie.text.List list = new com.lowagie.text.List(true, 10);
            String type = ((String) h.get("type"));
            if (type != null && type.toLowerCase().equals("a")) {
                list.setLettered(true);
                list.setNumbered(false);
                list.setLowercase(type.equals("a"));
            }
            String indent = ((String) h.get("indent"));
            if (indent != null && indent.matches("[0-9]+")) {
                list.setSymbolIndent(Integer.valueOf(indent));
            }
            stack.push(list);
            return;
        }
        if (tag.equals("li")) {
            if (pendingLI)
                endElement("li");
            skipText = false;
            pendingLI = true;
            cprops.addToChain(tag, h);
            stack.push(FactoryProperties.createListItem(cprops));
            return;
        }
        if (tag.equals("div") || tag.equals("body")) {
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals("pre")) {
            if (!h.containsKey("face")) {
                h.put("face", "Courier");
            }
            cprops.addToChain(tag, h);
            isPRE = true;
            return;
        }
        if (tag.equals("p")) {
            cprops.addToChain(tag, h);
            currentParagraph = FactoryProperties.createParagraph(h);
            return;
        }
        if (tag.equals("tr")) {
            if (pendingTR)
                endElement("tr");
            skipText = true;
            pendingTR = true;
            cprops.addToChain("tr", h);
            return;
        }
        if (tag.equals("td") || tag.equals("th")) {
            if (pendingTD)
                endElement(tag);
            skipText = false;
            pendingTD = true;
            cprops.addToChain("td", h);
            stack.push(new IncCell(tag, cprops));
            return;
        }
        if (tag.equals("table")) {
            cprops.addToChain("table", h);
            IncTable table = new IncTable(h);
            stack.push(table);
            tableState.push(new boolean[] { pendingTR, pendingTD });
            pendingTR = pendingTD = false;
            skipText = true;
            return;
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.activityinfo.server.report.renderer.itext.ItextMapRenderer.java

License:Open Source License

private void addPieChartDescription(MapReportElement element, Cell descriptionCell, PiechartMapLayer layer)
        throws BadElementException, IOException {

    for (Slice slice : layer.getSlices()) {
        IndicatorDTO indicator = element.getContent().getIndicatorById(slice.getIndicatorId());
        Color color = ColorUtil.colorFromString(slice.getColor());
        ItextGraphic sliceImage = renderSlice(imageCreator, color, 10);

        Chunk box = new Chunk(sliceImage.toItextImage(), 0, 0);
        Chunk description = new Chunk(indicator.getName());

        Phrase phrase = new Phrase();
        phrase.add(box);//from w w w  . j  a v  a 2 s.  com
        phrase.add(description);

        Paragraph paragraph = new Paragraph(phrase);

        descriptionCell.add(paragraph);
    }
}