How can I get infos about font (color etc) of the current node

Hi,

I’m trying to do the following:

-Open document
-Move to Mergefield XYZ
-Detect the current font color and apply black if current color is white.

I’m using a documentbuilder:

builder.MoveToMergeField(field.Name)
– Condition here to validate current color (if it’s white, then change it to black)
builder.CurrentParagraph.ParagraphBreakFont.Color = System.Drawing.Color.Black
– Condition end

The property builder.CurrentParagraph.ParagraphBreakFont.Color always return the same no matter if source document field font color is white, red or black.

Am I using the wrong property or is there a way to achieve this?

Regards,

JSR

Hi,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

No problem!

Here’s the input word document. There’s a mergefield named “InvertigateThisField” in the footer. That’s the field I would like to validate font and color with previously posted code (document builder).

Regards,

JSR

Hi,

Thanks for your inquiry. In your case, you can retrieve the color of your MERGEFIELD by using the following code snippet:

Document doc = new Document(@"C:\Temp\Table+des+matières.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Node[] fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true).ToArray();
foreach (FieldStart fieldStart in fieldStarts)
{
    if (fieldStart.FieldType == FieldType.FieldMergeField)
    {
        builder.MoveTo(fieldStart);
        string fieldCode = GetFieldCode(fieldStart).Trim();
        if (fieldCode.Contains("InvertigateThisField"))
        {
            Run run = fieldStart.NextSibling as Run;
            Color color = run.Font.Color;
        }
    }
}
private static string GetFieldCode(FieldStart fieldStart)
{
    StringBuilder builder = new StringBuilder();
    for (Node node = fieldStart; node != null && node.NodeType != NodeType.FieldSeparator &&
    node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))
    {
        if (node.NodeType == NodeType.Run)
            builder.Append(node.GetText());
    }
    return builder.ToString();
}

I hope, this helps.

Best regards,

Wow!

Thanks a lot, working fine!

Thanks for quick answer

JSR

Hi,

Thanks for your feedback. Please let us know any time you have any further queries. We’re always glad to help you.

Best regards,