Adding vector graphics to PDF files is a common requirement for generating reports, invoices, and interactive documents. Conholdate.Total for Java is a powerful SDK that simplifies PDF manipulation on the server side. In this guide you will learn how to insert rectangles, ellipses, and lines into a PDF using a Graph object, configure their appearance, and handle performance considerations, all with clear Java code examples.
Steps to Add Shapes to PDF in Java
Add Maven Repository and Dependency - Include the Conholdate Maven repository and the
conholdate-totaldependency in yourpom.xml. This makes the SDK classes such asDocument,Page, andGraphavailable.<repositories> <repository> <id>conholdate-repo</id> <name>Conholdate Maven Repository</name> <url>https://repository.conholdate.com/repo/</url> </repository> </repositories> <dependency> <groupId>com.conholdate</groupId> <artifactId>conholdate-total</artifactId> <version>24.9</version> <type>pom</type> </dependency>Create the PDF Document and a Page - Create an instance of
Documentand add a page to its pages collection. The class is documented in the API reference.// Create Document instance Document doc = new Document(); // Add page to pages collection of PDF file com.aspose.pdf.Page page = doc.getPages().add();Create Shape Objects - Create a
Graphobject first, then instantiate rectangles, ellipses, or lines and add them to its shapes collection. Set position, size, and visual attributes through each shape’sGraphInfo.// Create Graph instance com.aspose.pdf.drawing.Graph graph = new com.aspose.pdf.drawing.Graph(100, 600); // Rectangle com.aspose.pdf.drawing.Rectangle rect = new com.aspose.pdf.drawing.Rectangle(100, 100, 170, 120); graph.getShapes().add(rect); // Ellipse com.aspose.pdf.drawing.Ellipse ellipse = new com.aspose.pdf.drawing.Ellipse(300, 150, 150, 100); ellipse.getGraphInfo().setColor(com.aspose.pdf.Color.getGreen()); ellipse.getGraphInfo().setLineWidth(1.5f); graph.getShapes().add(ellipse); // Line com.aspose.pdf.drawing.Line line = new com.aspose.pdf.drawing.Line((new float[]{100, 300, 400, 300})); line.getGraphInfo().setColor(com.aspose.pdf.Color.getRed()); line.getGraphInfo().setLineWidth(3); graph.getShapes().add(line);Add Shapes to a Page - The individual shapes live inside the
Graphobject’s shapes collection. Add theGraphitself to the page’s paragraphs collection so all the shapes it contains are rendered on that page.// Add graph object to paragraphs collection of page instance page.getParagraphs().add(graph);Save the Updated PDF - After the graph (with all its shapes) has been added to the page, save the document to a new file.
// Save PDF file doc.save("Shapes.pdf");
Adding Shapes to PDF in Java - Complete Code Example
The following example puts all steps together into a single, ready‑to‑run program.
import com.aspose.pdf.Document;
import com.aspose.pdf.Color;
public class AddShapesDemo {
public static void main(String[] args) throws Exception {
// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
com.aspose.pdf.Page page = doc.getPages().add();
// Create Graph instance
com.aspose.pdf.drawing.Graph graph = new com.aspose.pdf.drawing.Graph(100, 600);
// Create Rectangle instance
com.aspose.pdf.drawing.Rectangle rect = new com.aspose.pdf.drawing.Rectangle(100, 100, 170, 120);
// Add rectangle object to shapes collection of Graph object
graph.getShapes().add(rect);
// Create ellipse
com.aspose.pdf.drawing.Ellipse ellipse = new com.aspose.pdf.drawing.Ellipse(300, 150, 150, 100);
ellipse.getGraphInfo().setColor(com.aspose.pdf.Color.getGreen());
ellipse.getGraphInfo().setLineWidth(1.5f);
// Add ellipse object to shapes collection of Graph object
graph.getShapes().add(ellipse);
// Create line
com.aspose.pdf.drawing.Line line = new com.aspose.pdf.drawing.Line((new float[]{100, 300, 400, 300}));
line.getGraphInfo().setColor(com.aspose.pdf.Color.getRed());
line.getGraphInfo().setLineWidth(3);
// Add line object to shapes collection of Graph object
graph.getShapes().add(line);
// Add graph object to paragraphs collection of page instance
page.getParagraphs().add(graph);
// Save PDF file
doc.save("Shapes.pdf");
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the output file path (
Shapes.pdf) to match your actual target location, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Installation and Setup in Java
To start using Conholdate.Total for Java, download the latest release from the official site and add the Maven dependency shown earlier. The SDK works on any Java 8+ runtime and does not require additional native libraries.
- Download URL: Conholdate.Total for Java Release
- Documentation: Detailed usage instructions are available in the official documentation.
- License: Obtain a temporary license from the temporary license page or view full pricing on the pricing page.
Key Features and Overview
Add Shapes to PDF in Java with Conholdate.Total
Conholdate.Total for Java provides a unified API for creating and editing PDF content. The shape‑drawing functionality works with vector graphics through the Graph object and its shapes collection, ensuring that added elements remain crisp at any zoom level. You can draw basic primitives (rectangle, ellipse, line) and combine several of them inside a single Graph before placing it on a page.
Conholdate.Total Features That Matter For This Task
- Cross‑platform compatibility: Works on Windows, Linux, and macOS servers.
- High‑performance rendering: Shapes are rendered using the same engine that generates native PDF content, avoiding rasterization.
- Full control over appearance: Set stroke color and line width for each shape through its
GraphInfoobject. - Grouped graphics: Add multiple shapes to a single
Graphobject so they can be positioned and managed together on the page.
Configuring Shape Properties for Optimal Rendering
When adding shapes, consider the following properties to achieve the desired visual result:
- Position and Size: Shape constructors take coordinates and dimensions in points, e.g.
new Rectangle(100, 100, 170, 120)ornew Ellipse(300, 150, 150, 100). - Graph Placement: The
Graphobject itself is positioned withnew Graph(width, height)and added to a page throughpage.getParagraphs().add(graph). - Colors: Use the
com.aspose.pdf.Colorclass (for exampleColor.getGreen()orColor.getRed()) and set it on a shape viashape.getGraphInfo().setColor(...). - Line Width: Set with
shape.getGraphInfo().setLineWidth(...), measured in points; a value of1equals 1/72 inch. - Lines: A
Lineshape is created from an array of coordinates, e.g.new Line(new float[]{100, 300, 400, 300}), representing the start and end points of the line.
Example of setting shape appearance:
ellipse.getGraphInfo().setColor(com.aspose.pdf.Color.getGreen());
ellipse.getGraphInfo().setLineWidth(1.5f);
Performance Considerations When Adding Shapes to PDFs
Adding many vector objects can increase processing time and memory usage. Follow these guidelines:
- Batch Drawing: Group related shapes into a single
Graphobject before adding that graph to the page. - Reuse Objects: If you need identical shapes on multiple pages, reuse shape configuration logic instead of duplicating it.
- Avoid Over‑Scaling: Define shapes at the final display size to prevent costly raster conversions.
- Dispose Resources: Save and release the
Documentpromptly once all shapes have been added.
Conclusion
Conholdate.Total for Java gives Java developers a straightforward way to add shapes to PDF documents, enabling the creation of rich, interactive reports and invoices. By following the steps, configuration tips, and performance guidelines in this guide, you can integrate vector graphics into your PDF workflow with confidence. Remember to secure a proper license for production use; a temporary license is available for evaluation, and full pricing details are listed on the pricing page.
FAQs
Do I need to create a Graph object for every shape?
No. A single Graph object can hold multiple shapes — rectangles, ellipses, and lines can all be added to the same graph’s shapes collection before the graph is added to the page.
Is it possible to add shapes to a specific page of a multi‑page PDF?
Yes. Retrieve the target page from doc.getPages() and call page.getParagraphs().add(graph) on that specific Page instance.
What file formats can I export to after adding shapes?
Conholdate.Total for Java’s Document.save() method can write to PDF as well as other formats supported by the SDK. Use doc.save("Shapes.pdf") to save the result as a standard PDF file.
