Example usage for com.lowagie.text Image plainWidth

List of usage examples for com.lowagie.text Image plainWidth

Introduction

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

Prototype

float plainWidth

To view the source code for com.lowagie.text Image plainWidth.

Click Source Link

Document

This is the width of the image without rotation.

Usage

From source file:com.develog.utils.report.engine.export.JRPdfExporter.java

License:Open Source License

/**
 *
 *///w  w  w  .  j  a v a  2  s .  c  o  m
protected void exportImage(JRPrintImage printImage) throws DocumentException, IOException {
    pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(),
            printImage.getBackcolor().getBlue());

    int borderOffset = 0;
    float borderCorrection = 0f;
    float lineWidth = 1f;
    boolean isLineDotted = false;

    switch (printImage.getPen()) {
    case JRGraphicElement.PEN_DOTTED: {
        borderOffset = 0;
        borderCorrection = 0f;
        lineWidth = 1f;
        isLineDotted = true;
        break;
    }
    case JRGraphicElement.PEN_4_POINT: {
        borderOffset = 2;
        borderCorrection = 0f;
        lineWidth = 4f;
        isLineDotted = false;
        break;
    }
    case JRGraphicElement.PEN_2_POINT: {
        borderOffset = 1;
        borderCorrection = 0f;
        lineWidth = 2f;
        isLineDotted = false;
        break;
    }
    case JRGraphicElement.PEN_THIN: {
        borderOffset = 0;
        borderCorrection = 0.25f;
        lineWidth = 0.5f;
        isLineDotted = false;
        break;
    }
    case JRGraphicElement.PEN_NONE: {
        borderOffset = 0;
        borderCorrection = 0.5f;
        lineWidth = 1f;
        isLineDotted = false;

        break;
    }
    case JRGraphicElement.PEN_1_POINT:
    default: {
        borderOffset = 0;
        borderCorrection = 0f;
        lineWidth = 1f;
        isLineDotted = false;
        break;
    }
    }

    if (printImage.getMode() == JRElement.MODE_OPAQUE) {
        pdfContentByte.setRGBColorStroke(printImage.getBackcolor().getRed(),
                printImage.getBackcolor().getGreen(), printImage.getBackcolor().getBlue());
        pdfContentByte.setLineWidth(0.1f);
        pdfContentByte.setLineDash(0f);
        pdfContentByte.rectangle(printImage.getX() - borderCorrection,
                jasperPrint.getPageHeight() - printImage.getY() + borderCorrection,
                printImage.getWidth() + 2 * borderCorrection - 1,
                -printImage.getHeight() - 2 * borderCorrection + 1);
        pdfContentByte.fillStroke();
    }

    int availableImageWidth = printImage.getWidth() - 2 * borderOffset;
    availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth;

    int availableImageHeight = printImage.getHeight() - 2 * borderOffset;
    availableImageHeight = (availableImageHeight < 0) ? 0 : availableImageHeight;

    int xoffset = 0;
    int yoffset = 0;

    if (printImage.getImageData() != null && availableImageWidth > 0 && availableImageHeight > 0) {
        //java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());

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

        float xalignFactor = 0f;
        switch (printImage.getHorizontalAlignment()) {
        case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
            xalignFactor = 1f;
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
            xalignFactor = 0.5f;
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_LEFT:
        default: {
            xalignFactor = 0f;
            break;
        }
        }

        float yalignFactor = 0f;
        switch (printImage.getVerticalAlignment()) {
        case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
            yalignFactor = 1f;
            break;
        }
        case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
            yalignFactor = 0.5f;
            break;
        }
        case JRAlignment.VERTICAL_ALIGN_TOP:
        default: {
            yalignFactor = 0f;
            break;
        }
        }

        switch (printImage.getScaleImage()) {
        case JRImage.SCALE_IMAGE_CLIP: {
            java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());
            //image = com.lowagie.text.Image.getInstance(awtImage, null);

            int awtWidth = awtImage.getWidth(null);
            int awtHeight = awtImage.getHeight(null);

            xoffset = (int) (xalignFactor * (availableImageWidth - awtWidth));
            yoffset = (int) (yalignFactor * (availableImageHeight - awtHeight));

            int minWidth = Math.min(awtWidth, availableImageWidth);
            int minHeight = Math.min(awtHeight, availableImageHeight);

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

            Graphics g = bi.getGraphics();
            g.setColor(printImage.getBackcolor());
            g.fillRect(0, 0, minWidth, minHeight);
            g.drawImage(awtImage, (xoffset > 0 ? 0 : xoffset), (yoffset > 0 ? 0 : yoffset), null);

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

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

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

            break;
        }
        case JRImage.SCALE_IMAGE_FILL_FRAME: {
            try {
                image = com.lowagie.text.Image.getInstance(printImage.getImageData());
                imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
            } catch (Exception e) {
                java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());
                image = com.lowagie.text.Image.getInstance(awtImage, null);
            }
            image.scaleAbsolute(availableImageWidth, availableImageHeight);
            break;
        }
        case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
        default: {
            try {
                image = com.lowagie.text.Image.getInstance(printImage.getImageData());
                imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
            } catch (Exception e) {
                java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData());
                image = com.lowagie.text.Image.getInstance(awtImage, null);
            }
            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;
        }
        }

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

        Chunk chunk = new Chunk(image, -0.5f, 0.5f);

        if (printImage.getAnchorName() != null) {
            chunk.setLocalDestination(printImage.getAnchorName());
        }

        switch (printImage.getHyperlinkType()) {
        case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {
            if (printImage.getHyperlinkReference() != null) {
                chunk.setAnchor(printImage.getHyperlinkReference());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR: {
            if (printImage.getHyperlinkAnchor() != null) {
                chunk.setLocalGoto(printImage.getHyperlinkAnchor());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: {
            if (printImage.getHyperlinkPage() != null) {
                chunk.setLocalGoto("JR_PAGE_ANCHOR_" + printImage.getHyperlinkPage().toString());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR: {
            if (printImage.getHyperlinkReference() != null && printImage.getHyperlinkAnchor() != null) {
                chunk.setRemoteGoto(printImage.getHyperlinkReference(), printImage.getHyperlinkAnchor());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: {
            if (printImage.getHyperlinkReference() != null && printImage.getHyperlinkPage() != null) {
                chunk.setRemoteGoto(printImage.getHyperlinkReference(),
                        printImage.getHyperlinkPage().intValue());
            }
            break;
        }
        case JRHyperlink.HYPERLINK_TYPE_NONE:
        default: {
            break;
        }
        }

        ColumnText colText = new ColumnText(pdfContentByte);
        colText.setSimpleColumn(new Phrase(chunk), printImage.getX() + xoffset + borderOffset,
                jasperPrint.getPageHeight() - printImage.getY() - image.scaledHeight() - yoffset - borderOffset,
                printImage.getX() + xoffset + borderOffset + image.scaledWidth(),
                jasperPrint.getPageHeight() - printImage.getY() - yoffset - borderOffset, image.scaledHeight(),
                Element.ALIGN_LEFT);

        colText.go();
    }

    if (printImage.getPen() != JRGraphicElement.PEN_NONE) {
        pdfContentByte.setRGBColorStroke(printImage.getForecolor().getRed(),
                printImage.getForecolor().getGreen(), printImage.getForecolor().getBlue());

        pdfContentByte.setLineWidth(lineWidth);

        if (isLineDotted) {
            pdfContentByte.setLineDash(5f, 3f, 0f);
        } else {
            pdfContentByte.setLineDash(0f);
        }

        pdfContentByte.rectangle(printImage.getX() - borderCorrection,
                jasperPrint.getPageHeight() - printImage.getY() + borderCorrection,
                printImage.getWidth() + 2 * borderCorrection - 1,
                -printImage.getHeight() - 2 * borderCorrection + 1);

        pdfContentByte.stroke();
    }
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

/**
 *
 *///w w  w .  j  ava 2s  .c  om
public void exportImage(JRPrintImage printImage) throws DocumentException, IOException, JRException {
    if (printImage.getModeValue() == ModeEnum.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;

    Renderable renderer = printImage.getRenderable();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        if (renderer.getTypeValue() == RenderableTypeEnum.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 = RenderableUtil.getInstance(jasperReportsContext).getOnErrorRendererForImageData(renderer,
                    printImage.getOnErrorTypeValue());
        }
    } else {
        renderer = null;
    }

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

        Chunk chunk = null;

        float scaledWidth = availableImageWidth;
        float scaledHeight = availableImageHeight;

        if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
            com.lowagie.text.Image image = null;

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

            switch (printImage.getScaleImageValue()) {
            case CLIP: {
                // Image load might fail, from given image data. 
                // Better to test and replace the renderer now, in case of lazy load error.
                renderer = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForDimension(renderer, printImage.getOnErrorTypeValue());
                if (renderer == null) {
                    break;
                }

                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

                Dimension2D dimension = renderer.getDimension(jasperReportsContext);
                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.getModeValue() == ModeEnum.OPAQUE) {
                    g.setColor(printImage.getBackcolor());
                    g.fillRect(0, 0, minWidth, minHeight);
                }
                renderer.render(jasperReportsContext, 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 FILL_FRAME: {
                if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) {
                    image = loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData(jasperReportsContext));
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                jasperReportsContext,
                                JRImageRenderer.getInstance(renderer.getImageData(jasperReportsContext)),
                                printImage.getOnErrorTypeValue());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage(jasperReportsContext);
                        image = com.lowagie.text.Image.getInstance(awtImage, null);
                    }

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

                image.scaleAbsolute(availableImageWidth, availableImageHeight);
                break;
            }
            case RETAIN_SHAPE:
            default: {
                if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) {
                    image = loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData(jasperReportsContext));
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                jasperReportsContext,
                                JRImageRenderer.getInstance(renderer.getImageData(jasperReportsContext)),
                                printImage.getOnErrorTypeValue());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage(jasperReportsContext);
                        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(jasperReportsContext);
            if (dimension != null) {
                normalWidth = dimension.getWidth();
                normalHeight = dimension.getHeight();
                displayWidth = normalWidth;
                displayHeight = normalHeight;

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

                switch (printImage.getScaleImageValue()) {
                case CLIP: {
                    xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                    yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
                    clip = new Rectangle2D.Double(-xoffset, -yoffset, availableImageWidth,
                            availableImageHeight);
                    break;
                }
                case FILL_FRAME: {
                    ratioX = availableImageWidth / normalWidth;
                    ratioY = availableImageHeight / normalHeight;
                    normalWidth *= ratioX;
                    normalHeight *= ratioY;
                    xoffset = 0;
                    yoffset = 0;
                    break;
                }
                case 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.getModeValue() == ModeEnum.OPAQUE) {
                g.setColor(printImage.getBackcolor());
                g.fillRect(0, 0, (int) displayWidth, (int) displayHeight);
            }

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

            renderer.render(jasperReportsContext, 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);

            //tagHelper.startImage(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();

            //tagHelper.endImage();
        }
    }

    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:corner.orm.tapestry.pdf.components.PdfImage.java

License:Apache License

/**
 * @see corner.orm.tapestry.pdf.components.AbstractPdfComponent#renderPdf(corner.orm.tapestry.pdf.PdfWriterDelegate, com.lowagie.text.Document)
 *//*from w w  w  .  j a  v  a2s.  c o  m*/
@Override
public void renderPdf(PdfWriterDelegate writer, Document doc) {
    Defense.notNull(getRectangle(), "TextField?");
    String[] p = getRectangle().split(",");
    try {
        Image image = Image.getInstance(getValue());
        image.setAbsolutePosition(Float.valueOf(p[0]), Float.valueOf(p[1]));// ?
        if (getAutoSize()) {
            if (Constants.PDFIMAGE_COMPONENT_SCALE_TYPE_BORDER.equals(getScaleType())) {
                image.scaleAbsolute((Float.valueOf(p[2]) - Float.valueOf(p[0])),
                        (Float.valueOf(p[3]) - Float.valueOf(p[1])));// ?
            } else {
                float picHeight = image.plainHeight();// 
                float picWeight = image.plainWidth();// 
                float borderHeight = Float.valueOf(p[3]) - Float.valueOf(p[1]);// 
                float borderWeight = Float.valueOf(p[2]) - Float.valueOf(p[0]);// 

                float heightPer = borderHeight / picHeight * 100;
                float widthPer = borderWeight / picWeight * 100;

                // ????
                if (heightPer > widthPer) {
                    image.scalePercent(widthPer);
                } else {
                    image.scalePercent(heightPer);
                }
            }
        }
        doc.add(image);
    } catch (BadElementException e) {
        throw new CornerSystemException(e);
    } catch (MalformedURLException e) {
        throw new CornerSystemException(e);
    } catch (IOException e) {
        throw new CornerSystemException(e);
    } catch (DocumentException e) {
        throw new CornerSystemException(e);
    }
}

From source file:de.xirp.report.ReportGenerator.java

License:Open Source License

/**
 * Adds a/*from   w ww  .  ja va  2 s  .  co m*/
 * {@link de.xirp.report.data.ContentPartImage image}
 * item to the PDF {@link com.lowagie.text.Document}.
 * 
 * @param item
 *            The image item to add.
 * @throws IOException
 *             if something went wrong saving the PDF.
 * @throws DocumentException
 *             if something went wrong adding the page.
 * @throws MalformedURLException
 *             if something went wrong saving the PDF.
 * @see de.xirp.report.data.ContentPartImage
 * @see com.lowagie.text.Document
 */
private static void addImage(ContentPartImage item)
        throws MalformedURLException, IOException, DocumentException {

    imageCounter++;

    String path = ""; //$NON-NLS-1$
    File file = new File(item.getPath());
    if (file.exists()) {
        path = item.getPath();
    }

    Image image = Image.getInstance(path);
    image.setAlignment(Image.MIDDLE);
    int maxWidth = 500;
    float plain = image.plainWidth();
    if (plain > maxWidth) {
        float percentage = maxWidth / plain;
        image.scalePercent(percentage * 100);
    }
    document.add(image);
    document.add(getParagraph(I18n.getString("ReportGenerator.report.document.image") //$NON-NLS-1$
            + " " + imageCounter + ": " + item.getShortDescription(), //$NON-NLS-1$ //$NON-NLS-2$
            IMAGE, Element.ALIGN_CENTER));

}

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

License:LGPL

/**
 *
 */// w w  w.  j  av a2s . c  o m
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:org.apache.maven.doxia.module.itext.ITextSink.java

License:Apache License

/**
 * If the <code>name</code> is a relative link, the internal link will used a System property
 * <code>itext.basedir</code>, or the class loader.
 * {@inheritDoc}//  w  w  w . jav a2 s .  co m
 */
public void figureGraphics(String name) {
    String urlName = null;
    File nameFile = null;
    if ((name.toLowerCase(Locale.ENGLISH).startsWith("http://"))
            || (name.toLowerCase(Locale.ENGLISH).startsWith("https://"))) {
        urlName = name;
    } else {
        if (System.getProperty("itext.basedir") != null) {
            try {
                nameFile = new File(System.getProperty("itext.basedir"), name);
                urlName = nameFile.toURI().toURL().toString();
            } catch (MalformedURLException e) {
                getLog().error("MalformedURLException: " + e.getMessage(), e);
            }
        } else {
            if (getClassLoader() != null) {
                if (getClassLoader().getResource(name) != null) {
                    urlName = getClassLoader().getResource(name).toString();
                }
            } else {
                if (ITextSink.class.getClassLoader().getResource(name) != null) {
                    urlName = ITextSink.class.getClassLoader().getResource(name).toString();
                }
            }
        }
    }

    if (urlName == null) {
        String msg = "No image '" + name
                + "' found in the class loader. Try to call setClassLoader(ClassLoader) before.";
        logMessage("imageNotFound", msg);

        return;
    }

    if (nameFile != null && !nameFile.exists()) {
        String msg = "No image '" + nameFile + "' found in your system, check the path.";
        logMessage("imageNotFound", msg);

        return;
    }

    boolean figureCalled = figureDefined;
    if (!figureCalled) {
        figure();
    }

    float width = 0;
    float height = 0;
    try {
        Image image = Image.getInstance(new URL(urlName));
        image.scaleToFit(ITextUtil.getDefaultPageSize().width() / 2,
                ITextUtil.getDefaultPageSize().height() / 2);
        width = image.plainWidth();
        height = image.plainHeight();
    } catch (BadElementException e) {
        getLog().error("BadElementException: " + e.getMessage(), e);
    } catch (MalformedURLException e) {
        getLog().error("MalformedURLException: " + e.getMessage(), e);
    } catch (IOException e) {
        getLog().error("IOException: " + e.getMessage(), e);
    }

    writeAddAttribute(ElementTags.URL, urlName);
    writeAddAttribute(ElementTags.ALIGN, ElementTags.ALIGN_MIDDLE);
    writeAddAttribute(ElementTags.PLAINWIDTH, String.valueOf(width));
    writeAddAttribute(ElementTags.PLAINHEIGHT, String.valueOf(height));

    actionContext.setAction(SinkActionContext.FIGURE_GRAPHICS);

    if (!figureCalled) {
        figure_();
    }
}