Detect shape within a nodetype.run

I am trying to find text color and background color. Using NodeType.Run, it is fairly easy but I run into issues when the text is in a shape. The aim is to match up the font color and the background color, but since within the run, I am unable to get the shape, my test fails.

I understand that NodeType.Shape should be used, but is there a way I can use it within the Runs I am looping?

Thanks

Hi Shams,

Thanks for your inquiry. Sure, you can find out if a Run is contained inside a Shape node by using the following code:

Shape containingShape = (Shape)run.GetAncestor(NodeType.Shape);

I hope, this helps.

Best regards,

Hi Awais.

Unfortunately, that did not work out. I have attached a sample file that has a shape I am having issues with.

My code below always gets null in the shape var.

var runCollection = doc.GetChildNodes(NodeType.Run, true);
foreach(Run run in runCollection)
{
    var parent = run.ParentParagraph;
    var style = parent.ParagraphFormat.Style;
    var shape = (Shape) run.GetAncestor(NodeType.Shape);
    Console.WriteLine(run.Text);
    run.Remove();
}

Can you please help?

Thanks

Hi,

Thanks for your inquiry. In your document, only the last run is contained inside the shape. Please try run the following code to capture those runs which are contained inside shapes.

var runCollection = doc.GetChildNodes(NodeType.Run, true);
foreach(Run run in runCollection)
{
    var parent = run.ParentParagraph;
    var style = parent.ParagraphFormat.Style;
    var shape = (Shape) run.GetAncestor(NodeType.Shape);
    if (shape != null)
    {
        Console.WriteLine("you can write your logic here");
    }
    Console.WriteLine(run.Text);
    run.Remove();
}

I hope, this helps.

Best regards,

Hi Awais,

That still fails when I try to detect a shape style. I have a word doc that has “shape style” object with some text written within it and that is not detected by shape or any other type.

I am more than happy to email you the doc if you send in ur email address.

thanks
shams

Hi Shams,

Please note that it is safe to attach files in the forum. If you attach your document here, only you and Aspose staff members can download it. Also, you can send the file to my e-mail as described here:
https://forum.aspose.com/t/aspose-words-faq/2711
Best regards,

Hi Awais,

I have attached the file.

Thanks
Shams

Hi Shams,

Thanks for your inquiry. The text formatted with white colour is actually not contained inside any shape node. The blue rectangle shape is a floating object and because this shape has “Behind Text” text wrapping that is why the white coloured text is appearing above that shape. Please refer to the following article to learn the Aspose.Words’ DOM hierarchy:

Best regards,

Hi Awais,

Thanks for that. In my case I need to find invisible text i.e text having same font color as the background color. Regarding this example, how would I then detect the background color since it always comes back white?

thanks

Hi Shams,

Thanks for your inquiry. Well, in case your target text is contained inside a table cell, you could compare the value of “Run.Font.Color” property with the value of it’s parent Cell’s “CellFormat.Shading.BackgroundPatternColor” property. Similarly, if the text is contained (by contained I mean this run node is a child of current node) inside a shape, you could compare the value of “Run.Font.Color” property with the value of it’s parent Shape’s “FillColor” property and so on.

However, in your case, the paragraphs having the white coloured content are contained directly inside the Body, you could compare font colour with either Document.PageColor or Document.BackgroundShape.FillColor. The point is, once you find the position of your particular run node, you can move up the DOM hierarchy and find out a suitable node from which you want to compare the colours with.

Best regards,