【问题标题】:How to get the BackgroundColor value for Run in Aspose.Words?如何在 Aspose.Words 中获取 Run 的 BackgroundColor 值?
【发布时间】:2017-02-03 13:12:41
【问题描述】:

这里是:

• Aspose.Words

• 系统绘图

如果修改了主要 .docx 文档段落之一(始终启用 TrackChange),您必须确定修改后的运行的背景颜色(颜色代码)。

当一个运行被修改好时,它被正确确定(例如,当写“client”而不是“contractor”时,它显示为修改后的运行)。如何获取背景颜色代码?

文档格式为表格。我把所有的细胞。在单元格中,我取所有段落,在段落中,我取所有运行:

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)
    }
}

【问题讨论】:

  • .ToString("X2") 总是返回前导零,因此下面的额外代码是多余的。
  • 你检查过clr.R、clr.G和clr.B的实际值吗?
  • 不,在我的情况下,它返回没有前导零的 HEX 代码。我之前试过 string.format。
  • 实际值为 0。黑色 (#000000)。我认为,它只返回字体颜色。还是有错误,我不知道。
  • 请通过任何免费的文件共享服务器共享示例输入文档,例如保管箱。然后我将分享代码示例。 - 我与 Aspose 合作,担任开发人员宣传员。

标签: c# aspose.words


【解决方案1】:

请使用 Run.Font.Shading.BackgroundPatternColor 属性获取 Run 节点的 BackgroundColor。

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);
        }
    }
}

我与 Aspose 合作,担任开发人员传道者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 2023-03-22
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多