Work with Vector Graphics in Java
Aspose.PDF for Java exposes vector content through GraphicsAbsorber and GraphicElement objects. This lets you inspect low-level vector elements on a page and then update, remove, copy, or export them.
Inspect vector graphics on a page
Use this example when you need to enumerate vector elements and inspect their page, position, and operator count.
- Open the source PDF Document.
- Create a GraphicsAbsorber and visit the target page.
- Iterate through the absorbed GraphicElement objects and output their properties.
public static void usingGraphicsAbsorber(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
try {
Page page = document.getPages().get_Item(1);
graphicsAbsorber.visit(page);
for (GraphicElement element : graphicsAbsorber.getElements()) {
System.out.println("Page Number: " + element.getSourcePage().getNumber());
System.out.println("Position: (" + element.getPosition().getX() + ", "
+ element.getPosition().getY() + ")");
System.out.println("Number of Operators: " + element.getOperators().size());
}
} finally {
graphicsAbsorber.dispose();
}
}
}
Move vector graphics on the page
Use this example when all detected vector elements should be shifted to a new position.
- Open the source PDF Document.
- Visit the target page with GraphicsAbsorber and temporarily suppress updates.
- Change the position of each absorbed element, resume updates, and save the document.
public static void moveGraphics(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
try {
Page page = document.getPages().get_Item(1);
graphicsAbsorber.visit(page);
graphicsAbsorber.suppressUpdate();
for (GraphicElement element : graphicsAbsorber.getElements()) {
Point position = element.getPosition();
element.setPosition(new Point(position.getX() + 150, position.getY() - 10));
}
graphicsAbsorber.resumeUpdate();
} finally {
graphicsAbsorber.dispose();
}
document.save(outputFile.toString());
}
System.out.println("Vector graphics moved in " + outputFile);
}
Remove vector graphics by position with element removal
Use this example when vector elements inside a specific rectangle should be deleted one by one.
- Open the source PDF Document.
- Visit the page with GraphicsAbsorber and define the target Rectangle.
- Remove the matching elements, resume updates, and save the document.
public static void removeGraphicsMethod1(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
try {
Page page = document.getPages().get_Item(1);
Rectangle rectangle = new Rectangle(70, 248, 170, 252, true);
graphicsAbsorber.visit(page);
graphicsAbsorber.suppressUpdate();
for (GraphicElement element : graphicsAbsorber.getElements()) {
if (rectangle.contains(element.getPosition(), false)) {
element.remove();
}
}
graphicsAbsorber.resumeUpdate();
} finally {
graphicsAbsorber.dispose();
}
document.save(outputFile.toString());
}
System.out.println("Vector graphics removed with method 1 in " + outputFile);
}
Remove vector graphics by deleting a collection
Use this example when matching vector elements should be collected first and then removed in one page operation.
- Open the source PDF Document.
- Visit the page with GraphicsAbsorber and collect the matching elements.
- Delete the collected graphics from the page contents and save the updated document.
public static void removeGraphicsMethod2(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
try {
Page page = document.getPages().get_Item(1);
Rectangle rectangle = new Rectangle(70, 248, 170, 252, true);
graphicsAbsorber.visit(page);
GraphicElementCollection removedElements = new GraphicElementCollection();
for (GraphicElement element : graphicsAbsorber.getElements()) {
if (rectangle.contains(element.getPosition(), false)) {
removedElements.add(element);
}
}
page.getContents().suppressUpdate();
page.deleteGraphics(removedElements);
page.getContents().resumeUpdate();
} finally {
graphicsAbsorber.dispose();
}
document.save(outputFile.toString());
}
System.out.println("Vector graphics removed with method 2 in " + outputFile);
}
Copy vector graphics to another page element by element
Use this example when each absorbed vector element should be added individually to a new page.
- Open the source PDF Document and add a destination page.
- Visit the source page with GraphicsAbsorber.
- Add each GraphicElement to the destination page and save the document.
public static void addToAnotherPageMethod1(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
try {
Page page1 = document.getPages().get_Item(1);
Page page2 = document.getPages().add();
graphicsAbsorber.visit(page1);
page2.getContents().suppressUpdate();
for (GraphicElement element : graphicsAbsorber.getElements()) {
element.addOnPage(page2);
}
page2.getContents().resumeUpdate();
} finally {
graphicsAbsorber.dispose();
}
document.save(outputFile.toString());
}
System.out.println("Vector graphics copied with method 1 in " + outputFile);
}
Copy vector graphics to another page as a collection
Use this example when the entire absorbed vector graphic collection should be copied to a new page in one call.
- Open the source PDF Document and add a destination page.
- Visit the source page with GraphicsAbsorber.
- Add the absorbed graphics collection to the destination page and save the document.
public static void addToAnotherPageMethod2(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
try {
Page page1 = document.getPages().get_Item(1);
Page page2 = document.getPages().add();
graphicsAbsorber.visit(page1);
page2.getContents().suppressUpdate();
page2.addGraphics(graphicsAbsorber.getElements());
page2.getContents().resumeUpdate();
} finally {
graphicsAbsorber.dispose();
}
document.save(outputFile.toString());
}
System.out.println("Vector graphics copied with method 2 in " + outputFile);
}