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:org.javad.stamp.pdf.CompositeRow.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }//from   ww  w  .j a v  a  2s  . c om
    float maxHeight = 0.0f;
    float maxWidth = 0.0f;
    float top = getY();

    if (getDescription() != null && !getDescription().isEmpty()) {
        content.setColorStroke(BaseColor.BLACK);
        Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.CompositeSetDescription);
        content.setFontAndSize(descFont.getBaseFont(), descFont.getSize());

        top -= descFont.getCalculatedSize();
        int count = 0;
        int tc = getDescription().split("\n").length;
        for (String desc : getDescription().split("\n")) {
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false));
            PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) maxWidth);
            count++;
            top -= descFont.getCalculatedSize() + ((count < tc) ? 2 : 4);
        }

    }

    float totalWidth = 0.0f;
    float totalHeight = getY() - top;
    float spacing = PdfUtil.convertFromMillimeters(getSpacingMode().getSpacing(box_spacing));
    int count = 0;
    for (int i = 0; i < rows.size(); i++) {
        StampRow set = rows.get(i);
        if (set.isSkipped()) {
            continue;
        }
        totalWidth += PdfUtil.findMaximumWidth(set, content) + ((count > 0) ? spacing : 0.0f);
        count++;
    }
    float cur_x = getX() - totalWidth / 2.0f;
    count = 0;
    for (StampRow set : rows) {
        if (set.isSkipped()) {
            continue;
        }
        set.setX(cur_x + PdfUtil.findMaximumWidth(set, content) / 2.0f);
        set.setY(top);
        OutputBounds rect = set.generate(content);
        count++;
        cur_x += rect.width + ((count > 0) ? spacing : 0);
        maxHeight = Math.max(maxHeight, rect.height);

    }
    maxWidth = Math.max(maxWidth, totalWidth);
    totalHeight += maxHeight;
    return new OutputBounds(getX() - (maxWidth / 2.0f), getY(), maxWidth, totalHeight);
}

From source file:org.javad.stamp.pdf.SetTenant.java

License:Apache License

protected void drawSeparator(PdfContentByte content, float x, float y, float width, float height) {

    float sx = (getOrientation() == Orientation.HORIZONTAL) ? x
            : x + PdfUtil.convertFromMillimeters(getPadding() + 2);
    float dx = (getOrientation() == Orientation.HORIZONTAL) ? x
            : x + width - PdfUtil.convertFromMillimeters(getPadding() + 2);
    float sy = (getOrientation() == Orientation.HORIZONTAL)
            ? y + PdfUtil.convertFromMillimeters(getVerticalPadding() + 2)
            : y;/*from  ww  w. j  a v  a2  s . c om*/
    float dy = (getOrientation() == Orientation.HORIZONTAL)
            ? y + height - PdfUtil.convertFromMillimeters(getVerticalPadding() + 2)
            : y;

    content.setLineWidth(0.5f);
    content.setColorStroke(BaseColor.GRAY);
    content.moveTo(sx, sy);
    content.setLineDash(5.0f, 2.0f, 0.0f);
    content.lineTo(dx, dy);
    content.stroke();
    content.setLineDash(1.0f, 0.0f, 0.0f);
}

From source file:org.javad.stamp.pdf.SetTenant.java

License:Apache License

void drawBorder(PdfContentByte content, OutputBounds rect) {
    content.setColorStroke(BaseColor.BLACK);
    content.setLineWidth(0.8f);/*from   ww w .  j av  a  2  s  . c o  m*/
    content.rectangle(rect.x, rect.y, rect.width, rect.height);
    content.stroke();
}

From source file:org.javad.stamp.pdf.StampBox.java

License:Apache License

/**
 * Will draw a black frame shape for a given stamp box. The current
 * supported shapes include/* w  w w.j a  v  a 2  s.c o m*/
 * <ul><li>rectangle</li>
 * <li>triangle</li>
 * <li>diamond</li>
 * </ul>
 *
 * @param content
 * @param rect
 */
void drawShape(PdfContentByte content, OutputBounds rect) {

    content.setColorFill(BaseColor.WHITE);
    drawPath(content, rect);
    content.fill();
    if (isBorder()) {
        content.setColorStroke(BaseColor.BLACK);
        content.setLineWidth(0.8f);
        drawPath(content, rect);
        content.stroke();
    }
    content.setColorFill(BaseColor.BLACK);
}

