解析 Word 文档中使用的颜色,用作 ListViewItem 的背景色 - 颜色错误 [英] Parse colors used in Word document, use as backcolor for ListViewItem - wrong color

查看:53
本文介绍了解析 Word 文档中使用的颜色,用作 ListViewItem 的背景色 - 颜色错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出 Word 文档中使用的所有字体颜色,并将它们显示为彩色 ListViewItems.

I'm trying to list all font colors used in a Word document, display them as colored ListViewItems.

我可以解析文档并获得所有独特的字体颜色.

I can parse the doc and get all unique font colors.

什么不起作用?- 以正确的颜色获取 ListViewItems.Grey35 显示为黄色,绿色显示为深绿色.

What does not work? - Getting the ListViewItems in the correct color. Grey35 appears as yellow, green as dark green.

这是我的活动代码部分

var maxnum = doc.Words.Count;
var ind = 0;
foreach (Word.Range wd in doc.Content.Words)
{
    if (!string.IsNullOrEmpty(wd.Text.Trim('\r', '\n', ' ')))
    {
        ind++;
        bkwParseColors.ReportProgress(100*ind/maxnum, wd.Font.Color);
    }
}

这是我用它做的:

private void bkwParseColors_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    var color = (Word.WdColor)e.UserState;
    var drin = lstColors.FindItemWithText(color.GetHashCode().ToString());
    if(drin==null)
    {
        var li = new ListViewItem(color.GetHashCode().ToString());
        li.BackColor = ColorTranslator.FromOle((int) color);
        lstColors.Items.Add(li);
    }
}

唉,我得到的是黄色而不是灰色,而不是 Word 的绿色,我得到的是深绿色,而不是浅灰色,而是 50% 的深灰色(至少要深得多).唯一正确的颜色是黑色.

Alas, instead of grey, I get yellow, instead of Word's green I get a dark, full green and instead of a light grey I get a dark 50% grey (at least much darker). The only correct color is black.

在调试过程中,我还发现第一个灰色(在 ListView 中转换为黄色)被列为 wdWhite.:-??

During debugging I also found that the first grey, which translates to yellow in the ListView is listed as wdWhite. :-??

看起来我只得到了全彩",但缺少一些价值(亮度).有人能告诉我如何获得正确的颜色吗?

It almost looks like I am only getting "full colors" with some value (lightness) missing. Can someone tell me how to get the correct color?

使用 Word 2010、VS 社区 2013、框架 4.0.

Using Word 2010, VS Community 2013, Framework 4.0.

我似乎越来越近了!

==> 行为会有所不同,具体取决于我是使用单击文本颜色时直接出现的主题颜色"对文本进行着色,还是单击更多颜色"然后从色轮中选择一种!如果我从色轮为文本着色,我似乎得到了正确的值,包括灰色.如果我使用首先出现的默认调色板中的灰色,则灰色表示为白色,背景 1,更深的 xx%",这可以解释 wdWhite.

==> The behavior is different depending on whether I have colored the text using "theme colors" that come up directly when clicking text color, or whether I click "More colors" and then pick one from the color wheel! If I color text from the color wheel, I seem to be getting the correct value, including grey. If I use the grey from the default palette that comes up first, grey is denoted as "White, Background 1, darker xx%", which would explain the wdWhite.

不幸的是,这适用于已经包含彩色文本并且着色不受我控制的文档.所以我需要一种方法来包含主题颜色".

Unfortunately, this is meant for docs that already contain colored text and the coloring is not under my control. So I need a way to include "theme colors" into this.

Edit2: 看起来我的问题的答案就在这里:Office 2007 [及更高版本] 互操作:检索 RGB 颜色或者基本上在那里链接的页面中:http://www.wordarticles.com/文章/颜色/2007.php#UIConsiderations

It looks as though the answer to my question lies here: Office 2007 [and higher] interop: retrieve RGB-color Or basically in the page linked there: http://www.wordarticles.com/Articles/Colours/2007.php#UIConsiderations

我会自己解决这个问题,希望从主题颜色中获得正确的颜色值.

I will work myself through this in the hope to be getting correct color values from theme colors.

推荐答案

现在得到了满意的结果.我首先做的是

