Example usage for com.itextpdf.text.pdf PdfContentByte setColorFill

List of usage examples for com.itextpdf.text.pdf PdfContentByte setColorFill

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte setColorFill.

Prototype

public void setColorFill(final BaseColor color) 

Source Link

Document

Sets the fill color.

Usage

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

/**
 * This method will append to the pdf a legend explaining the visual feedback in the pdf, an overview of the styles
 * used and an overview of the properties used.
 *
 * @throws DocumentException/*from   w w  w.  j av  a 2 s .co m*/
 */
public static void appendDebugInfo(PdfWriter writer, Document document, EnhancedMap settings,
        StylerFactory stylerFactory) throws DocumentException, VectorPrintException {

    PdfContentByte canvas = writer.getDirectContent();
    canvas.setFontAndSize(FontFactory.getFont(FontFactory.COURIER).getBaseFont(), 8);
    canvas.setColorFill(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));
    canvas.setColorStroke(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    Font f = FontFactory.getFont(FontFactory.COURIER, 8);

    f.setColor(itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    float top = document.getPageSize().getTop();

    document.add(new Phrase(new Chunk("table: ", f).setLocalDestination(DEBUGPAGE)));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("cell: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("image: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("text: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("background color is shown in a small rectangle", f));
    document.add(Chunk.NEWLINE);
    canvas.setLineWidth(2);
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    float left = document.leftMargin();
    canvas.rectangle(left + 80, top - 25, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineWidth(0.3f);
    canvas.setLineDash(new float[] { 2, 2 }, 0);
    canvas.rectangle(left + 80, top - 37, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 1, 0 }, 0);
    canvas.rectangle(left + 80, top - 50, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    canvas.rectangle(left + 80, top - 63, left + 80, 8);
    canvas.closePathStroke();
    document.add(Chunk.NEWLINE);

    document.add(new Phrase("fonts available: " + FontFactory.getRegisteredFonts(), f));

    document.add(Chunk.NEWLINE);

    if (settings.getBooleanProperty(false, DEBUG)) {
        document.add(new Phrase("OVERVIEW OF STYLES FOR THIS REPORT", f));
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    Font b = new Font(f);
    b.setStyle("bold");
    Set<Map.Entry<String, String>> entrySet = stylerFactory.getStylerSetup().entrySet();
    for (Map.Entry<String, String> styleInfo : entrySet) {
        String key = styleInfo.getKey();
        document.add(new Chunk(key, b).setLocalDestination(key));
        document.add(new Chunk(": " + styleInfo.getValue(), f));
        document.add(Chunk.NEWLINE);
        document.add(new Phrase("   styling configured by " + key + ": ", f));
        for (BaseStyler st : (Collection<BaseStyler>) stylerFactory.getBaseStylersFromCache((key))) {
            document.add(Chunk.NEWLINE);
            document.add(new Chunk("      ", f));
            document.add(new Chunk(st.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                    .setLocalGoto(st.getClass().getSimpleName()));
            document.add(new Chunk(":", f));
            document.add(Chunk.NEWLINE);
            document.add(new Phrase("         non default parameters for " + st.getClass().getSimpleName()
                    + ": " + getParamInfo(st), f));
            document.add(Chunk.NEWLINE);
            if (!st.getConditions().isEmpty()) {
                document.add(new Phrase("      conditions for this styler: ", f));
            }
            for (StylingCondition sc : (Collection<StylingCondition>) st.getConditions()) {
                document.add(Chunk.NEWLINE);
                document.add(new Chunk("         ", f));
                document.add(
                        new Chunk(sc.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                                .setLocalGoto(sc.getClass().getSimpleName()));
                document.add(Chunk.NEWLINE);
                document.add(new Phrase("            non default parameters for "
                        + sc.getClass().getSimpleName() + ": " + getParamInfo(sc), f));
            }
        }
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    document.newPage();

    document.add(new Phrase("Properties used for the report", f));
    document.add(Chunk.NEWLINE);

    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bo);

    settings.listProperties(ps);
    ps.close();
    document.add(new Paragraph(bo.toString(), f));

    document.newPage();

    try {
        bo = new ByteArrayOutputStream();
        ps = new PrintStream(bo);
        Help.printHelpHeader(ps);
        ps.close();
        document.add(new Paragraph(bo.toString(), f));

        Help.printStylerHelp(document, f);

        Help.printConditionrHelp(document, f);

    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
            | NoSuchMethodException | InvocationTargetException ex) {
        log.log(Level.SEVERE, null, ex);
    }
}

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

License:Open Source License

@Override
public void draw(Rectangle rect, String genericTag) throws VectorPrintException {
    if (genericTag == null) {
        if (log.isLoggable(Level.FINE)) {
            log.fine("not drawing shadow because genericTag is null (no data for shadow)");
        }/*from   ww w . j ava2 s. c  o  m*/
        return;
    }
    DelayedData delayed = getDelayed(genericTag);
    PdfContentByte canvas = getPreparedCanvas();
    try {
        com.itextpdf.text.Font f = delayed.getChunk().getFont();
        if (f.getBaseFont() == null) {
            throw new VectorPrintRuntimeException(
                    "font " + f.getFamilyname() + " does not have a basefont, check your fontloading");
        }
        /*
         * print as much of the text as fits in the width of the rectangle
         */
        String toPrint = delayed.getStringData();
        int i = toPrint.length() + 1;
        do {
            toPrint = toPrint.substring(0, --i);
        } while (ItextHelper.getTextWidth(toPrint, f.getBaseFont(), f.getSize()) > rect.getWidth() + 1);
        if (i < delayed.getStringData().length()) {
            String nextPart = delayed.getStringData().substring(i).replaceFirst(" *", "");
            if (log.isLoggable(Level.FINE)) {
                log.fine(String.format("event %s, printed shadow %s of %s, left %s for next event", genericTag,
                        toPrint, delayed.getData(), nextPart));
            }
            delayed.setData(nextPart);
        }

        canvas.setFontAndSize(f.getBaseFont(), f.getSize());
        canvas.setColorFill((getColor() == null) ? f.getColor() : itextHelper.fromColor(getColor()));
        canvas.setColorStroke((getColor() == null) ? f.getColor() : itextHelper.fromColor(getColor()));

        canvas.beginText();
        HashMap<String, Object> attributes = delayed.getChunk().getAttributes();
        if (attributes != null && attributes.containsKey(Chunk.SKEW)) {
            float[] skew = (float[]) attributes.get(Chunk.SKEW);
            canvas.setTextMatrix(1, skew[0], skew[1], 1, rect.getLeft() + calculateShift(getShiftx(), f),
                    rect.getBottom() - calculateShift(getShifty(), f));
        } else {
            canvas.setTextMatrix(1, 0, 0, 1, rect.getLeft() + calculateShift(getShiftx(), f),
                    rect.getBottom() - calculateShift(getShifty(), f));
        }
        canvas.setTextRise(delayed.getChunk().getTextRise());
        canvas.showText(toPrint);
        canvas.endText();
    } catch (Exception ex) {
        resetCanvas(canvas);
        throw new VectorPrintException(ex);
    }
    resetCanvas(canvas);
}

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

License:Open Source License

@Override
protected void draw(PdfContentByte canvas, float x, float y, float width, float height, String genericTag) {
    if (getBorderWidth() > 0) {
        canvas.setLineWidth(getBorderWidth());
        canvas.setColorStroke(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getBorderColor()));
    }//from w w  w . j  ava 2 s  . co m
    if (width == -1) {
        width = getWidth();
    }
    if (height == -1) {
        height = getHeight();
    }
    canvas.setColorFill(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getColor()));
    if (isRounded()) {
        canvas.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
    }
    float xx = x, yy = y;
    float[] points = getPoints();
    float padding = getPadding();
    switch (getShape()) {
    case free:
        float xdif = x - points[0];
        float ydif = y - points[1];
        xx = points[0] + xdif;
        yy = points[1] + ydif;
        canvas.moveTo(points[0] + xdif, points[1] + ydif);
        for (int i = 2; i < points.length; i = i + 2) {
            canvas.lineTo(points[i], points[i + 1]);
        }
        break;
    case bezier:
        xdif = x - points[0];
        ydif = y - points[1];
        xx = points[0] + xdif;
        yy = points[1] + ydif;
        canvas.moveTo(points[0] + xdif, points[1] + ydif);
        for (int i = 2; i < points.length; i = i + 4) {
            canvas.curveTo(points[i] + xdif, points[i + 1] + ydif, points[i + 2] + xdif, points[i + 3] + ydif);
        }
        break;
    case rectangle:
        if (isClose()) {
            xx = x - padding;
            yy = y - padding - height;
            canvas.rectangle(xx, yy, width + padding * 2, height + padding * 2);
        } else {
            canvas.rectangle(x, y, width, height);
        }
        break;
    case roundrectangle:
        if (isEnclosing()) {
            xx = x - padding;
            yy = y - padding - height;
            canvas.roundRectangle(xx, yy, width + padding * 2, height + padding * 2, getRadius());
        } else {
            canvas.roundRectangle(x, y, width, height, getRadius());
        }
        break;
    case ellipse:
        if (isEnclosing()) {
            xx = x - padding;
            yy = y - padding - height;
            canvas.ellipse(xx, yy, x + width + 2 * padding, y + 2 * padding);
        } else {
            canvas.ellipse(x, y, x + width, y + height);
        }
        break;
    }
    if (isClose()) {
        if (isFill()) {
            canvas.closePathFillStroke();
        } else {
            canvas.closePathStroke();
        }
    } else {
        canvas.stroke();
    }
    if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.DEBUG)) {
        DebugHelper.styleLink(canvas, getStyleClass(), " (styling)", xx, yy, getSettings(), getLayerManager());
    }
}

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

License:Open Source License

@Override
protected void draw(PdfContentByte canvas, float x, float y, float width, float height, String genericTag)
        throws VectorPrintException {
    BaseFont bf = FontFactory.getFont(getAlias()).getBaseFont();
    if (bf == null) {
        throw new VectorPrintRuntimeException(
                "font " + getAlias() + " does not have a basefont, check your fontloading");
    }//from w  w  w  .ja  v a  2  s .  c  o m
    canvas.setFontAndSize(bf, getSize());
    canvas.setColorFill(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getColor()));
    canvas.setColorStroke(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getColor()));
    canvas.beginText();
    canvas.showTextAligned(Element.ALIGN_LEFT, getData(), x, y, getRotate());
    canvas.endText();
    if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.DEBUG)) {
        DebugHelper.styleLink(canvas, getStyleClass(), " (styling)", x, y, getSettings(), getLayerManager());
    }
}

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

