如何创建自定义虚线分隔符? [英] How to create a custom dashed line separator?

查看:230
本文介绍了如何创建自定义虚线分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用DottedLineSeparator类我可以在itext中绘制虚线分隔符。同样可以在PdfPCell中绘制像'---------------------'这样的连续连字符作为分隔符吗?

Using DottedLineSeparator class i am able to draw dotted line separator in itext. Similarly is it possible to draw continuous hyphens like '---------------------' as separator in a PdfPCell?

我查看了此处的示例。是否有其他解决方案?

I checked the example here. Is any other solutions available?

推荐答案

可以使用 LineSeparator 类绘制一条实线水平线。或者相当于HTML中的< hr> 标记,或者作为同一行中两部分文本之间的分隔符。 DottedLineSeparator 扩展 LineSeparator 类并绘制虚线而不是实线。您可以通过更改线宽来定义点的大小,并获得一种方法来定义点之间的间隙。

The LineSeparator class can be used to draw a solid horizontal line. Either as the equivalent of the <hr> tag in HTML, or as a separator between two parts of text on the same line. The DottedLineSeparator extends the LineSeparator class and draws a dotted line instead of a solid line. You can define the size of the dots by changing the line width and you get a method to define the gap between the dots.

您需要一条虚线,这很容易创建自己的 LineSeparator 实现。最简单的方法是扩展 DottedLineSeparator 类,如下所示:

You require a dashed line and it's very easy to create your own LineSeparator implementation. The easiest way to do this, is by extending the DottedLineSeparator class like this:

class CustomDashedLineSeparator extends DottedLineSeparator {
    protected float dash = 5;
    protected float phase = 2.5f;

    public float getDash() {
        return dash;
    }

    public float getPhase() {
        return phase;
    }

    public void setDash(float dash) {
        this.dash = dash;
    }

    public void setPhase(float phase) {
        this.phase = phase;
    }

    public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
        canvas.saveState();
        canvas.setLineWidth(lineWidth);
        canvas.setLineDash(dash, gap, phase);
        drawLine(canvas, llx, urx, y);
        canvas.restoreState();
    }
}

如您所见,我们引入了两个额外的参数, 破折号值和阶段值。 破折号值定义连字符的长度。 阶段值告诉iText从哪里开始(例如从半个连字符开始)。

As you can see, we introduce two extra parameters, a dash value and a phase value. The dash value defines the length of the hyphen. The phase value tells iText where to start (e.g. start with half a hyphen).

请看一下 CustomDashedLine 示例。在这个例子中,我使用 LineSeparator 的这个自定义实现,如下所示:

Please take a look at the CustomDashedLine example. In this example, I use this custom implementation of the LineSeparator like this:

CustomDashedLineSeparator separator = new CustomDashedLineSeparator();
separator.setDash(10);
separator.setGap(7);
separator.setLineWidth(3);
Chunk linebreak = new Chunk(separator);
document.add(linebreak);

结果是一条虚线,连字符长10pt,厚3pt,间隙为7pt。第一个短划线只有7.5pt长,因为我们没有更改阶段值。在我们的自定义实现中,我们定义了一个2.5pt的阶段,这意味着我们在2.5pt时开始10pt的连字符,产生一个长度为7.5pt的连字符。

The result is a dashed line with hyphens of 10pt long and 3pt thick, with gaps of 7pt. The first dash is only 7.5pt long because we didn't change the phase value. In our custom implementation, we defined a phase of 2.5pt, which means that we start the hyphen of 10pt at 2.5pt, resulting in a hyphen with length 7.5pt.

您可以使用此自定义 LineSeparator 与使用 DottedLineSeparator 的方式相同,例如在 PdfPCell 中作为

You can use this custom LineSeparator in the same way you use the DottedLineSeparator, e.g. as a Chunk in a PdfPCell.

这篇关于如何创建自定义虚线分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