Drawing Shapes

This guide shows how to draw geometric shapes and manage graphical page decorations in Aspose.PDF FOSS for Java. You will learn to draw circles using CircleAnnotation and to enumerate or remove Artifact objects such as watermarks and headers.

Shape Drawing

Aspose.PDF FOSS for Java exposes geometric shapes primarily through the annotation layer. CircleAnnotation draws an ellipse at a given Rectangle on a page. Set the fill color with setColor() and add it to the page annotation collection.

try (Document doc = new Document()) {
    PageCollection pages = doc.getPages();
    Page page = pages.add();
    CircleAnnotation circle = new CircleAnnotation(page,
        new Rectangle(100, 100, 200, 200));
    circle.setColor(Color.fromRgb(0, 0, 1));
    page.getAnnotations().add(circle);
    doc.save("shapes.pdf");
}

The Rectangle constructor takes (x1, y1, x2, y2) in PDF coordinate units (points, measured from the bottom-left corner of the page).

Artifacts

The Artifact class models page decorations: headers, footers, watermarks, and background images. ArtifactCollection, accessible via page resources, allows enumeration and removal of existing artifact objects.

try (Document doc = new Document("input.pdf")) {
    PageCollection pages = doc.getPages();
    Page page = pages.get(1);
    // Enumerate artifacts (watermarks, headers, footers)
    for (Artifact artifact : page.getArtifacts()) {
        System.out.println(artifact.getArtifactType());
    }
}

Use ArtifactCollection.delete(artifact) to remove specific artifacts without modifying the page content stream directly.

Removing Watermarks

To strip watermark artifacts from a page, iterate the artifact collection, identify watermarks by type, and remove them before saving:

try (Document doc = new Document("watermarked.pdf")) {
    PageCollection pages = doc.getPages();
    Page page = pages.get(1);
    for (Artifact artifact : page.getArtifacts()) {
        if ("Watermark".equals(artifact.getArtifactType())) {
            page.getArtifacts().delete(artifact);
            break;
        }
    }
    doc.save("clean.pdf");
}

See Also

 English