Get vector graphic objects information

Hi Team,

I want to get some information (Position, Rectangle and Color) about the vector graphic objects in a PDF.

How can I extract these objects and get these information?

My main goal is the following:

  1. Check if a Textfragment (Text) is behind or in front of a vector graphic object.
  2. If the text is in front of the graphic then I want to check if the text color is the same as the vector graphic object color.

So for example I want to detect if a black text is in front of a rectangle filled with black color. So basically the text is invisible.

How can I check this use case?

Thanks for your help!

@erdeiga

To extract information about vector graphic objects in a PDF, such as their position, rectangle, and color, you can utilize the GraphicsAbsorber class provided by Aspose.PDF. This class allows you to access and manipulate vector graphics within PDF documents effectively.

Here’s a step-by-step approach to achieve your goals:

  1. Extract Vector Graphics: Use the GraphicsAbsorber to extract vector graphics from a specific page of the PDF. This will allow you to retrieve properties such as position and rectangle.

  2. Check Text Position: After extracting the vector graphics, you can analyze the position of the text fragments on the same page. You will need to compare the coordinates of the text fragments with those of the vector graphics to determine if the text is in front of or behind the graphic objects.

  3. Compare Colors: If you find that the text is in front of a vector graphic, you can then check the color of the text fragment against the color of the vector graphic. This will help you identify if the text is effectively invisible (e.g., black text on a black rectangle).

Here is a sample code snippet to get you started:

using Aspose.Pdf;
using Aspose.Pdf.Devices;
using Aspose.Pdf.Text;

// Load the PDF document
Document pdfDocument = new Document("input.pdf");

// Create a GraphicsAbsorber to extract vector graphics
GraphicsAbsorber absorber = new GraphicsAbsorber();
pdfDocument.Pages[1].Accept(absorber);

// Iterate through the extracted graphics
foreach (var graphic in absorber.Graphics)
{
    // Get the rectangle and position of the graphic
    var rect = graphic.Rectangle;
    var position = graphic.Position;

    // You can also access the color of the graphic here
    // (Assuming you have a method to get the color)
    var graphicColor = GetGraphicColor(graphic);
    
    // Now, check for text fragments on the same page
    foreach (TextFragment textFragment in pdfDocument.Pages[1].Paragraphs)
    {
        // Check if the text is in front of the graphic
        if (IsTextInFront(textFragment, rect))
        {
            // Compare colors
            if (textFragment.TextState.ForegroundColor.Equals(graphicColor))
            {
                // The text is invisible
            }
        }
    }
}

// Method to get the color of the graphic (implementation needed)
Color GetGraphicColor(Graphic graphic)
{
    // Implementation to extract color from the graphic
}

// Method to check if text is in front of the graphic (implementation needed)
bool IsTextInFront(TextFragment text, Rectangle graphicRect)
{
    // Implementation to compare positions
}

This code provides a basic framework for extracting vector graphics and checking the visibility of text fragments in relation to those graphics. You will need to implement the methods for color extraction and position comparison based on your specific requirements.

For more detailed information on working with vector graphics and the GraphicsAbsorber class, you can refer to the official documentation here and here.

Sources:
[1]: Extract Vector Data from a PDF file using C# - Aspose Documentation
[2]: Working with Vector Graphics|Aspose.PDF for .NET

Position has X and Y coordinates is it possible to get Z coordinates somehow so I can check if a text is in front of a GraphicElement object?

Could you help me how can I get the color of the GraphicElement? I have to check the Operators?

@erdeiga

If possible, can you please provide your sample PDF document for our reference along with the sample code snippet that you have tried so far. We will investigate the feasibility of the features that you require and share our feedback with you accordingly.