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, configure their appearance, and handle performance considerations, all with clear Java code examples.

Steps to Add Shapes to PDF in Java

  1. Add Maven Repository and Dependency - Include the Conholdate Maven repository and the conholdate-total dependency in your pom.xml. This makes the SDK classes such as PdfDocument and ShapeFactory available.

    <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>
    
  2. Load the PDF Document - Create an instance of PdfDocument and open the target PDF file. The class is documented in the API reference.

    PdfDocument pdf = new PdfDocument();
    pdf.open("input.pdf");
    
  3. Create Shape Objects - Use ShapeFactory to instantiate rectangles, ellipses, or lines. Set position, size, and visual attributes.

    // Rectangle
    Shape rect = ShapeFactory.createRectangle(100, 150, 200, 100);
    rect.setFillColor(Color.BLUE);
    rect.setStrokeColor(Color.BLACK);
    rect.setLineWidth(2);
    
    // Ellipse
    Shape ellipse = ShapeFactory.createEllipse(350, 150, 150, 100);
    ellipse.setFillColor(Color.GREEN);
    ellipse.setStrokeColor(Color.DARK_GRAY);
    ellipse.setLineWidth(1.5f);
    
    // Line
    Shape line = ShapeFactory.createLine(100, 300, 500, 300);
    line.setStrokeColor(Color.RED);
    line.setLineWidth(3);
    
  4. Add Shapes to a Page - Retrieve the desired page from the document and add each shape to its graphics collection.

    Page page = pdf.getPages().get_Item(0); // first page
    page.getGraphics().addShape(rect);
    page.getGraphics().addShape(ellipse);
    page.getGraphics().addShape(line);
    
  5. Save the Updated PDF - After all shapes are added, save the document to a new file.

    pdf.save("output.pdf");
    pdf.close();
    

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.conholdate.total.pdf.*;
import com.conholdate.total.pdf.shapes.*;
import java.awt.Color;

public class AddShapesDemo {
    public static void main(String[] args) throws Exception {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument();
        pdf.open("input.pdf");

        // Create rectangle
        Shape rectangle = ShapeFactory.createRectangle(100, 150, 200, 100);
        rectangle.setFillColor(Color.BLUE);
        rectangle.setStrokeColor(Color.BLACK);
        rectangle.setLineWidth(2);

        // Create ellipse
        Shape ellipse = ShapeFactory.createEllipse(350, 150, 150, 100);
        ellipse.setFillColor(Color.GREEN);
        ellipse.setStrokeColor(Color.DARK_GRAY);
        ellipse.setLineWidth(1.5f);

        // Create line
        Shape line = ShapeFactory.createLine(100, 300, 500, 300);
        line.setStrokeColor(Color.RED);
        line.setLineWidth(3);

        // Add shapes to the first page
        Page page = pdf.getPages().get_Item(0);
        page.getGraphics().addShape(rectangle);
        page.getGraphics().addShape(ellipse);
        page.getGraphics().addShape(line);

        // Save the result
        pdf.save("output.pdf");
        pdf.close();
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (input.pdf, output.pdf) to match your actual file locations, 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.

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, ensuring that added elements remain crisp at any zoom level. You can draw basic primitives (rectangle, ellipse, line) as well as complex paths.

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 fill colors, stroke colors, line widths, opacity, and blend modes.
  • Layered graphics: Add shapes to specific layers or groups for easier later manipulation.

Configuring Shape Properties for Optimal Rendering

When adding shapes, consider the following properties to achieve the desired visual result:

  • Position and Size: Use absolute coordinates (points) or percentages relative to the page size.
  • Colors: The SDK accepts java.awt.Color objects. For transparency, use new Color(r, g, b, alpha).
  • Line Width: Measured in points; a value of 1 equals 1/72 inch.
  • Opacity and Blend Mode: Adjust with setOpacity(float) and setBlendMode(BlendMode).
  • Rotation and Skew: Apply transformations via setRotation(double) or setSkew(double, double) for advanced layouts.

Example of setting advanced properties:

ellipse.setOpacity(0.7f);
ellipse.setBlendMode(BlendMode.MULTIPLY);
ellipse.setRotation(45);

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 graphics container before adding them to the page.
  • Reuse Objects: If you need identical shapes on multiple pages, clone an existing shape instead of creating new instances.
  • Avoid Over‑Scaling: Define shapes at the final display size to prevent costly raster conversions.
  • Dispose Resources: Close the PdfDocument promptly to free native resources.

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

How can I add a custom font to shapes in a PDF?
You can embed a TrueType font using pdf.getFonts().addFont("MyFont.ttf") and then assign it to a shape via setFont(myFont). The SDK ensures the font is embedded in the final PDF.

Is it possible to insert shapes into an existing PDF without losing existing content?
Yes. Opening the PDF with PdfDocument.open() preserves all existing pages and objects. Adding shapes to a page’s graphics collection only augments the page; it does not overwrite existing content.

What file formats can I export to after adding shapes?
Conholdate.Total for Java supports saving to PDF, PDF/A, PDF/X, and also to image formats such as PNG, JPEG, and BMP. Use pdf.save("output.pdf") or pdf.save("output.png", ImageSaveOptions.Png) as needed.