0

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?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Subhu.rys
  • 25
  • 1
  • 7
  • I'm about to answer your question, but I'm in doubt. Do you want to draw a dashed line similar to what you'd do with the `DottedLineSeparator`, or do you want to create a `PdfPCell` with a dashed line border. The former requires me to write an example, the latter is already explained here: [Dotted Line for cell border](http://stackoverflow.com/questions/20117321/dotted-line-for-cell-border). If you study the example, you'll see that I created a border that consists of hyphens. – Bruno Lowagie Jan 03 '15 at 10:26
  • Intention was to draw dashed line. Similar to DottedLineSeparator and i could see your implementation below. Based on your comment on considering PdfPCell border to draw dashed line, i would like to try it following the [link](http://stackoverflow.com/questions/20117321/dotted-line-for-cell-border) shared. thanks. – Subhu.rys Jan 07 '15 at 12:47

2 Answers2

3

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.

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

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

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.

enter image description here

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0
double wPage = doc.PageSize.Width;
double hPage = doc.PageSize.Height;

cb.MoveTo(0,hPage/2);
for (int i = 0; i < wPage; i+=5)
{
    cb.LineTo(i, hPage / 2);
    i += 5;
    cb.MoveTo(i, hPage / 2);
}

this is a solution too :))

RalfFriedl
  • 1,134
  • 3
  • 11
  • 12
  • How exactly does this easily be used *as separator in a PdfPCell*? Drawing a line is not the real problem here. Automatically drawing one in content itext is asked to layout automatically, that's what this question is about at its core. And by the way, drawing dotted lines like you do puts a larger burden on pdf viewers than doing it like bruno using `canvas.setLineDash`. – mkl Jun 09 '19 at 18:18