简体   繁体   中英

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

Here is:

• Aspose.Words

• 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
        System.Drawing.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)
    }
}

Please use Run.Font.Shading.BackgroundPatternColor property to get the BackgroundColor of Run node.

Document doc = new Document(MyDir + "Sample.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
foreach (Paragraph par in table.LastRow.LastCell.Paragraphs)
{  
    foreach (Run run in par.Runs) //par - it's Paragraph in Cells
    {
        if (run.IsInsertRevision || run.IsDeleteRevision) //check revisions (in TrackChange)
        {
            Console.WriteLine(run.Font.Shading.BackgroundPatternColor);
        }
    }
}

I work with Aspose as Developer evangelist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM