Example usage for com.itextpdf.text.pdf PdfGState setFillOpacity

List of usage examples for com.itextpdf.text.pdf PdfGState setFillOpacity

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfGState setFillOpacity.

Prototype

public void setFillOpacity(float ca) 

Source Link

Document

Sets the current stroking alpha constant, specifying the constant shape or constant opacity value to be used for nonstroking operations in the transparent imaging model.

Usage

From source file:com.qmetric.document.watermark.strategy.MessageWatermarkStrategy.java

License:Open Source License

private void applyTextTransparency(final PdfContentByte overContent) {
    // Make transparent - see http://itext-general.2136553.n4.nabble.com/Insert-transparent-Text-td2158904.html
    final PdfGState gstate = new PdfGState();
    gstate.setFillOpacity(OPACITY);
    gstate.setStrokeOpacity(OPACITY);/* w  ww  . j a v a2  s  . c o  m*/

    overContent.saveState();
    overContent.setGState(gstate);
}

From source file:com.vectorprint.report.itext.style.stylers.AdvancedImpl.java

License:Open Source License

/**
 * get a canvas for drawing, prepared according to settings
 *
 * @see #draw(com.itextpdf.text.Rectangle, java.lang.String) 
 * @return/*from w w  w.j  a v  a2 s . c o  m*/
 */
protected final PdfContentByte getPreparedCanvas(float opacity) {
    needRestore = false;
    PdfContentByte canvas = (tableForeground != null) ? tableForeground
            : (isBg()) ? getWriter().getDirectContentUnder() : getWriter().getDirectContent();
    String layerName = getLayerName();
    BLENDMODE blend = getBlend();
    if (getWriter().getPDFXConformance() == PdfWriter.PDFX1A2001) {
        // check blend, opacity, layers
        if (!PdfGState.BM_NORMAL.equals(blend.getBlend())
                && !PdfGState.BM_COMPATIBLE.equals(blend.getBlend())) {
            throw new VectorPrintRuntimeException("blend not supported in PDF/X-1a: " + blend);
        }
        if (layerName != null) {
            throw new VectorPrintRuntimeException("layers not supported in PDF/X-1a: " + layerName);
        }
        if (opacity < 1) {
            throw new VectorPrintRuntimeException("opacity not supported in PDF/X-1a: " + opacity);
        }
    }
    if (layerName != null) {
        layerManager.startLayerInGroup(layerName, canvas);
    }
    //                pgs.setAlphaIsShape(true);
    if (opacity <= 1) {
        //                        PdfShading shading = PdfShading.simpleAxial(getWriter(), 0, 0, getDocument().right() - getDocument().getPageSize().getWidth() * 0.6f, getDocument().top() - getDocument().getPageSize().getHeight() * 0.6f, Color.green, Color.orange,true,true);
        //                        canvas.paintShading(shading);
        canvas.saveState();
        needRestore = true;
        PdfGState pgs = new PdfGState();
        pgs.setFillOpacity(opacity);
        pgs.setStrokeOpacity(opacity);
        canvas.setGState(pgs);
    }
    if (!BLENDMODE.NORMAL.equals(blend)) {
        if (!needRestore) {
            canvas.saveState();
            needRestore = true;
        }
        PdfGState pgs = new PdfGState();
        pgs.setBlendMode(blend.getBlend());
        canvas.setGState(pgs);
    }
    if (getTransform() != null && !(this instanceof Image)) {
        canvas.transform(new AffineTransform(getTransform()));
    }
    return canvas;
}

From source file:com.vectorprint.report.jfree.ItextChartHelper.java

License:Open Source License

/**
 *
 * @param chart the chart tp plot/*from w w w.  j a  va 2s . c  o m*/
 * @param contentByte the canvas to plot to
 * @param width the width of the chart will be fit to
 * @param height the height of the chart will be fit to
 * @return
 * @throws BadElementException
 */
public static Image getChartImage(JFreeChart chart, PdfContentByte contentByte, float width, float height,
        float opacity) throws BadElementException {

    // create PdfTemplate from PdfContentByte
    PdfTemplate template = contentByte.createTemplate(width, height);
    template.saveState();
    PdfGState pgs = new PdfGState();
    pgs.setFillOpacity(opacity);
    pgs.setStrokeOpacity(opacity);
    template.setGState(pgs);

    // create Graphics2D from PdfTemplate
    Graphics2D g2 = new PdfGraphics2D(template, width, height);

    // setup the drawing area
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);

    // pass the Graphics2D and drawing area to JFreeChart
    chart.draw(g2, r2D);
    g2.dispose(); // always dispose this

    template.restoreState();

    // create Image from PdfTemplate
    return Image.getInstance(template);
}

