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

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

Introduction

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

Prototype

public void setColorStroke(final BaseColor color) 

Source Link

Document

Sets the stroke 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/*  www.jav a2s . c  o 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   w  w  w .ja  v a  2 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()));
    }//  www  .  j a  va2s .c  o 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");
    }//  www.  j  a va2 s .  co  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:com.vectorprint.report.itext.style.stylers.Underline.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 underline because genericTag is null (no data for underline)");
        }/*from www  .j  av  a 2 s.  c om*/
        return;
    }
    DelayedData delayed = getDelayed(genericTag);
    PdfContentByte canvas = getPreparedCanvas();
    try {
        com.itextpdf.text.Font f = delayed.getChunk().getFont();
        canvas.setColorStroke((getColor() == null) ? f.getColor() : itextHelper.fromColor(getColor()));
        canvas.setLineWidth(getLineWidth());
        if (delayed.getChunk().getTextRise() != 0) {
            canvas.transform(AffineTransform.getTranslateInstance(0, delayed.getChunk().getTextRise()));
        }
        HashMap<String, Object> attributes = delayed.getChunk().getAttributes();
        if (attributes != null && attributes.containsKey(Chunk.SKEW)) {
            log.warning("lines under skewed text may not be at the correct position");
        }
        canvas.moveTo(rect.getLeft(), rect.getBottom());
        canvas.lineTo(rect.getLeft() + rect.getWidth(), rect.getBottom());
        canvas.stroke();
    } catch (Exception ex) {
        resetCanvas(canvas);
        throw new VectorPrintException(ex);
    }
    resetCanvas(canvas);
}

From source file:ConexionBD.CreaPrefichaPDF.java

public static void drawRectangle(PdfContentByte content, float x, float y, float width, float height) {
    content.saveState();//from www. ja v  a 2s  .com
    PdfGState state = new PdfGState();
    content.setGState(state);
    content.setRGBColorFill(232, 232, 232);
    content.setColorStroke(BaseColor.BLUE);
    content.setLineWidth((float) .5);
    content.rectangle(x, y, width, height);
    content.fillStroke();
    content.restoreState();
}

From source file:ConexionBD.CreaPrefichaPDF.java

public static void drawRectangleSC(PdfContentByte content, float x, float y, float width, float height) {
    content.saveState();/*www  . j ava2  s. c  om*/
    PdfGState state = new PdfGState();
    content.setGState(state);
    content.setRGBColorFill(0xFF, 0xFF, 0xFA);
    content.setColorStroke(BaseColor.BLUE);
    content.setLineWidth((float) .5);
    content.rectangle(x, y, width, height);
    content.fillStroke();
    content.restoreState();
}

From source file:ConexionBD.CreaPrefichaPDF.java

public static void drawRectangleText(PdfContentByte content, float x, float y, float width, float height) {
    content.saveState();/*from   w ww  . j  a  va 2  s  .  com*/
    PdfGState state = new PdfGState();
    content.setGState(state);
    content.setRGBColorFill(0, 230, 255);
    content.setColorStroke(BaseColor.BLUE);
    content.setLineWidth((float) .5);
    content.rectangle(x, y, width, height);
    content.fillStroke();
    content.restoreState();
}

From source file:de.jost_net.JVerein.io.HeaderFooter.java

License:Open Source License

/**
 * Adds the header and the footer.//w  w  w.  j a  v a2  s .  co  m
 * 
 */
@Override
public void onEndPage(PdfWriter writer, Document document) {
    Rectangle rect = document.getPageSize();
    switch (writer.getPageNumber() % 2) {
    case 0:
        // ColumnText.showTextAligned(writer.getDirectContent(),
        // Element.ALIGN_RIGHT, header[0], rect.getRight(), rect.getTop(), 0);
        break;
    case 1:
        // ColumnText.showTextAligned(writer.getDirectContent(),
        // Element.ALIGN_LEFT,
        // header[1], rect.getLeft(), rect.getTop(), 0);
        break;
    }
    float left = rect.getLeft() + document.leftMargin();
    float right = rect.getRight() - document.rightMargin();
    float bottom = rect.getBottom() + document.bottomMargin();
    PdfContentByte pc = writer.getDirectContent();
    pc.setColorStroke(BaseColor.BLACK);
    pc.setLineWidth(0.5f);
    pc.moveTo(left, bottom - 5);
    pc.lineTo(right, bottom - 5);
    pc.stroke();
    pc.moveTo(left, bottom - 25);
    pc.lineTo(right, bottom - 25);
    pc.stroke();

    ColumnText.showTextAligned(pc, Element.ALIGN_CENTER,
            new Phrase(footer + " " + pagenumber, Reporter.getFreeSans(7)), (left + right) / 2, bottom - 18, 0);
}

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//from  w ww .jav a2  s  . c om
 * @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();
}