Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding of filled circles to PDF page using Apache PDFBox

I'm trying to use the Apache PDFBox library to create a PDF document programmatically. The class PDPageContentStream contains methods to write text, draw lines, bezier curves, rectangles. But I can't find a way to draw a simple filled circle. Is there a way to draw it using this library? If not, can you please suggest a free Java library that provides flexible API to create PDF documents programmatically? Thanks in advance.

like image 911
Victor Semenovich Avatar asked Dec 18 '22 08:12

Victor Semenovich


1 Answers

OK, thanks everyone for responses. I like the solution with bezier curves. This approach works for me:

private void drawCircle(PDPageContentStream contentStream, int cx, int cy, int r, int red, int green, int blue) throws IOException {
    final float k = 0.552284749831f;
    contentStream.setNonStrokingColor(red, green, blue);
    contentStream.moveTo(cx - r, cy);
    contentStream.curveTo(cx - r, cy + k * r, cx - k * r, cy + r, cx, cy + r);
    contentStream.curveTo(cx + k * r, cy + r, cx + r, cy + k * r, cx + r, cy);
    contentStream.curveTo(cx + r, cy - k * r, cx + k * r, cy - r, cx, cy - r);
    contentStream.curveTo(cx - k * r, cy - r, cx - r, cy - k * r, cx - r, cy);
    contentStream.fill();
}
like image 105
Victor Semenovich Avatar answered Jan 12 '23 14:01

Victor Semenovich