John
John

Reputation: 45

How to draw a filled polygon in PDFBOX?

I want to draw a filled polygon. I had looked into the documentation and the method:

fillPolygon(float[] x, float[] y)
Deprecated. 
Use moveTo(float, float) and lineTo(float, float) methods instead.

I cant find an alternative way to fill a polygon in pdfbox.

Upvotes: 1

Views: 403

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18861

Derived from the PDFBox source code:

public void fillPolygon(PDPageContentStream cs, float[] x, float[] y) throws IOException
{
    if (x.length != y.length)
    {
        throw new IllegalArgumentException("Error: some points are missing coordinate");
    }
    for (int i = 0; i < x.length; i++)
    {
        if (i == 0)
        {
            cs.moveTo(x[i], y[i]);
        }
        else
        {
            cs.lineTo(x[i], y[i]);
        }
    }
    cs.closePath();
    cs.fill();
}

Upvotes: 3

Related Questions