Example usage for com.itextpdf.kernel.pdf.extgstate PdfExtGState setFillOpacity

List of usage examples for com.itextpdf.kernel.pdf.extgstate PdfExtGState setFillOpacity

Introduction

In this page you can find the example usage for com.itextpdf.kernel.pdf.extgstate PdfExtGState setFillOpacity.

Prototype

public PdfExtGState setFillOpacity(float fillingAlphaConstant) 

Source Link

Document

Sets the current alpha constant, specifying the constant shape or constant opacity value for non-stroking operations in the transparent imaging model, ca key.

Usage

From source file:org.reactome.server.tools.diagram.exporter.raster.itext.awt.PdfGraphics2D.java

License:Open Source License

private void setPaint(boolean fill) {
    if (paint instanceof Color) {
        Color color = (Color) paint;
        int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfExtGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfExtGState();
                    gs.setFillOpacity(alpha / 255f);
                    fillGState[alpha] = gs;
                }//w  w  w.j a  va  2 s .com
                canvas.setExtGState(gs);
            }
            canvas.setFillColor(prepareColor(color));
        } else {
            if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfExtGState gs = strokeGState[alpha];
                if (gs == null) {
                    gs = new PdfExtGState();
                    gs.setStrokeOpacity(alpha / 255f);
                    strokeGState[alpha] = gs;
                }
                canvas.setExtGState(gs);
            }
            canvas.setStrokeColor(prepareColor(color));
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading.Axial shading = new PdfShading.Axial(DeviceRgb.RED.getColorSpace(), (float) p1.getX(),
                normalizeY((float) p1.getY()), c1.getRGBComponents(null), (float) p2.getX(),
                normalizeY((float) p2.getY()), c2.getRGBComponents(null));
        final PdfPattern.Shading pattern = new PdfPattern.Shading(shading);
        if (fill)
            canvas.setFillColorShading(pattern);
        else
            canvas.setStrokeColorShading(pattern);
    } else if (paint instanceof LinearGradientPaint) {
        final LinearGradientPaint gp = (LinearGradientPaint) paint;
        final Point2D p1 = gp.getStartPoint();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getEndPoint();
        transform.transform(p2, p2);
        final float[][] colors = Arrays.stream(gp.getColors()).map(color -> color.getColorComponents(null))
                .toArray(float[][]::new);
        final AxialShading shading = new AxialShading(DeviceRgb.RED.getColorSpace(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), colors);
        //         final PdfShading.Axial shading = new PdfShading.Axial(DeviceRgb.RED.getColorSpace(), (float) p1.getX(), normalizeY((float) p1.getY()), c1.getRGBComponents(null), (float) p2.getX(), normalizeY((float) p2.getY()), c2.getRGBComponents(null));
        final PdfPattern.Shading pattern = new PdfPattern.Shading(shading);
        if (fill)
            canvas.setFillColorShading(pattern);
        else
            canvas.setStrokeColorShading(pattern);
    } else if (paint instanceof TexturePaint) {
        //         try {
        //            TexturePaint tp = (TexturePaint) paint;
        //            BufferedImage img = tp.getImage();
        //            Rectangle2D rect = tp.getAnchorRect();
        //            com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(img, null);
        //            PdfPatternPainter pattern = canvas.createPattern(image.getWidth(), image.getHeight());
        //            AffineTransform inverse = this.normalizeMatrix();
        //            inverse.translate(rect.getX(), rect.getY());
        //            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
        //            double[] mx = new double[6];
        //            inverse.getMatrix(mx);
        //            pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]);
        //            image.setAbsolutePosition(0, 0);
        //            pattern.addImage(image);
        //            if (fill)
        //               canvas.setPatternFill(pattern);
        //            else
        //               canvas.setPatternStroke(pattern);
        //         } catch (Exception ex) {
        //            if (fill)
        //               canvas.setColorFill(BaseColor.GRAY);
        //            else
        //               canvas.setColorStroke(BaseColor.GRAY);
        //         }
    } else {
        try {
            BufferedImage img = null;
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            img = new BufferedImage((int) width, (int) height, type);
            Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            AffineTransform inv = transform.createInverse();
            Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
            fillRect = inv.createTransformedShape(fillRect);
            g.setPaint(paint);
            g.fill(fillRect);
            g.dispose();
        } catch (Exception ex) {
            if (fill)
                canvas.setFillColor(new DeviceGray(0.5f));
            else
                canvas.setStrokeColor(new DeviceGray(0.5f));
        }
    }
}

From source file:org.reactome.server.tools.diagram.exporter.raster.itext.awt.PdfGraphics2D.java

License:Open Source License

private boolean drawImage(Image img, Image mask, AffineTransform xform, Color bgColor, ImageObserver obs) {
    if (xform == null)
        xform = new AffineTransform();
    else/*from www .  j  av a2 s.  c  om*/
        xform = new AffineTransform(xform);
    xform.translate(0, img.getHeight(obs));
    xform.scale(img.getWidth(obs), img.getHeight(obs));

    AffineTransform inverse = this.normalizeMatrix();
    AffineTransform flipper = AffineTransform.getScaleInstance(1, -1);
    inverse.concatenate(xform);
    inverse.concatenate(flipper);

    double[] mx = new double[6];
    inverse.getMatrix(mx);
    if (currentFillGState != 255) {
        PdfExtGState gs = fillGState[255];
        if (gs == null) {
            gs = new PdfExtGState();
            gs.setFillOpacity(1);
            fillGState[255] = gs;
        }
        canvas.setExtGState(gs);
    }

    try {
        final ImageData image = ImageDataFactory.create(img, bgColor);
        if (mask != null) {
            ImageDataFactory.create(mask, null, true);
        }
        canvas.addImage(image, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4],
                (float) mx[5]);
    } catch (Exception ex) {
        throw new IllegalArgumentException(ex);
    }
    if (currentFillGState >= 0 && currentFillGState != 255) {
        PdfExtGState gs = fillGState[currentFillGState];
        canvas.setExtGState(gs);
    }
    return true;
}