From source file:org.javad.stamp.pdf.StampBox.java

License:Apache License

/**
 * Will draw the bisect lines within the content are of the output
 * rectangle.//from ww w . java2 s .  c o  m
 *
 * @param content
 * @param rect
 */
@SuppressWarnings("incomplete-switch")
void drawBisect(PdfContentByte content, OutputBounds rect) {
    content.setLineWidth(0.5f);
    content.setColorStroke(BaseColor.GRAY);
    float dx1 = 0.0f;
    float dx2 = 0.0f;
    float dy1 = 0.0f;
    float dy2 = 0.0f;
    float xp = (int) PdfUtil.convertFromMillimeters(getPadding() + 2);
    float yp = (int) PdfUtil.convertFromMillimeters(getVerticalPadding() + 2);
    switch (bisect) {
    case top_left:
        dx1 = xp;
        dy1 = rect.height - yp;
        dx2 = rect.width - xp;
        dy2 = yp;
        break;
    case top_right:
        dx1 = xp;
        dy1 = yp;
        dx2 = rect.width - xp;
        dy2 = rect.height - yp;
        break;
    case vertical:
        dx1 = rect.width / 2;
        dy1 = yp;
        dx2 = dx1;
        dy2 = rect.height - yp;
    }
    content.moveTo(rect.x + dx1, rect.y + dy1);
    content.setLineDash(5.0f, 2.0f, 0.0f);
    content.lineTo(rect.x + dx2, rect.y + dy2);
    content.stroke();
    content.setLineDash(1.0f, 0.0f, 0.0f);
}

From source file:org.javad.stamp.pdf.StampRow.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }// w w  w .jav  a2  s  . c  o  m
    float maxWidth = 0;
    float top = getY();
    if (getDescription() != null && !getDescription().isEmpty()) {
        content.setColorStroke(BaseColor.BLACK);
        Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.RowDescription);
        content.setFontAndSize(descFont.getBaseFont(), descFont.getSize());
        top -= descFont.getCalculatedSize();
        int count = 0;
        int tc = getDescription().split("\n").length;
        for (String desc : getDescription().split("\n")) {
            maxWidth = Math.max(maxWidth, (int) descFont.getBaseFont().getWidthPoint(desc, descFont.getSize()));
            PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) (maxWidth * 1.10));
            count++;
            top -= descFont.getCalculatedSize() + ((count < tc) ? 2 : 4);

        }
    }
    float totalWidth = 0;
    int maxHeight = 0;
    int count = 0;
    for (int i = 0; i < stampContents.size(); i++) {
        if (stampContents.get(i).isSkipped()) {
            continue;
        }

        totalWidth += stampContents.get(i).getWidth() + stampContents.get(i).getPadding();
        if (count > 0) {
            totalWidth += padding;
        }
        count++;
        maxHeight = Math.max(maxHeight, stampContents.get(i).getHeight());
    }
    totalWidth = PdfUtil.convertFromMillimeters(totalWidth);
    float start_x = getX() - (totalWidth / 2.0f);
    float cur_x = start_x;
    float totalHeight = getY() - top;
    float deltaHeight = totalHeight;
    count = 0;
    for (IStampContent s : stampContents) {
        if (s.isSkipped()) {
            continue;
        }
        if (count > 0) {
            cur_x += PdfUtil.convertFromMillimeters(padding);
        }
        s.setX(cur_x);
        int delta_y = getVerticalAlignmentOffset(s, maxHeight);
        s.setY(top - PdfUtil.convertFromMillimeters(delta_y));
        OutputBounds r = s.generate(content);
        totalHeight = Math.max(r.height + deltaHeight, totalHeight);
        cur_x += r.width;
        count++;
    }
    maxWidth = (int) Math.max(maxWidth, totalWidth);
    return new OutputBounds(start_x, getY(), maxWidth, totalHeight);
}

