How to create a custom dashed line separator?

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

Posted on StackOverflow on Jan 3, 2015 by Retalemine

In iText 7 DottedLineSeparator class does not exist anymore, but we have DottedLine class. The LineSeparator class can be used to draw a solid horizontal line. Either as the equivalent of the

tag in HTML, or as a separator between two parts of text on the same line. The DottedLine class draws a dotted 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.


You require a dashed line and it's very easy to create your own Line implementation. The easiest way to do this, is by extending the DottedLine 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.setLineDash(dash, gap, phase);
        drawLine(canvas, llx, urx, y);

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).

Please take a look at the CustomDashedLine example. In this example, we use it next way:

CustomDashedLine customLine= new CustomDashedLine();
doc.add(new LineSeparator(customLine));

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.

Custom dashed line

Custom dashed line

You can use this custom CustomDashedLine in the same way you use the DottedLine, e.g. as a Text in a Cell (equivalents for Chunks and PdfPCells in iText 5).

Click this link if you want to see how to answer this question in iText 5.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now