Work with Vector Graphics in Python
Aspose.PDF for Python via .NET provides the GraphicsAbsorber class for accessing and manipulating vector graphics already present in a PDF page. Call its visit method on any page to extract paths, shapes, and other graphical operators, then move, remove, or copy those elements as needed.
Use this page when you need to inspect or modify vector drawing elements embedded in an existing PDF, rather than drawing new shapes from scratch.
Extracting Graphics
Extraction is the starting point for all vector graphics tasks. GraphicsAbsorber reads the content stream of a page and exposes each graphic element with its page reference, position, and raw operators.
- Open the PDF document.
- Create a GraphicsAbsorber instance.
- Call
visiton the target page to populateelements. - Iterate over
elementsto inspect position and operator counts.
import aspose.pdf as ap
import sys
from os import path
def using_graphics_absorber(infile: str):
with ap.Document(infile) as document:
with ap.vector.GraphicsAbsorber() as graphics_absorber:
page = document.pages[1]
graphics_absorber.visit(page)
for element in graphics_absorber.elements:
print(f"Page Number: {element.source_page.number}")
print(f"Position: ({element.position.x}, {element.position.y})")
print(f"Number of Operators: {element.operators.length}")
GraphicsAbsorber is part of the aspose.pdf.vector namespace and is specifically designed to interact with vector graphics in PDF content streams.
Moving Graphics
After extraction, set a new position on each element to relocate it on the same page. Wrap the updates in suppress_update / resume_update calls to batch content-stream writes into a single operation and avoid redundant repaints.
- Open the PDF document.
- Create a
GraphicsAbsorberand callvisiton the target page. - Call
suppress_updateto pause content-stream writes. - Update the
positionof each element. - Call
resume_updateto commit all changes at once. - Save the modified document.
import aspose.pdf as ap
import sys
from os import path
def move_graphics(infile: str, outfile: str):
with ap.Document(infile) as document:
with ap.vector.GraphicsAbsorber() as graphics_absorber:
page = document.pages[1]
graphics_absorber.visit(page)
graphics_absorber.suppress_update()
for element in graphics_absorber.elements:
position = element.position
element.position = ap.Point(position.x + 150, position.y - 10)
graphics_absorber.resume_update()
document.save(outfile)
Removing Graphics
To delete specific vector elements from a page, filter by position or bounding rectangle and then remove them. Aspose.PDF for Python provides two approaches depending on whether you want to remove elements inline or collect them first.
Method 1: Remove Inline Using Rectangle Boundary
This approach checks each element’s position against a rectangle and calls element.remove() directly inside the loop. Use it when you want concise code and do not need to inspect the removed set afterward.
- Open the PDF document.
- Create a
GraphicsAbsorberand callvisiton the target page. - Define the target Rectangle.
- Call
suppress_updateto pause content-stream writes. - Iterate
elements, callingremove()on each element whose position falls inside the rectangle. - Call
resume_updateto commit the deletions. - Save the modified document.
import aspose.pdf as ap
import sys
from os import path
def remove_graphics_method_1(infile: str, outfile: str):
with ap.Document(infile) as document:
with ap.vector.GraphicsAbsorber() as graphics_absorber:
page = document.pages[1]
graphics_absorber.visit(page)
rectangle = ap.Rectangle(70, 248, 170, 252, True)
graphics_absorber.suppress_update()
for element in graphics_absorber.elements:
if rectangle.contains(element.position, False):
element.remove()
graphics_absorber.resume_update()
document.save(outfile)
Method 2: Collect Elements First, Then Delete
This approach gathers matching elements into a GraphicElementCollection and passes the collection to page.delete_graphics. Use it when you need to review or log what will be removed before committing the deletion.
- Open the PDF document.
- Create a
GraphicsAbsorberand callvisiton the target page. - Define the target rectangle.
- Iterate
elementsand add matching elements to aGraphicElementCollection. - Call
page.contents.suppress_updateto pause content-stream writes. - Call
page.delete_graphicswith the collection. - Call
page.contents.resume_updateto commit the deletions. - Save the modified document.
import aspose.pdf as ap
import sys
from os import path
def remove_graphics_method_2(infile: str, outfile: str):
with ap.Document(infile) as document:
with ap.vector.GraphicsAbsorber() as graphics_absorber:
page = document.pages[1]
rectangle = ap.Rectangle(70, 248, 170, 252, True)
graphics_absorber.visit(page)
removed_elements_collection = ap.vector.GraphicElementCollection()
for element in graphics_absorber.elements:
if rectangle.contains(element.position, False):
removed_elements_collection.add(element)
page.contents.suppress_update()
page.delete_graphics(removed_elements_collection)
page.contents.resume_update()
document.save(outfile)
Adding Graphics to Another Page
Vector elements extracted from one page can be placed on any other page in the same document. Two methods are available: adding elements one by one, or passing the entire collection in a single call.
Method 1: Add Elements Individually
Use this method when you need per-element control, such as filtering or transforming individual elements before placing them on the destination page.
- Open the PDF document.
- Create a
GraphicsAbsorberand callvisiton the source page. - Add a new destination page to the document.
- Call
page_2.contents.suppress_updateto pause content-stream writes. - Call
element.add_on_page(page_2)for each element. - Call
page_2.contents.resume_updateto commit all additions. - Save the modified document.
import aspose.pdf as ap
import sys
from os import path
def add_to_another_page_method_1(infile: str, outfile: str):
with ap.Document(infile) as document:
with ap.vector.GraphicsAbsorber() as graphics_absorber:
page_1 = document.pages[1]
page_2 = document.pages.add()
graphics_absorber.visit(page_1)
page_2.contents.suppress_update()
for element in graphics_absorber.elements:
element.add_on_page(page_2)
page_2.contents.resume_update()
document.save(outfile)
Method 2: Add the Entire Collection at Once
Use this method when you want to copy all extracted elements to a page in a single operation without iterating manually.
- Open the PDF document.
- Create a
GraphicsAbsorberand callvisiton the source page. - Add a new destination page to the document.
- Call
page_2.contents.suppress_updateto pause content-stream writes. - Call
page_2.add_graphicswith the fullelementscollection. - Call
page_2.contents.resume_updateto commit all additions. - Save the modified document.
import aspose.pdf as ap
import sys
from os import path
def add_to_another_page_method_2(infile: str, outfile: str):
with ap.Document(infile) as document:
with ap.vector.GraphicsAbsorber() as graphics_absorber:
page_1 = document.pages[1]
page_2 = document.pages.add()
graphics_absorber.visit(page_1)
page_2.contents.suppress_update()
page_2.add_graphics(graphics_absorber.elements, None)
page_2.contents.resume_update()
document.save(outfile)