From source file:org.javad.stamp.pdf.StampSet.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }/*from  ww  w .j  a  v a2 s .  c  o m*/
    float maxWidth = 0;
    content.setColorStroke(BaseColor.BLACK);

    float top = getY();
    if (getIssue() != null && !getIssue().isEmpty()) {
        top -= PdfUtil.convertFromMillimeters(5.0f);
        Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.SetIssue);
        content.setFontAndSize(f.getBaseFont(), f.getSize());
        String is = getIssue().replace("\\n", "\n");
        StringTokenizer tokenizer = new StringTokenizer(is, "\n", true);
        while (tokenizer.hasMoreTokens()) {
            is = tokenizer.nextToken();
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(is, false));
            if (is.equals("\n")) {
                top -= f.getCalculatedSize() + 2;
            } else {
                PdfUtil.renderConstrainedText(content, is, f, getX(), top, (int) maxWidth);
                if (tokenizer.hasMoreTokens()) {
                    top -= f.getCalculatedSize() + 2;
                }
            }

        }
        top -= PdfUtil.convertFromMillimeters(3.0f);
    }
    if (getDescription() != null && !getDescription().isEmpty()) {
        Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetDescription);
        content.setFontAndSize(descFont.getBaseFont(), descFont.getSize());
        top -= descFont.getCalculatedSize();
        int count = 0;
        int tc = getDescription().split("\n").length;
        for (String desc : getDescription().split("\n")) {
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false));
            PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) maxWidth);
            count++;
            if (count < tc) {
                top -= descFont.getCalculatedSize() + 2;
            }

        }
    }
    if (getDescriptionSecondary() != null && !getDescriptionSecondary().isEmpty()) {
        Font secFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetDescriptionSecondary);
        content.setFontAndSize(secFont.getBaseFont(), secFont.getSize());
        top -= secFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(3.0f);
        int count = 0;
        int tc = getDescriptionSecondary().split("\n").length;
        for (String desc : getDescriptionSecondary().split("\n")) {
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false));
            PdfUtil.renderConstrainedText(content, desc, secFont, getX(), top, (int) maxWidth);
            count++;
            if (count < tc) {
                top -= secFont.getCalculatedSize() + 2;
            }

        }
    }

    if (!rows.isEmpty()) {
        top -= ((top != getY()) ? PdfUtil.convertFromMillimeters(3.0f) : 0);
        int count = 0;
        for (int i = 0; i < rows.size(); i++) {
            ISetContent row = rows.get(i);
            if (row.isSkipped()) {
                continue;
            }
            top -= ((count > 0) ? (PdfUtil.convertFromMillimeters(verticalPadding)) : 0.0f);
            row.setX(getX());
            row.setY(top);
            OutputBounds bounds = row.generate(content);
            count++;
            top -= bounds.height;
            maxWidth = Math.max(maxWidth, bounds.width);
        }
    }
    if (getComment() != null && !getComment().isEmpty()) {

        top -= PdfUtil.convertFromMillimeters((int) (0.75 * verticalPadding));
        Font cFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetComment);
        content.setFontAndSize(cFont.getBaseFont(), cFont.getSize());
        for (String s : getComment().split("\n")) {
            top -= cFont.getCalculatedSize();
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(s, false));
            PdfUtil.renderConstrainedText(content, s, cFont, getX(), top, (int) maxWidth);
        }

    }

    OutputBounds rect = new OutputBounds(getX() - maxWidth / 2, getY(), maxWidth, getY() - top);
    return rect;
}

From source file:org.primaresearch.pdf.PageToPdfConverter.java

License:Apache License

/**
 * Draws the outline of the given layout object on the current PDF page. 
 * @param obj/*from w w  w.jav a2 s.  c  o m*/
 * @param canvas
 * @param pageHeight
 */
private void drawLayoutObject(ContentObject obj, PdfContentByte canvas, int pageHeight) {
    if (obj == null)
        return;

    Polygon polygon = obj.getCoords();

    if (polygon == null || polygon.getSize() < 3)
        return;

    canvas.setColorStroke(getOutlineColor(obj.getType()));
    canvas.setLineWidth(1.0f);

    //Move to last point
    Point p = polygon.getPoint(polygon.getSize() - 1);
    canvas.moveTo(p.x, pageHeight - p.y);
    //Now draw all line segments
    for (int i = 0; i < polygon.getSize(); i++) {
        p = polygon.getPoint(i);
        canvas.lineTo(p.x, pageHeight - p.y);
    }
    canvas.stroke();
}

From source file:PDF.CrearPDF_Ficha.java

