Create Graphs and Charts in PDF in Java

Graphs and charts are used to visually represent the data in PDF files. Furthermore, you can use them to graphically describe the flow of the activities or operations in a system such as flow charts. In this article, you will learn how to create graphs and charts in PDF files programmatically in Java. We will explicitly cover how to create different graphical objects in a PDF file.

Java API to Create Graphs and Charts in PDF

Aspose.PDF for Java is an amazing API that provides a range of features for PDF generation and manipulation. Using the API, you can seamlessly create PDF files of simple and complex layouts. We will use this API to create graphs and charts in PDF files. You can download the API or install it using the following Maven configurations.

Repository:
<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>22.4</version>
</dependency>

Create an Arc in a PDF in Java

The following are the steps to add an arc in PDF in Java.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Arc class and set its dimensions and other parameters.
  5. Add arc to the shapes collection of Graph using Graph.getShapes().add(Arc) method.
  6. Add graph to the page using Page.getParagraphs().add(Graph) method.
  7. Save the PDF file using Document.save(String) method.

The following code sample shows how to add arcs in a PDF in Java.

The following are the arcs that we have generated using the above code sample.

Create an Arc in PDF in Java

Create a Circle in a PDF in Java

The following are the steps to add a circle to a PDF file in Java.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Circle class and set its dimensions.
  5. Add circle to the shapes collection of Graph using Graph.getShapes().add(Circle) method.
  6. Add graph to the page using Page.getParagraphs().add(Graph) method.
  7. Save the PDF file using Document.save(String) method.

The following code sample shows how to add a circle in PDF in Java.

The following is the circle that we have generated using the above code sample.

Create a Circle in PDF in Java

Add a Curve in a PDF in Java

The following are the steps to add a curve in a PDF file in Java.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Curve class and set its dimensions.
  5. Add curve to the shapes collection of Graph using Graph.getShapes().add(Curve) method.
  6. Add graph to the page using Page.getParagraphs().add(Graph) method.
  7. Save the PDF file using Document.save(String) method.

The following code sample shows how to add curves to a PDF file in Java.

The following are the curves that we have generated using the above code sample.

Add a Curve in PDF in Java

Add a Line in a PDF in Java

The following are the steps to add a line in a PDF using Java.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Line class and set its dimensions.
  5. Set other properties of line such as style, width, etc.
  6. Add line to the shapes collection of Graph using Graph.getShapes().add(Line) method.
  7. Add graph to the page using Page.getParagraphs().add(Graph) method.
  8. Save the PDF file using Document.save(String) method.

The following code sample shows how to add lines in PDF in Java.

The following are the lines that we have created using the above code sample.

Add a Line in PDF in Java

Create a Rectangle in PDF in Java

Similar to other graphical objects, you can add a rectangle to the PDF. Also, you can fill the rectangle with a certain color, control Z-Order, add gradient color, etc. The following are the steps to create a rectangle in a PDF file in Java.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Rectangle class and set its dimensions.
  5. Set other properties of the rectangle such as fill color, gradient, etc.
  6. Add rectangle to the shapes collection of Graph using Graph.getShapes().add(Rectangle) method.
  7. Add graph to the page using Page.getParagraphs().add(Graph) method.
  8. Save the PDF file using Document.save(String) method.

The following code sample shows how to add rectangles in PDF in Java.

The following are the rectangles that we have created using the above code sample.

Create a Rectangle in PDF in Java

Create an Ellipse in PDF in Java

Last but not the least, let’s have a look at how to create an ellipse in PDF in Java.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Ellipse class and set its dimensions.
  5. Set other properties of ellipse such as fill color, etc.
  6. Add ellipse to the shapes collection of Graph using Graph.getShapes().add(Ellipse) method.
  7. Add graph to the page using Page.getParagraphs().add(Graph) method.
  8. Save the PDF file using Document.save(String) method.

The following code sample shows how to add an ellipse in a PDF file in Java.

The following are the ellipses that we have created using the above code sample.

Create an Ellipse in PDF in Java

Get a Free License

You can get a free temporary license in order to use Aspose.PDF for Java without evaluation limitations.

Conclusion

In this article, you have learned how to create different objects of charts and graphs in PDF files using Java. You have seen how to add arcs, circles, rectangles, lines, curves, and ellipses in a PDF. Furthermore, you can explore more about the Java PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.

See Also