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 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.
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 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.
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 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.