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;
}
}