Extracting Shape from PDF

How to extract the shape from the pdf ?

do we need to use operators to get the shape ?

@arunkumar.mp

Can you please share what kind of shape your PDF has? Are these images? If possible, can you please share your sample PDF with us and explain how you want to process the extracted shape/image further?

apologies for the late reply

Shape.pdf (104.1 KB)

I need to extract the shapes in this pdf

@arunkumar.mp

We added a new functionality for interacting with graphic elements. Elements can be absorbed by GraphicAbsorber class :

var source = new Document(input1);
var ga = new GraphicsAbsorber();
ga.Visit(source.Pages[1]);

You can position of each element (SuppressUpdate and ResumeUpdate are needed to avoid contents stream updates after each position change):

va.SuppressUpdate();
foreach (var el in va.Elements)
{
    var position = el.Position;
    el.Position = new Point(position.X - 150, position.Y - 100);
}
va.ResumeUpdate();

You can delete delete elements from the page’s content one by one:

va.SuppressUpdate();
var rectangle = new Rectangle(500, 0, 600, 200);
foreach (var el in va.Elements)
{
    if (!rectangle.Contains(element.Position))
    {
        el.Remove();
    }
}
va.ResumeUpdate();

or collect items into GraphicElementCollection and delete them at once (better performance):

var rectangle = new Rectangle(500, 0, 600, 200);
var elems = new GraphicElementCollection();
foreach (var element in ga.Elements)
{
    if (!rectangle.Contains(element.Position))
    {
         elems.Add(element);
    }
}
source.Pages[1].DeleteGraphics(elems);

And also you can add graphic elements to another page:

destinationDoc.Pages[1].Contents.SuppressUpdate();
foreach (var el in elems)
{
    el.AddOnPage(destinationDoc.Pages[1]);
}
destinationDoc.Pages[1].Contents.ResumeUpdate();

or by using:

destinationDoc.Pages[1].AddGraphics(ga.Elements);

If you don’t find any function to process the extracted graphics as per your desire, please let us know so that we can further proceed accordingly.

Will check this and get back to you for any clarification.Thank you

@arunkumar.mp

Sure, please take your time.

https://reference.aspose.com/pdf/java/com.aspose.pdf.engine.pagemodel/graphicelement/
Im not able to access this page

How i can get the shape(eg: square,circle, triangle) using the Position from GraphicsElement ?

@asad.ali

@arunkumar.mp

We need to check and investigate for this case. We have logged below ticket in our issue tracking system for this purpose.

Issue ID(s): PDFJAVA-44104

We will look into its details and let you know as soon as the ticket is resolved. Please be patient and spare us some time.

1 Like

With the GraphicsElement Im able to get the shape path.
But how to get the Color, Reflection, Shadow of the shape ?

@asad.ali

@arunkumar.mp

The ticket is currently under the phase of the investigation. We have recorded your concerns and will surely inform you as soon as it is resolved. Please spare us some time. We are sorry for the inconvenience.