Add Arc Shapes to PDF in Java

Aspose.PDF for Java uses Graph together with shape objects such as Arc and Line to render vector graphics.

Add arc outlines

  1. Create a new PDF Document.
  2. Add a Page to the document.
  3. Create a Graph container and add it to the page.
  4. Create the Arc shape and configure its geometry.
  5. Add the Arc to the Graph container.
  6. Set the shape properties required by the example, including Color.
  7. Save the output PDF Document.
public static void addArc(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Graph graph = new Graph(400.0, 400.0);
        graph.setBorder(new BorderInfo(BorderSide.All, Color.getGreen()));

        Arc arc1 = new Arc(100, 100, 95, 0, 90);
        arc1.getGraphInfo().setColor(Color.getGreenYellow());
        graph.getShapes().addItem(arc1);

        page.getParagraphs().add(graph);
        document.save(outputFile.toString());
    }
}

The full example adds three arcs with different radii, angles, and colors to the same graph.

Add a filled arc segment

  1. Create a new PDF Document.
  2. Add a Page to the document.
  3. Create a Graph container and add it to the page.
  4. Create the Line shape and configure its coordinates.
  5. Create the Arc shape and configure its geometry.
  6. Add the Line and Arc to the Graph container.
  7. Save the output PDF Document.
public static void addArcFilled(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Graph graph = new Graph(400.0, 400.0);
        graph.setBorder(new BorderInfo(BorderSide.All, Color.getGreen()));

        Arc arc = new Arc(100, 100, 95, 0, 90);
        arc.getGraphInfo().setFillColor(Color.getGreenYellow());
        graph.getShapes().addItem(arc);

        Line line = new Line(new float[]{195, 100, 100, 100, 100, 195});
        line.getGraphInfo().setFillColor(Color.getGreenYellow());
        graph.getShapes().addItem(line);

        page.getParagraphs().add(graph);
        document.save(outputFile.toString());
    }
}