How to draw a borderless rounded rectangle?

Tags: rounded cornersrectangleborderiText 5

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"));;
canvas = pdfw.getDirectContent();
canvas.roundRectangle(100, 100, 100, 100, 10);
canvas.rectangle(100, 210, 100, 100);
canvas.roundRectangle(210, 100, 100, 100, 10);
canvas.rectangle(210, 210, 100, 100);
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.


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(llx,lly, wid, hei, 10);

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:


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():


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


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.