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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With