License:Open Source License

/**
 * Prints a square and fills half of it with a gray rectangle.
 * @param x// w  ww  .  j  av a 2s .co m
 * @param y
 * @param cb
 * @throws Exception
 */
public void pictureBackdrop(float x, float y, PdfContentByte cb) throws Exception {
    cb.setColorStroke(new BaseColor(Color.black));
    cb.setColorFill(new BaseColor(Color.gray));
    cb.rectangle(x, y, 100, 200);
    cb.fill();
    cb.setLineWidth(2);
    cb.rectangle(x, y, 200, 200);
    cb.stroke();
}

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  w  w.jav  a 2 s  .  c  om
 * @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:engine.Pdf.java

public static void drawLines(Vector<HoughLine> vectors, PdfWriter writer) {
    PdfContentByte canvas = writer.getDirectContent();
    canvas.setColorStroke(BaseColor.RED);
    canvas.setLineWidth(1);/*from ww w . j a  v a 2  s  .  c  o m*/
    for (int i = 0; i < vectors.size(); i++) {
        HoughLine line = vectors.elementAt(i);
        canvas.moveTo(line.x1, line.y1);
        canvas.lineTo(line.x2, line.y2);
        canvas.closePathStroke();
    }
    //        canvas.closePathStroke();
    canvas.setColorFill(BaseColor.BLUE);
    canvas.fill();
}