From source file:edu.cornell.mannlib.vitro.webapp.visualization.visutils.PDFDocument.java

License:Open Source License

private void createTransparencyShapes(Document document, PdfWriter writer) throws Exception {
    PdfContentByte cb = writer.getDirectContent();

    pictureBackdrop(document.leftMargin(), 350, cb);
    cb.saveState();/*from w ww . j  a  va 2 s.  co  m*/
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(0.5f);
    cb.setGState(gs1);
    pictureCircles(document.leftMargin(), 350, cb);
    cb.restoreState();

    cb.resetRGBColorFill();
}

From source file:edu.cornell.mannlib.vitro.webapp.visualization.visutils.PDFDocument.java

License:Open Source License

/**
 * Prints 3 circles in different colors that intersect with eachother.
 * @param x/*from  w ww . jav  a 2s . c  o m*/
 * @param y
 * @param cb
 * @throws Exception
 */
public void pictureCircles(float x, float y, PdfContentByte cb) throws Exception {

    cb.saveState();
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(1.0f);
    cb.setGState(gs1);
    cb.setColorFill(new BaseColor(Color.red));
    cb.circle(x + 70, y + 70, 50);
    cb.fill();
    cb.restoreState();

    cb.setColorFill(new BaseColor(Color.yellow));
    cb.circle(x + 100, y + 130, 50);
    cb.fill();
    cb.setColorFill(new BaseColor(Color.blue));
    cb.circle(x + 130, y + 70, 50);
    cb.fill();
}

From source file:EplanPrinter.PDFPrint.java

License:Open Source License

public String insertSketch(String points, String color, String opac, int pageNum, int masterHeight,
        int masterWidth, int lineWeight, String lineStyle) throws DocumentException, IOException {
    float f = Float.valueOf(opac);
    PdfGState gs1 = new PdfGState();

    int t = points.indexOf("POLYGON");
    gs1.setFillOpacity(f);

    PdfContentByte fg = pds.getOverContent(pageNum);
    fg.setGState(gs1);/*  www . j  ava  2  s.c o m*/
    float[] pointsSt = shatterSketches(points);
    color = "0x" + color.substring(1);
    Color c = Color.decode(color);

    fg.setLineWidth(lineWeight * getRatio(masterHeight, masterWidth, pageNum));
    if (lineStyle.equals("dash")) {
        fg.setLineDash(lineWeight * 4f, lineWeight);
    } else {
        fg.setLineDash(0);
    }

    //fg.setLineWidth(5);
    fg.setColorStroke(new BaseColor(c.getRGB()));
    fg.setColorFill(new BaseColor(c.getRGB()));
    float[] prefl = scaleShape(pointsSt, masterHeight, masterWidth, pageNum);
    float[] fl = sketchTrans(prefl, pageNum);

    /* Addition. ftorres - 7/22/2015 - Added to account for rotated pages 
     *   with the origin (0,0) not set to the bottom-left of the page. [1400] */
    float flTrans[] = translateRotation(fl[0], fl[1], pageNum);

    if (points.indexOf("LINEPOINT") != -1) {
        fg.circle(flTrans[0], flTrans[1], 5);
        fg.fillStroke();
        if (t == -1) {
            gs1.setFillOpacity(0f);
        } else {
            gs1.setFillOpacity(f);
        }
        fg.setGState(gs1);
    }
    fg.moveTo(flTrans[0], flTrans[1]);
    for (int i = 2; i < pointsSt.length; i = i + 2) {
        flTrans = translateRotation(fl[i], fl[i + 1], pageNum);
        fg.lineTo(flTrans[0], flTrans[1]);
    }

    if (t != -1) {
        fg.closePathFillStroke();
        fg.fill();
    } else {
        fg.stroke();
    }

    return "";
}

From source file:EplanPrinter.PDFPrint.java

License:Open Source License

