I use itextsharp and I need to draw a dotted line break from the left to the right of the page (100% width) but I don't know how to do this. The document always has a margin to the left and the right.

Chunk linebreak = new Chunk(new DottedLineSeparator());
doc.Add(linebreak);  

Posted on StackOverflow on Nov 27, 2013 by nam vo

Please take a look at the example FullDottedLine. You're creating a DottedLineSeparator of which the width percentage is 100% by default. This 100% is the full available width within the margins of the page. If you want the line to exceed the available width, you need a percentage that is higher than 100%.

In iText 7 DottedLineSeparator class does not exist anymore, but we have DottedLine class that draws a dotted line. You require a custom width of the line depending on the page size. The easiest way to set it, is by extending the DottedLine class and introducing an extra parameter:

class CustomDottedLine extends DottedLine {
    protected Rectangle pageSize;

    public CustomDottedLine(Rectangle pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public void draw(PdfCanvas canvas, Rectangle drawArea) {
        super.draw(canvas, new Rectangle(pageSize.getLeft(), drawArea.getBottom(), pageSize.getWidth(), drawArea.getHeight()));
    }
}

Take a look at the resulting pdf and you'll see that the line now runs through the margins.

Click How to add a full line break? if you want to see how to answer this question in iText 5.