Work with vector graphics in C++

Aspose.PDF for C++ provides the GraphicsAbsorber class for inspecting and modifying vector graphics in an existing PDF. Visit a page to obtain its graphic elements, then inspect their properties, change their positions, remove them, or copy them to another page.

The examples below operate on the first page, so the input PDF must contain at least one page. Each function accepts an input PDF path; examples that modify the document also accept an output PDF path. Configure the Aspose.PDF for C++ SDK before using the snippets.

Inspect vector graphics

GraphicsAbsorber reads graphic elements from the selected page. This example prints each element’s source page number, position, and operator count. It does not save a modified PDF or export graphics to a separate file.

  1. Open the input PDF with Document.
  2. Create a GraphicsAbsorber and call Visit() on the first page.
  3. Iterate through GraphicsAbsorber::get_Elements().
  4. Read the GraphicElement source page, position, and operators and print the values.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Vector/GraphicsAbsorber.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElement.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElementCollection.h>
#include <iostream>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Vector;

void UsingGraphicsAbsorber(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto graphicsAbsorber = MakeObject<GraphicsAbsorber>();
    auto page = document->get_Pages()->idx_get(1);
    graphicsAbsorber->Visit(page);
    for (auto element : graphicsAbsorber->get_Elements())
    {
        std::cout << "Page Number: " << element->get_SourcePage()->get_Number() << std::endl;
        std::cout << "Position: (" << element->get_Position()->get_X() << ", "
                  << element->get_Position()->get_Y() << ")" << std::endl;
        std::cout << "Number of Operators: " << element->get_Operators()->get_Count() << std::endl;
    }
}

Move vector graphics

Move every extracted graphic element on the first page by adding 150 to its X position and subtracting 10 from its Y position. The example batches changes through the absorber before saving.

  1. Open the input PDF with Document.
  2. Create a GraphicsAbsorber and call Visit() on the first page.
  3. Call GraphicsAbsorber::SuppressUpdate() before changing the elements.
  4. Read each GraphicElement position.
  5. Set its new position with set_Position() and a Point containing the shifted coordinates.
  6. Call GraphicsAbsorber::ResumeUpdate() to apply the pending updates.
  7. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Vector/GraphicsAbsorber.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElement.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElementCollection.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Vector;

void MoveGraphics(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto graphicsAbsorber = MakeObject<GraphicsAbsorber>();
    auto page = document->get_Pages()->idx_get(1);
    graphicsAbsorber->Visit(page);
    graphicsAbsorber->SuppressUpdate();
    for (auto element : graphicsAbsorber->get_Elements())
    {
        auto position = element->get_Position();
        element->set_Position(MakeObject<Point>(position->get_X() + 150, position->get_Y() - 10));
    }
    graphicsAbsorber->ResumeUpdate();
    document->Save(outputFile);
}

Remove vector graphics

Remove individual elements

Define the rectangle (70, 248, 170, 252) and remove elements whose Position passes Rectangle::Contains(position, false). This is a position-based filter; it does not test whether the complete graphic lies inside or intersects the rectangle.

  1. Open the input PDF with Document.
  2. Create a GraphicsAbsorber and call Visit() on the first page.
  3. Create the target Rectangle.
  4. Call GraphicsAbsorber::SuppressUpdate() before changing the elements.
  5. Check each element position with Rectangle::Contains().
  6. Call GraphicElement::Remove() on matching elements.
  7. Call GraphicsAbsorber::ResumeUpdate() to apply the pending updates.
  8. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Vector/GraphicsAbsorber.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElement.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElementCollection.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Vector;

void RemoveGraphicsMethod1(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto graphicsAbsorber = MakeObject<GraphicsAbsorber>();
    auto page = document->get_Pages()->idx_get(1);
    auto rectangle = MakeObject<Rectangle>(70, 248, 170, 252, true);
    graphicsAbsorber->Visit(page);
    graphicsAbsorber->SuppressUpdate();
    for (auto element : graphicsAbsorber->get_Elements())
    {
        if (rectangle->Contains(element->get_Position(), false))
        {
            element->Remove();
        }
    }
    graphicsAbsorber->ResumeUpdate();
    document->Save(outputFile);
}