From source file:engine.Pdf.java

public static void drawLines(PdfWriter writer) {
    PdfContentByte canvas = writer.getDirectContent();
    canvas.setColorStroke(BaseColor.RED);
    canvas.setLineWidth(3);//www.  j  ava  2  s.  c  o  m
    canvas.moveTo(20, 30);
    canvas.lineTo(900, 900);
    canvas.lineTo(220, 330);
    canvas.closePathStroke();
    canvas.setColorFill(BaseColor.RED);
    canvas.fill();
}

From source file:EplanPrinter.PDFPrint.java

License:Open Source License

public String insertStamp(String loc, float x, float y, int width, int height, int set, String date,
        int pageNum, int masterHeight, int masterWidth, String projNo)
        throws BadElementException, MalformedURLException, IOException, DocumentException {
    Image image = Image.getInstance(loc);
    float[] scalar = scale(x, y, width, height, masterHeight, masterWidth, pageNum);
    float[] trans = translate(x, y, r[pageNum - 1].getHeight(), r[pageNum - 1].getWidth(), masterHeight,
            masterWidth, pageNum);//from w w w  .  j  a v  a2s  .c  o m
    float[] f = commentTrans(x, y, masterHeight, masterWidth, pageNum);

    float shift = 0;

    /* 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] */
    trans = translateRotation(trans[0], trans[1], pageNum);

    if (set == 1) {
        float m = image.getPlainHeight();
        float pageChunk = r[pageNum - 1].getHeight() / 10;
        shift = r[pageNum - 1].getHeight() / 100;
        scalar[1] = (int) (pageChunk);
        scalar[0] = (int) (image.getPlainWidth() * pageChunk) / image.getPlainHeight();
        trans[0] = (int) (0 + (pageChunk * widthScalar));
        trans[1] = (int) (r[pageNum - 1].getHeight() - (pageChunk * heightScalar)
                - (shift * (heightScalar + 2)));
        heightScalar = heightScalar + 1;
        if (heightScalar == 8) {
            heightScalar = 0;
            widthScalar = widthScalar + 2;
        }
    }

    if (set == 1) {
        image.setAbsolutePosition(trans[0] + shift, trans[1] - scalar[1]);
        image.scaleAbsoluteHeight(scalar[1]);
        image.scaleAbsoluteWidth(scalar[0]);
        image.setRotationDegrees(rot);
    } else {
        //swap stamp dimensions for rotated drawings
        if (rot > 0) {
            image.setAbsolutePosition(trans[0] - 2 * scalar[1], trans[1] - 2 * scalar[0]);
        } else {
            image.setAbsolutePosition(trans[0], trans[1] - 2 * scalar[1]);
        }
        image.scaleAbsoluteHeight(scalar[1] * 2);
        image.scaleAbsoluteWidth(scalar[0] * 2);
        image.setRotationDegrees(rot);
    }

    PdfContentByte fg = pds.getOverContent(pageNum);
    fg.addImage(image);

    //Added by tmittelstadt on 09/21/2012 for ticket #698
    //draws a box around the date
    fg.setColorFill(BaseColor.WHITE);
    fg.setLineWidth(0f);

    //dmoody removed box.  ticket 900
    /*fg.moveTo(trans[0], trans[1] - scalar[1] * 2);
       fg.lineTo(trans[0], trans[1] - scalar[1] * 2 - 10);
       fg.lineTo(trans[0] + scalar[0] * 2, trans[1] - scalar[1] * 2 - 10);
       fg.lineTo(trans[0] + scalar[0] * 2, trans[1] - scalar[1] * 2);
       fg.lineTo(trans[0], trans[1] - scalar[1] * 2);*/

    fg.closePathFillStroke();
    fg.fill();

    //adds the date the stamp was added to the document to the pdf
    Phrase p = new Phrase(date);
    p.getFont().setColor(BaseColor.BLACK);
    //Modification zreeve 10/11/2012.  set font size to 11.
    p.getFont().setSize(9f);
    //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));
    ColumnText.showTextAligned(fg, Element.ALIGN_CENTER, p, (float) (trans[0]),
            (float) (trans[1] - scalar[1] * 2 - 12), 0);
    Phrase pn = new Phrase("#[" + projNo + "]");
    pn.getFont().setColor(BaseColor.BLACK);
    pn.getFont().setSize(10f);
    pn.getFont().setStyle("bold");
    ColumnText.showTextAligned(fg, Element.ALIGN_CENTER, pn, (float) (trans[0]),
            (float) (trans[1] - scalar[1] * 2 - 22), 0);

    return "";
}

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);/*w  w w. ja  va2  s  .c o m*/

    PdfContentByte fg = pds.getOverContent(pageNum);
    fg.setGState(gs1);
    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 "";
}