We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Find Text by Color

Is it possible to find text by color? If so, could you point me to a code example?


Thanks!
Hi Charles,

Thanks for your inquiry. All text of the document is stored in runs of text. Please use Run.Font.Color property to get the color of text. If you face any issue, please share your input document and expected output here for our reference. We will then provide you more information about your query.


Document doc = new Document(MyDir + "in.docx");

foreach (Run run in doc.GetChildNodes(NodeType.Run, true))

{

if (run.Font.Color == Color.Red)

Console.WriteLine(run.Text);

}

Thanks!


I had to use Color.Name, but otherwise it worked great.
For Each r as Run in doc.GetChildNodes(NodeType.Run, true)
if (r.Font.Color.Name = “ffff0000”) Then
debug.WriteLine(r.Text)
End If
Next
On another note, though, is there a way to code the above using LINQ syntax? Or do I have to use a loop?
Thanks.
Hi Charles,

Thanks for your inquiry. Please try following code example. Hope this helps you.

Dim doc As New Document(MyDir + "in.docx")

Dim nodes As IEnumerable(Of Run) = doc.GetChildNodes(NodeType.Run, True).Cast(Of Run)().ToList().Where(Function(run) run.Font.Name = "ffff0000")

For Each run As Run In nodes

Console.WriteLine(run.Text)

Next

1 Like