Delete a collection of elements

Use the same position filter to collect matching elements first, then pass that collection to Page::DeleteGraphics(). This version batches updates through the page Contents collection.

  1. Open the input PDF with Document.
  2. Create a GraphicsAbsorber and call Visit() on the first page.
  3. Create a Rectangle and an empty GraphicElementCollection.
  4. Use Rectangle::Contains() to test each element position and add matching elements to the collection.
  5. Call OperatorCollection::SuppressUpdate() on the page Contents collection.
  6. Remove the collected elements with Page::DeleteGraphics().
  7. Call OperatorCollection::ResumeUpdate() on the page Contents collection.
  8. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/OperatorCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Vector/GraphicsAbsorber.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElement.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElementCollection.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Vector;

void RemoveGraphicsMethod2(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto graphicsAbsorber = MakeObject<GraphicsAbsorber>();
    auto page = document->get_Pages()->idx_get(1);
    auto rectangle = MakeObject<Rectangle>(70, 248, 170, 252, true);
    graphicsAbsorber->Visit(page);
    auto removedElements = MakeObject<GraphicElementCollection>();
    for (auto element : graphicsAbsorber->get_Elements())
    {
        if (rectangle->Contains(element->get_Position(), false))
        {
            removedElements->Add(element);
        }
    }
    page->get_Contents()->SuppressUpdate();
    page->DeleteGraphics(removedElements);
    page->get_Contents()->ResumeUpdate();
    document->Save(outputFile);
}

Copy vector graphics to another page

Add elements individually

Append a new page to the same document and copy each graphic element from the first page with AddOnPage(). The new page is not necessarily page 2 when the input already contains multiple pages. The example copies the extracted graphics rather than the entire source page.

  1. Open the input PDF with Document.
  2. Get the first page and append a destination page with PageCollection::Add().
  3. Create a GraphicsAbsorber and call Visit() on the first page.
  4. Call OperatorCollection::SuppressUpdate() on the destination page Contents collection.
  5. Iterate through the extracted elements and call GraphicElement::AddOnPage() for the destination page.
  6. Call OperatorCollection::ResumeUpdate() on the destination page Contents collection.
  7. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/OperatorCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Vector/GraphicsAbsorber.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElement.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElementCollection.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Vector;

void AddToAnotherPageMethod1(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto graphicsAbsorber = MakeObject<GraphicsAbsorber>();
    auto page1 = document->get_Pages()->idx_get(1);
    auto page2 = document->get_Pages()->Add();
    graphicsAbsorber->Visit(page1);
    page2->get_Contents()->SuppressUpdate();
    for (auto element : graphicsAbsorber->get_Elements())
    {
        element->AddOnPage(page2);
    }
    page2->get_Contents()->ResumeUpdate();
    document->Save(outputFile);
}

Add the element collection

Append a new page and pass the extracted graphic element collection to AddGraphics(). This provides a collection-based alternative to calling AddOnPage() for each element.

  1. Open the input PDF with Document.
  2. Append a destination page with PageCollection::Add().
  3. Create a GraphicsAbsorber and call Visit() on the first page.
  4. Call OperatorCollection::SuppressUpdate() on the destination page Contents collection.
  5. Copy the extracted collection with Page::AddGraphics().
  6. Call OperatorCollection::ResumeUpdate() on the destination page Contents collection.
  7. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/OperatorCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Vector/GraphicsAbsorber.h>
#include <Aspose.PDF.Cpp/Vector/Model/GraphicElementCollection.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Vector;

void AddToAnotherPageMethod2(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto graphicsAbsorber = MakeObject<GraphicsAbsorber>();
    auto page1 = document->get_Pages()->idx_get(1);
    auto page2 = document->get_Pages()->Add();
    graphicsAbsorber->Visit(page1);
    page2->get_Contents()->SuppressUpdate();
    page2->AddGraphics(graphicsAbsorber->get_Elements());
    page2->get_Contents()->ResumeUpdate();
    document->Save(outputFile);
}