Add Circle Shapes to PDF in Java
Contents
[
Hide
]
Add a circle outline
- Create a new PDF Document.
- Add a Page to the document.
- Create a Graph container and add it to the page.
- Create the Circle shape and configure its geometry.
- Add the Circle to the Graph container.
- Set the shape properties required by the example, including Color.
- Save the output PDF Document.
public static void addCircle(Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
Graph graph = new Graph(400.0, 200.0);
graph.setBorder(new BorderInfo(BorderSide.All, Color.getGreen()));
Circle circle = new Circle(100, 100, 40);
circle.getGraphInfo().setColor(Color.getGreenYellow());
graph.getShapes().addItem(circle);
page.getParagraphs().add(graph);
document.save(outputFile.toString());
}
}
Add a filled circle with text
- Create a new PDF Document.
- Add a Page to the document.
- Create a Graph container and add it to the page.
- Create the Circle shape and configure its geometry.
- Add the Circle to the Graph container.
- Set the shape properties required by the example, including Color and TextFragment.
- Save the output PDF Document.
public static void addCircleFilled(Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
Graph graph = new Graph(400.0, 200.0);
graph.setBorder(new BorderInfo(BorderSide.All, Color.getGreen()));
Circle circle = new Circle(100, 100, 40);
circle.getGraphInfo().setColor(Color.getGreenYellow());
circle.getGraphInfo().setFillColor(Color.getGreen());
circle.setText(new TextFragment("Circle"));
graph.getShapes().addItem(circle);
page.getParagraphs().add(graph);
document.save(outputFile.toString());
}
}