itext在pdf中画虚线

itext 5 中已经有了实线和点线的实现类,没有虚线的实现类,但是实际运用中碰到了需要使用虚线的场景,需要做小小的修改就可以实现。如下:

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfChunk;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;

public class DashLineSeparator extends VerticalPositionMark {

    /** 线的粗细. */
    protected float lineWidth = 1;

    /** 线占页面宽度的百分比 */
    protected float percentage = 100;

    /** 线色. */
    protected BaseColor lineColor;

    /** 线的对齐方式. */
    protected int alignment = Element.ALIGN_BOTTOM;

    public DashLineSeparator(float lineWidth, float percentage, BaseColor lineColor, int align, float offset) {
        this.lineWidth = lineWidth;
        this.percentage = percentage;
        this.lineColor = lineColor;
        this.alignment = align;
        this.offset = offset;
    }

    /**
     * Creates a new instance of the DashLineSeparator class.
     * 
     * @param font the font
     */
    public DashLineSeparator(Font font) {
        this.lineWidth = PdfChunk.UNDERLINE_THICKNESS * font.getSize();
        this.offset = PdfChunk.UNDERLINE_OFFSET * font.getSize();
        this.percentage = 100;
        this.lineColor = font.getColor();
    }

    /**
     * Creates a new instance of the LineSeparator class with default values:
     * lineWidth 1 user unit, width 100%, centered with offset 0.
     */
    public DashLineSeparator() {
    }

    /**
     * @see com.itextpdf.text.pdf.draw.DrawInterface#draw(com.itextpdf.text.pdf.PdfContentByte,
     *      float, float, float, float, float)
     */
    public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
        canvas.saveState();
        drawLine(canvas, llx, urx, y);
        canvas.restoreState();
    }

    /**
     * Draws a horizontal line.
     * 
     * @param canvas the canvas to draw on
     * @param leftX the left x coordinate
     * @param rightX the right x coordindate
     * @param y the y coordinate
     */
    public void drawLine(PdfContentByte canvas, float leftX, float rightX, float y) {
        float w;
        if (getPercentage() < 0)
            w = -getPercentage();
        else
            w = (rightX - leftX) * getPercentage() / 100.0f;
        float s;
        switch (getAlignment()) {
        case Element.ALIGN_LEFT:
            s = 0;
            break;
        case Element.ALIGN_RIGHT:
            s = rightX - leftX - w;
            break;
        default:
            s = (rightX - leftX - w) / 2;
            break;
        }
        canvas.setLineWidth(getLineWidth());
        if (getLineColor() != null)
            canvas.setColorStroke(getLineColor());
        canvas.setLineDash(3, 3, 0);
        canvas.moveTo(s + leftX, y + offset);
        canvas.lineTo(s + w + leftX, y + offset);
        canvas.stroke();
    }

    /**
     * Getter for the line width.
     * 
     * @return the thickness of the line that will be drawn.
     */
    public float getLineWidth() {
        return lineWidth;
    }

    /**
     * Setter for the line width.
     * 
     * @param lineWidth the thickness of the line that will be drawn.
     */
    public void setLineWidth(float lineWidth) {
        this.lineWidth = lineWidth;
    }

    /**
     * Setter for the width as a percentage of the available width.
     * 
     * @return a width percentage
     */
    public float getPercentage() {
        return percentage;
    }

    /**
     * Setter for the width as a percentage of the available width.
     * 
     * @param percentage a width percentage
     */
    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    /**
     * Getter for the color of the line that will be drawn.
     * 
     * @return a color
     */
    public BaseColor getLineColor() {
        return lineColor;
    }

    /**
     * Setter for the color of the line that will be drawn.
     * 
     * @param color a color
     */
    public void setLineColor(BaseColor color) {
        this.lineColor = color;
    }

    /**
     * Getter for the alignment of the line.
     * 
     * @return an alignment value
     */
    public int getAlignment() {
        return alignment;
    }

    /**
     * Setter for the alignment of the line.
     * 
     * @param align an alignment value
     */
    public void setAlignment(int align) {
        this.alignment = align;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值