Get absolute positions and bounding boxes of SVGElements

Hi, i’m new to SVG and have trouble parsing svg files to extract all the text with their absolute positions and bounding boxes. This is the code I wrote, From my understanding, GetScreenCTM() should give me a transformation matrix which when applied to the initial x and y pos from GetBBox() should give me the correct answer but I am not sure if I understood it clearly.

            foreach ( SVGTextElement textElement in textElements )
            {
                var nodeText = textElement.TextContent;
                if ( !string.IsNullOrEmpty(nodeText) )
                {
                    var bbox = textElement.GetBBox();
                    var ctm = textElement.GetScreenCTM();

                    var x1 = bbox.X;
                    var y1 = bbox.Y;
                    var width = bbox.Width;
                    var height = bbox.Height;

                    var x2 = x1 + width;
                    var y2 = y1 + height;

                    var p1 = new Vector2D((int) ( ( x1 * ctm.A ) + ctm.E ), (int) ( ( y1 * ctm.D ) + ctm.F ));
                    var p2 = new Vector2D((int) ( ( x2 * ctm.A ) + ctm.E ), (int) ( ( y2 * ctm.D ) + ctm.F ));

                    var textBoundingBox = new Bounds2D(p1, p2); // rect with x,y, width and height
                }
            }

@chiraagbangera

Could you kindly also share a sample SVG file with us as well. We will further proceed to assist you accordingly.

Please find it attached here . Sample_Tech_Pack_Advanced.pdf (637.4 KB)

@chiraagbangera

Sorry for the delayed response.

The attached document is a PDF document. Would you please share .zip of a SVG file in question?

Hi @asad.ali Sorry for the delayed response. I was able to solve this. Thank you though.

1 Like