public String insertComment(String sx, String sy, String id, String deptValue, String userInit, String comment,
        int pageNum, int masterHeight, int masterWidth, int pinned, int customFontSize)
        throws DocumentException, IOException {
    float ratio = getRatio(masterHeight, masterWidth, pageNum);
    float x = Float.parseFloat(sx);
    float y = Float.parseFloat(sy);
    float[] f = commentTrans(x, y, masterHeight, masterWidth, pageNum);
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(1);
    if (customFontSize > 0)
        fontSize = customFontSize;//from www.  j av  a2 s .  c  om

    PdfContentByte fg = pds.getOverContent(pageNum);
    fg.setGState(gs1);
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    float[] trans = translate(x, y, r[pageNum - 1].getHeight(), r[pageNum - 1].getWidth(), masterHeight,
            masterWidth, pageNum);
    // comment tag image (width=35pts, height=8pts)
    float[] scalar = scale(trans[0], trans[1], 35, 8, masterHeight, masterWidth, pageNum);

    /* Addition. ftorres - 7/21/2015 - Added to account for rotated pages 
     *   with the origin (0,0) not set to the bottom-left of the page. [1400] */
    float coords[] = translateRotation(trans[0], trans[1], pageNum);
    coords = checkBounds(coords[0], coords[1], pageNum);

    /* Addition. ftorres - 10/20/2015 - If the comment was pinned in EPC, then
     *   render the comment inside a comment box. */
    if (pinned == 1) {
        insertPinnedComment(coords[0], coords[1], id + " - " + deptValue + " (" + userInit + ")", comment,
                pageNum, masterHeight, masterWidth);

        // Add the pinned comment text to page annotation -Jon Changkachith 11/24/2015
        Rectangle rect = new Rectangle(0, 0, 0, 0);
        PdfAnnotation annotation = PdfAnnotation.createText(pds.getWriter(), rect,
                id + " - " + deptValue + " (" + userInit + ")", cleanupComment(comment), true, id);
        pds.addAnnotation(annotation, pds.getWriter().getCurrentPageNumber());
    } else {
        Image image = Image.getInstance(commentIMGPath);
        image.setAbsolutePosition(coords[0] + (scalar[0] * (id.length() / 5f)), coords[1]);

        /*
         * Commented out by Jon Changkachith 12/09/2015 because it was throwing
         * DocumentException with the message "The image must have absolute positioning."
        image.scaleAbsoluteHeight(1);
        image.scaleAbsoluteWidth(1);
        */
        image.scalePercent(ratio); //Added to fix DocumentException "The image must have absolute positioning." Jon Changkachith 12/09/2015
        image.setAnnotation(new Annotation(id + " - " + deptValue + " (" + userInit + ")",
                cleanupComment(comment), 0, 0, 0, 0));
        fg.addImage(image);
    }

    fg.setLineWidth(.5f * ratio);
    fg.setColorStroke(new BaseColor(Color.decode("0x6E2405").getRGB()));
    fg.setColorFill(new BaseColor(Color.decode("0x6E2405").getRGB()));

    float tHeight = scalar[1];
    float tWidth = 0;
    if (id.length() > 3) {
        tWidth = (scalar[0] * (id.length() / 5f));
    } else {
        tWidth = (scalar[0]);
    }

    fg.moveTo(coords[0], coords[1]);
    fg.lineTo(coords[0] + (10f * ratio), coords[1] - (tHeight / 2));
    fg.lineTo(coords[0] + tWidth, coords[1] - (tHeight / 2));
    fg.lineTo(coords[0] + tWidth, coords[1] + (tHeight / 2));
    fg.lineTo(coords[0] + (10f * ratio), coords[1] + (tHeight / 2));

    fg.lineTo(coords[0], coords[1]);
    fg.closePathFillStroke();
    fg.fill();

    // Comment number that goes on the comment tag image
    Phrase p = new Phrase(id);
    p.getFont().setColor(BaseColor.WHITE);
    p.getFont().setSize(8f * ratio); //comment number font size = 8f
    //p.getChunks().get(0).setAnnotation(PdfAnnotation.createText(pds.getWriter(), new Rectangle(trans[0],trans[1], trans[0]+5f, trans[1]+5f), id, comment, true, id));

    float fs[] = translateRotation(f[0], f[1], pageNum);
    fs = checkBounds(fs[0], fs[1], pageNum);
    ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, p, (float) (fs[0] + (9 * ratio)),
            (float) (fs[1] - (3 * ratio)), 0);

    return "";
}