public static void drawRectangle(PdfContentByte content, float x, float y, float width, float height) {
    try {/*w  w  w.j a  va2 s . c  o m*/

        content.saveState();

        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();

        BaseFont bf = BaseFont.createFont();
        float fontSize = 15f;
        Phrase phrase = new Phrase("Foto", new Font(bf, fontSize));
        ColumnText.showTextAligned(content, Element.ALIGN_CENTER, phrase, 475, 687, 0);
    } catch (DocumentException ex) {
        Logger.getLogger(CrearPDF_Ficha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CrearPDF_Ficha.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:se.billes.pdf.renderer.model.Image.java

License:Open Source License

@Override
public void onRender(PdfContentByte cb) throws PdfRenderException {

    ImageInstance instance = null;//from ww  w. j av a  2 s .c  om
    try {
        instance = new ImageFactory().getImageByFile(cb, file);
    } catch (Exception e) {
        throw new PdfRenderException(e);
    }

    float pageHeight = getPage().getPdfDocument().getSize()[1];
    float pageWidth = getPage().getPdfDocument().getSize()[0];

    float x = 0;
    float y = 0;
    float fitHeight = 0f;
    float imageHeight = 0;
    float imageWidth = 0;
    boolean alignImage = false;

    float[] positions = new BlockFactory().getBoundsInPs(this);

    float width = positions[2];
    float height = positions[3];

    com.itextpdf.text.Image image = instance.getImage();

    int dpiX = image.getDpiX();
    if (dpiX == 0) {
        dpiX = 300;

    }
    if (!new ImageFactory().isPdf(file)) {
        dpiX = getDotsPerInch();
        alignImage = true; // always align jpg
    }

    float realPercent = 72f / dpiX * 100; // only jpg
    if (isScaleToFit()) {
        image.scaleToFit(width, height);
        imageHeight = image.getScaledHeight();
        imageWidth = image.getScaledWidth();
    } else if (isFitContentProportionally()) {
        image.scaleToFit(width, height);
        imageHeight = image.getScaledHeight();
        imageWidth = image.getScaledWidth();
        fitHeight = height - imageHeight;
        alignImage = true;
    } else {
        if (isFillFrameProportionally()) {
            float percentWidth = width / image.getWidth();
            float percentHeight = height / image.getHeight();
            realPercent = Math.max(percentHeight, percentWidth) * 100;
            alignImage = true;
            if (new ImageFactory().isPdf(file)) {
                image.scalePercent(realPercent);
                imageHeight = image.getScaledHeight();
                imageWidth = image.getScaledWidth();
                fitHeight = height - imageHeight;
            }
        }

        if (isCenterImageToPageWidth() && new ImageFactory().isPdf(file)) {

            imageWidth = image.getScaledWidth();
            float middle = (pageWidth / 2) - (imageWidth / 2);
            positions[0] = SizeFactory.PostscriptPointsToMillimeters(middle);
        }

        if (!new ImageFactory().isPdf(file)) {
            image.scalePercent(realPercent);
            imageHeight = image.getScaledHeight();
            imageWidth = image.getScaledWidth();
            fitHeight = height - imageHeight;
        }
    }

    if (alignImage) {
        float[] result = handleAlignment(width, imageWidth, height, imageHeight, fitHeight);
        x = result[0];
        y = result[1];
    }
    try {
        PdfTemplate tp = cb.createTemplate(width, height);
        image.setAbsolutePosition(x, y);
        tp.roundRectangle(0, 0, width, height, SizeFactory.millimetersToPostscriptPoints(getRadius()));
        tp.clip();
        tp.newPath();
        tp.addImage(image);

        float left = getPosition()[0];
        float top = getPosition()[1];
        if (getPage().getPdfDocument().getCutmarks() != null) {
            left += SizeFactory.CUT_MARK;
            top -= SizeFactory.CUT_MARK;
        }

        cb.addTemplate(tp, SizeFactory.millimetersToPostscriptPoints(left),
                SizeFactory.millimetersToPostscriptPoints(pageHeight - (top + getPosition()[3])));
        if (getBorder() != null) {
            cb.setLineWidth(SizeFactory.millimetersToPostscriptPoints(getBorder().getThickness()));
            cb.setColorStroke(getBorder().getBaseColor());
            cb.roundRectangle(SizeFactory.millimetersToPostscriptPoints(left),
                    SizeFactory.millimetersToPostscriptPoints(pageHeight - (top + getPosition()[3])), width,
                    height, SizeFactory.millimetersToPostscriptPoints(getRadius()));
            cb.stroke();
        }

    } catch (Exception e) {
        throw new PdfRenderException(e);
    }

}