Skip to main content
Skip table of contents

How to draw a borderless rounded rectangle?

Is it possible to have a rounded Rectangle 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.

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:


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.

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())
            .roundRectangle(100, 100, 100, 100, 10)
            .rectangle(100, 210, 100, 100)
            .roundRectangle(210, 100, 100, 100, 10)
            .rectangle(210, 210, 100, 100)

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

Click How to draw a borderless rounded rectangle? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.