From source file:EplanPrinter.PDFPrint.java

License:Open Source License

private void insertPinnedComment(float xCoord, float yCoord, String id, String comment, int pageNum,
        int masterHeight, int masterWidth) throws DocumentException, IOException {

    int standardHeight = 38;
    int standardWidth = 130;
    int lineHeight = 7;

    if (fontSize == 8) {
        standardHeight = 48;//  ww  w .  ja va2  s  .c om
        standardWidth = 180;
        lineHeight = 10;
    } else if (fontSize == 10) {
        standardHeight = 70;
        standardWidth = 210;
        lineHeight = 12;
    }

    // comment box position defines lower-left corner
    xCoord += 41; // x-coordinate of comment text box
    yCoord -= 7; // lower offset moves box up

    float f = (float) 0.4;
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(f);

    PdfContentByte fg = pds.getOverContent(pageNum);
    fg.setGState(gs1);
    fg.setLineWidth(.5f * getRatio(masterHeight, masterWidth, pageNum));

    // Calculate additional needed height for the comment, based on length of comment.
    List<String> lines = createComment(cleanupComment(comment));
    int numLines = lines.size();
    int commentHeight = (numLines - 1) * lineHeight;

    // Draw the comment box
    String color = "0xF2F3E4";
    Color c = Color.decode(color);
    color = "0xFAFAF4";
    Color c2 = Color.decode(color);
    fg.setLineWidth(1f);
    fg.setColorStroke(new BaseColor(c.getRGB()));
    fg.setColorFill(new BaseColor(c2.getRGB()));
    float newCoords[] = pinnedCoords(xCoord, yCoord, standardWidth, standardHeight + commentHeight);
    fg.roundRectangle(xCoord, newCoords[1], standardWidth, standardHeight + commentHeight, 2f);
    fg.fillStroke();

    //BaseFont bf = BaseFont.createFont();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, true);
    BaseFont bfb = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, true);
    fg.setColorFill(BaseColor.RED);
    gs1.setFillOpacity(0.8f);
    fg.setGState(gs1);

    // Adding comment title text
    Phrase phrase = new Phrase("Comment", new Font(bfb, fontSize));
    ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, xCoord + 3, yCoord - 7, 0);

    // Adding comment number
    phrase = new Phrase(id, new Font(bf, fontSize));
    float offset = xCoord + 35;
    if (fontSize == 8)
        offset = xCoord + 45;
    else if (fontSize == 10)
        offset = xCoord + 55;
    ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, offset, yCoord - 7, 0);

    /*  Remove 10/27/2015 Jon Changkachith
    // Adding 'comment' label text
    phrase = new Phrase("Comment", new Font(bfb, fontSize));
    ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, xCoord + 3, yCoord - 20f, 0);
    */

    // Adding comment text
    int commentYOffset = 32;
    for (int i = 0; i < lines.size(); i++) {
        //phrase = new Phrase(lines.get(i), new Font(bf, fontSize));
        phrase = composePhrase(lines.get(i), bf, bfb);
        ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, xCoord + 3, yCoord - commentYOffset, 0);
        commentYOffset += lineHeight;
    }

}

From source file:org.frobic.colorednodes.renderers.NewNodeRenderer.java

License:Open Source License