Got a satisfying result now. What I first did was

  1. 使用 pkuderov 中链接的 RgbColorRetriever 类接受此线程的答案:Office 2007 [及更高版本]互操作:检索 RGB-颜色
  2. 由于生成的系统颜色比 Word 颜色略深,我还应用了 Pavel Vladov 在此线程中提出的照明效果(第二个答案,不是公认的):C#:根据系统颜色创建较浅/较深的颜色

编辑 唉,这似乎不适用于某些主题灰色.但是我也需要它来处理这些.

Edit Alas, this does not seem to work for certain theme grays. However I need it to also work with these.

因此:使用 Open XML SDK:

private void bkwParseColors_DoWork(object sender, DoWorkEventArgs e)
{
    var docItem = (string) e.Argument;
    using (var docx = WordprocessingDocument.Open(docItem, false))
    {
        var ind = 0;
        var maxnum = docx.MainDocumentPart.Document.Descendants<Run>().Count();
        foreach (Run rText in docx.MainDocumentPart.Document.Descendants<Run>())
        {
            if (rText.RunProperties != null)
            {
                if (rText.RunProperties.Color != null)
                {
                    ind++;
                    bkwParseColors.ReportProgress(100*ind/maxnum, rText.RunProperties.Color);
                }
            }
        }
    }
}

以正确的颜色创建ListViewItem并存储Word颜色值以及主题颜色的进度变化方法:

Progress change method for creating ListViewItem in correct color and storing Word color value as well as theme color:

private void bkwParseColors_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;

    var color = (DocumentFormat.OpenXml.Wordprocessing.Color)e.UserState;
    var thema = "";
    if (color.ThemeColor!=null)
        thema = color.ThemeColor.Value.ToString();

    var farbe = color.Val.Value; //hex RGB
    var drin = lstColors.FindItemWithText(farbe);
    if(drin==null)
    {
        var li = new myListItem
        {
            Design = thema,
            Farbe = farbe,
            Text = farbe,
            BackColor = ColorTranslator.FromHtml("#" + farbe)
        };
        lstColors.Items.Add(li);
    }
}

一些附加信息:我需要所有这些,因为我需要隐藏/取消隐藏某种颜色的文本,但该颜色永远不确定,即取决于客户的奇思妙想和/或文档中已使用的颜色...

Some additional info: I needed all this because I need to hide/unhide text of a certain color, but that color is never certain, i.e. depends on the whims of the customer and/or the colors already used in the document...

因此,为了完成起见,以下是我隐藏文档中除所选颜色文本之外的所有文本的方法:

So for completions sake, here is how I hide all text in the document except for text in the selected color:

private void bkwEinblenden_DoWork(object sender, DoWorkEventArgs e)
{
    var args = (List<object>) e.Argument;
    var pfad = (string) args[0];
    var color = (myListItem) args[1];
    using (var docx = WordprocessingDocument.Open(pfad, true))
    {
        var ind = 0;
        var maxnum = docx.MainDocumentPart.Document.Descendants<Run>().Count();
        foreach (Run rText in docx.MainDocumentPart.Document.Descendants<Run>())
        {
            bkwEinblenden.ReportProgress(100*ind/maxnum);
            var vanish = new Vanish() { Val = OnOffValue.FromBoolean(true) };
            if (rText.RunProperties == null)
            {
                var runProp = new RunProperties {Vanish = vanish};
                rText.RunProperties = runProp;
            }
            else
            {
                if (rText.RunProperties.Vanish == null)
                    rText.RunProperties.Vanish = vanish;
                else
                {
                    rText.RunProperties.Vanish.Val = OnOffValue.FromBoolean(true);
                }
            }
            if (rText.RunProperties.Color != null)
            {
                if (rText.RunProperties.Color.Val == color.Farbe)
                {
                    if (!string.IsNullOrEmpty(color.Design))
                    {
                        if (rText.RunProperties.Color.ThemeColor.Value.ToString() == color.Design)
                        {
                            rText.RunProperties.Vanish.Val = OnOffValue.FromBoolean(false);
                        }
                    }
                    else
                    {
                        rText.RunProperties.Vanish.Val = OnOffValue.FromBoolean(false);
                    }
                }
            }
        }
    }
}

这篇关于解析 Word 文档中使用的颜色,用作 ListViewItem 的背景色 - 颜色错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