Replace a color to another

Hello,

I’m using .NET for Words trial version and I’m wondering if there is a way to replace a color by another in the whole document.
I mean, not just in text but in tables, in borders, … in everything. Every time the first color occurs, it should be replaced by the second one.
Is there a way to do that other than browsing all the document structure recursively ?

Thanks by advance.
Regards.

Hi Edwige,

Thanks for your inquiry. Please attach the following resources here for testing:

  • Your input Word document.
  • Your expected Word document which shows the desired coloring. You can create this document using Microsoft Word.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you code to achieve the same expected output using Aspose.Words.

Best regards,

Hello,

I’ve attached you a document.
For example, we wanted to change all the white in red and all the light blue in pink (just an example).
In fact, we just want to replace in the whole document a color to an other one.

Thanks by advance.
Regards.

Hi Edwige,

Thanks for your inquiry. Please try executing the following code for example:

Document doc = new Document(MyDir + @"requete.doc");
foreach(Run run in doc.GetChildNodes(NodeType.Run, true))
{
    if (run.Font.Color.Name == "ffffffff")
        run.Font.Color = Color.Red;
}
doc.Save(MyDir + @"out.doc");

Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting 1) to Run nodes by using Styles e.g. a Glyph Style, 2) to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles) and 3) you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting. Therefore, you can change the color either by using Font.Color property or by using Style.Font.Color property. I hope, this helps.

Best regards,

Hello,

Thanks for you answer, it works fine for the font color but my purpose is to replace everything in one color into an other one, including in shapes, tables/cells, lines, borders, background (not images, of course), text in shapes, everything which is in the document and where the color occurs, we wanted to replace it with an other one.

Is it possible to do this in a more efficient way than this code sample ?

public virtual void ReplaceColor(Aspose.Words.Document document, IDictionary <Color, Color> colorMapping)
{
    if (colorMapping != null)
    {
        foreach(Color colorToReplace in colorMapping.Keys)
        {
            foreach(Run run in document.GetChildNodes(NodeType.Run, true))
            {
                if (run.Font.Color.ToArgb() == colorToReplace.ToArgb())
                {
                    run.Font.Color = colorMapping[colorToReplace];
                }
            }
            foreach(Cell cell in document.GetChildNodes(NodeType.Cell, true))
            {
                if (cell.CellFormat.Shading.BackgroundPatternColor.ToArgb() == colorToReplace.ToArgb())
                {
                    cell.CellFormat.Shading.BackgroundPatternColor = colorMapping[colorToReplace];
                }
            }
            foreach(Shape shape in document.GetChildNodes(NodeType.Shape, true))
            {
                if (shape.FillColor.ToArgb() == colorToReplace.ToArgb())
                {
                    shape.FillColor = colorMapping[colorToReplace];
                }
            }
        }
    }
}

Thanks by advance,
Regards.

Hi there,

Thank you for your request. Our support team are looking into this request and will answer as soon as possible, please hold tight.
Thanks,

Hello,

Thanks.
I will check this we I’ll come back (in september, I’m afraid).

Best regards.

Hi Edwige,

Thanks for the additional information. I think, you can build on the following code to meet this requirement:
https://reference.aspose.com/words/net/aspose.words/documentvisitor/

Document doc = new Document(MyDir + @"requete.doc");
ColorChanger cc = new ColorChanger();
doc.Accept(cc);
doc.Save(MyDir + @"out.doc");

public class ColorChanger: DocumentVisitor
{
    private readonly Color mColor;
    public ColorChanger()
    {
        mColor = Color.Blue;
    }
    public override VisitorAction VisitRun(Run run)
    {
        run.Font.Color = mColor;
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitTableEnd(Table table)
    {
        table.SetBorder(BorderType.Left, LineStyle.Single, 1, mColor, true);
        table.SetBorder(BorderType.Top, LineStyle.Single, 1, mColor, true);
        table.SetBorder(BorderType.Right, LineStyle.Single, 1, mColor, true);
        table.SetBorder(BorderType.Bottom, LineStyle.Single, 1, mColor, true);
        return VisitorAction.Continue;
    }
    public override VisitorAction VisitShapeEnd(Shape shape)
    {
        shape.Font.Color = mColor;
        shape.FillColor = mColor;
        return VisitorAction.Continue;
    }
}

You can override more DocumentVisitor methods as per your requirement. I hope, this helps.

Best regards,

Hello,

Thanks, it helps

Just one last question, how can we know what is the color of a border for a table ?

Thanks.
Regards.

Hi Edwige,

Thanks for your inquiry. You can determine color of any cell in Table by using properties like Cell.CellFormat.Borders.Bottom.Color, Cell.CellFormat.Borders.Left.Color etc. I hope, this helps.

Best regards,

Hello,

Yes, it helps.

Regards.