How to get the BackgroundColor value for Runs in Aspose.Words?

Here:

• System.Drawing
If one of the main .docx document paragraph is modified (with TrackChange always enabled), you have to determine the background color (color code) of the modified run.
When a run is modified well, it is determined correctly (for example, when “client” was written instead of “contractor”, it is shown as modified run). How to get the background color code?
The document is formatted as a table. I take all the cells. In the cells, I take all the paragraphs and in the paragraphs, I take all the runs:

foreach(Run run in par.Runs) //par - it's Paragraph in Cells
{
    if(run.IsInsertRevision || run.IsDeleteRevision) //check revisions (in TrackChange)
    {
        Paragraph parpar = run.ParentParagraph; //taking parent paragraph

        Shading shading = builder.ParagraphFormat.Shading; //create a new shading for current paragraph
        Color clr = shading.BackgroundPatternColor; //trying to get a backgroung color

        string r = clr.R.ToString("X2");
        string g = clr.G.ToString("X2");
        string b = clr.B.ToString("X2");

        r = r.Length == 1 ? "0" + r : r;
        g = g.Length == 1 ? "0" + g : g;
        b = b.Length == 1 ? "0" + b : b;

        string code = "#" + r + g + b;

        Console.WriteLine(code); //it's #000000 instead #fff001 (real backgroung color in the document)
    }
}

Hi,

Thanks for your inquiry. Could you please attach your sample Word document you’re getting this problem with here for testing? We will investigate the issue on our end and provide you more information.

Best regards,

Hello, Awais

Here’s sample of my Word document with structure:
- Cells
- Paragraph
- Runs

I need to get the background color of the paragraph, not cell.
Could you please attach sample of the code for this Sample.docx to help me with the task

Hi,

Thanks for your inquiry. You can get the blue color applied to Run nodes by using the following code:

Document doc = new Document(MyDir + @"sample.docx");
foreach (Revision rev in doc.Revisions)
{
    if (rev.RevisionType == RevisionType.FormatChange)
    {
        if (rev.ParentNode != null && rev.ParentNode.NodeType == NodeType.Paragraph)
        {
            Paragraph para = (Paragraph)rev.ParentNode;
            foreach (Run run in para)
            {
                Shading shading = run.Font.Shading;
                Color clr = shading.BackgroundPatternColor;
                string r = clr.R.ToString("X2");
                string g = clr.G.ToString("X2");
                string b = clr.B.ToString("X2");
                r = r.Length == 1 ? "0" + r : r;
                g = g.Length == 1 ? "0" + g : g;
                b = b.Length == 1 ? "0" + b : b;
                string code = "#" + r + g + b;
                Console.WriteLine(run.Text + " _ " + code); //it's #000000 instead #fff001 (real backgroung color in the document)
            }
        }
    }
}

Hope, this helps.

Best regards,

Thanks a lot!
Didn’t see Font.Shading in the runs!
You just saved my project

Hi,

Thanks for your feedback. In case you have further inquiries or need any help in future, please let us know.

Best regards,

I’m sorry but I have other request linked with this.
I need to get background color for a run in the case if its in the colored cell OR in the case of colored RUNs background color.

I mean, I need to get colors for the words (in attachment) “CELL COLOR” AND “PARAGRAPH COLOR”.
How can I do it using similar to last code?

If I use
CellFormat.Shading.BackgroundPatternColor for the runs, it returns #000000 instead Yellow.

In other words I need to get Yellow Color (approximate, of course, in HEX) code for “CELL COLOR” and Sky Blue code for “PARAGRAPH COLOR”.

Thanks, you’re the most responsive developers I know.

Hi,

Thanks for your inquiry. Please check if the following code helps you in achieving what you’re looking for:

Document doc = new Document(MyDir + @"sample.docx");
// Check all Run nodes in Document
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
    // Check if the Run node has background color
    if (!run.Font.Shading.BackgroundPatternColor.Equals(Color.Empty))
    {
        Console.WriteLine("This Run has direct background color applied to it = " +
        run.Font.Shading.BackgroundPatternColor.ToString());
    }
    else
    {
        // Check if the Run is contained inside a Cell
        Cell cell = (Cell)run.GetAncestor(NodeType.Cell);
        if (cell != null)
        {
            // Check if the Cell containing the Run node has background color
            if (!cell.CellFormat.Shading.BackgroundPatternColor.Equals(Color.Empty))
            {
                Console.WriteLine("This Cell containing the Run has background color applied to it = " +
                cell.CellFormat.Shading.BackgroundPatternColor.ToString());
            }
            else
            {
            }
        }
    }
}

Hope, this helps.

Best regards,