How to draw a borderless rounded rectangle?

Tags: rounded cornersrectangleborderiText 7

Is it possible to have a roundRectangle() with no border? I create rounded rectangles and rectangles with border sizes of 0f and 1f. For the rounded rectangles there is still a visible border when the line width is set to 0f, but this is not true for a rectangle with a border of 0f.

Here is the code I'm using:

magazine = new Document(PageSize.LETTER,0,0,0,0);
pdfw = PdfWriter.getInstance(magazine, new FileOutputStream("out.pdf"));
magazine.open();
canvas = pdfw.getDirectContent();
canvas.setColorStroke(BaseColor.BLACK);
canvas.setColorFill(BaseColor.GRAY);
canvas.setLineWidth(1f);
canvas.roundRectangle(100, 100, 100, 100, 10);
canvas.fillStroke();
canvas.rectangle(100, 210, 100, 100);
canvas.fillStroke();
canvas.setColorStroke(BaseColor.BLACK);
canvas.setColorFill(BaseColor.WHITE);
canvas.setLineWidth(0f);
canvas.roundRectangle(210, 100, 100, 100, 10);
canvas.fillStroke();
canvas.rectangle(210, 210, 100, 100);
canvas.fillStroke();
Posted on StackOverflow on Jul 5, 2015 by tsmithl

When you draw lines and shapes in PDF, you use path construction operators. The following method introduces an re (rectangle) operator to construct a rectangle.

canvas.rectangle(x, y, width, height);

iText also provides convenience methods. For instance: the following method introduces a sequence of m (move to), l (line to), c (curve to),... operators:

canvas.roundRectangle(x, y, width, height, radius);

As soon as you have constructed the path, you can use a path painting operator to actually draw something. iText has different fill(), stroke() and fillStroke() variations.

You are using this method:

canvas.fillStroke();

This means that you fill the path with the fill color and stroke the path with the stroke color. In your question, you indicate that you only want to fill the path (you want to color what's inside the rounded rectangle); you do not want to stroke it (you don't want to draw the border of the rounded rectangle).

This is easy to achieve. Just replace fillStroke() by fill():

canvas.fill();

Now you will only fill the rounded rectangle and not stroke its border.

mkl:

The PDF specification states: A line width of 0 shall denote the thinnest line that can be rendered at device resolution: 1 device pixel wide.

Indeed, it's a common misconception that changing the width of a line to 0 means that the line isn't drawn when invoking stroke(). If you don't want to see a line, the solution is simple: don't stroke it.

In iText 7 your code can look like this:

public void createPdf(String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage())
            .setStrokeColor(Color.BLACK)
            .setFillColor(Color.GRAY)
            .roundRectangle(100, 100, 100, 100, 10)
            .fill()
            .rectangle(100, 210, 100, 100)
            .fill()
            .setFillColor(Color.WHITE)
            .setLineWidth(0)
            .roundRectangle(210, 100, 100, 100, 10)
            .stroke()
            .rectangle(210, 210, 100, 100)
            .stroke();
    pdfDoc.close();
}

This peace of code shows 4 different ways of creating rectangles.

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