@Override
public void renderPDF(Item item, PDFTarget target, PreviewProperties properties) {
    Float x = item.getData(NodeItem.X);
    Float y = item.getData(NodeItem.Y);
    Float size = item.getData(NodeItem.SIZE);
    Float angle = item.getData(NodeItem.ANGLE);
    size /= 2f;//from w ww  .  ja va2  s.com
    Color color = item.getData(NodeItem.COLOR);
    Integer nbcolors = item.getData(NodeItem.NBCOLOR);
    Color[] colors = item.getData(NodeItem.COLORS);
    Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR))
            .getColor(color);
    float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
    float alpha = properties.getIntValue(PreviewProperty.NODE_OPACITY) / 100f;

    PdfContentByte cb = target.getContentByte();
    cb.setRGBColorStroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue());
    cb.setLineWidth(borderSize);
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    if (alpha < 1f) {
        cb.saveState();
        PdfGState gState = new PdfGState();
        gState.setFillOpacity(alpha);
        gState.setStrokeOpacity(alpha);
        cb.setGState(gState);
    }
    for (int i = 0; i < nbcolors; i++) {
        cb.setRGBColorFill(colors[nbcolors - i - 1].getRed(), colors[nbcolors - i - 1].getGreen(),
                colors[nbcolors - i - 1].getBlue());
        if (size >= 0.5) {
            cb.newPath();
            ArrayList ar = cb.bezierArc(x - size, -y + size, x + size, 0 - size - y,
                    360f * (nbcolors - i - 1) / nbcolors + (360f / 6.28f) * angle, 360f * (1) / nbcolors);
            //cb.arc(x-size,-y+size,x+size,0-size-y,360f*(nbcolors-i-1)/nbcolors+angle,360f*(1)/nbcolors) ;
            cb.moveTo(x, -y);
            float pt[] = (float[]) ar.get(0);
            cb.moveTo(pt[0], pt[1]);
            for (int k = 0; k < ar.size(); ++k) {
                pt = (float[]) ar.get(k);
                cb.curveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
            }
            cb.lineTo(x, -y);
            //strokeAndFill();
            //            cb.ClosePathFillStroke();
            if (borderSize > 0) {
                cb.fill();
            } else {
                cb.fill();
            }

            if (borderSize > 0) {
                cb.circle(x, -y, size);
                cb.stroke();
            }
        }
    }
    if (alpha < 1f) {
        cb.restoreState();
    }
}

From source file:org.gephi.preview.plugin.renderers.ArrowRenderer.java

License:Open Source License

public void renderStraight(RenderTarget target, Item item, float x1, float y1, float x2, float y2, float radius,
        float size, Color color) {
    Edge edge = (Edge) item.getSource();
    PVector direction = new PVector(x2, y2);
    direction.sub(new PVector(x1, y1));
    direction.normalize();/*from w  w  w  . j  a v a2  s  . c o  m*/

    PVector p1 = new PVector(direction.x, direction.y);
    p1.mult(radius);
    p1.add(new PVector(x2, y2));

    PVector p1r = new PVector(direction.x, direction.y);
    p1r.mult(radius - size);
    p1r.add(new PVector(x2, y2));

    PVector p2 = new PVector(-direction.y, direction.x);
    p2.mult(size * BASE_RATIO);
    p2.add(p1r);

    PVector p3 = new PVector(direction.y, -direction.x);
    p3.mult(size * BASE_RATIO);
    p3.add(p1r);

    if (target instanceof ProcessingTarget) {
        PGraphics graphics = ((ProcessingTarget) target).getGraphics();
        graphics.noStroke();
        graphics.fill(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
        graphics.triangle(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
    } else if (target instanceof SVGTarget) {
        SVGTarget svgTarget = (SVGTarget) target;
        Element arrowElem = svgTarget.createElement("polyline");
        arrowElem.setAttribute("points",
                String.format(Locale.ENGLISH, "%f,%f %f,%f %f,%f", p1.x, p1.y, p2.x, p2.y, p3.x, p3.y));
        arrowElem.setAttribute("class",
                edge.getSource().getNodeData().getId() + " " + edge.getTarget().getNodeData().getId());
        arrowElem.setAttribute("fill", svgTarget.toHexString(color));
        arrowElem.setAttribute("fill-opacity", (color.getAlpha() / 255f) + "");
        arrowElem.setAttribute("stroke", "none");
        svgTarget.getTopElement(SVGTarget.TOP_ARROWS).appendChild(arrowElem);
    } else if (target instanceof PDFTarget) {
        PDFTarget pdfTarget = (PDFTarget) target;
        PdfContentByte cb = pdfTarget.getContentByte();
        cb.moveTo(p1.x, -p1.y);
        cb.lineTo(p2.x, -p2.y);
        cb.lineTo(p3.x, -p3.y);
        cb.closePath();
        cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
        if (color.getAlpha() < 255) {
            cb.saveState();
            float alpha = color.getAlpha() / 255f;
            PdfGState gState = new PdfGState();
            gState.setFillOpacity(alpha);
            cb.setGState(gState);
        }
        cb.fill();
        if (color.getAlpha() < 255) {
            cb.restoreState();
        }
    }
}