C# 解析Word文档中使用的颜色,用作ListViewItem的背景色-颜色错误
我试图列出Word文档中使用的所有字体颜色,并将它们显示为彩色ListViewItems 我可以解析文档并获得所有唯一的字体颜色 什么不起作用以正确的颜色获取ListViewItems。灰色35显示为黄色,绿色为深绿色 下面是我的活动代码部分C# 解析Word文档中使用的颜色,用作ListViewItem的背景色-颜色错误,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我试图列出Word文档中使用的所有字体颜色,并将它们显示为彩色ListViewItems 我可以解析文档并获得所有唯一的字体颜色 什么不起作用以正确的颜色获取ListViewItems。灰色35显示为黄色,绿色为深绿色 下面是我的活动代码部分 var maxnum = doc.Words.Count; var ind = 0; foreach (Word.Range wd in doc.Content.Words) { if (!string.IsNullOrEmpty(wd.Text.
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%的深灰色(至少要深得多)。唯一正确的颜色是黑色
在调试期间,我还发现ListView中转换为黄色的第一个灰色列为wdWhite.:-
看起来我只得到了“全彩”,缺少了一些值(亮度)。
有人能告诉我如何得到正确的颜色吗
使用Word 2010,VS Community 2013,框架4.0
编辑:我似乎越来越近了
==>根据我是使用单击文本颜色时直接出现的“主题颜色”为文本着色,还是单击“更多颜色”,然后从色轮中选择一种颜色,行为会有所不同!如果我从色轮给文本上色,我似乎得到了正确的值,包括灰色。如果我使用第一个出现的默认调色板中的灰色,灰色表示为“白色,背景1,较暗的xx%”,这可以解释白色
不幸的是,这是指已经包含彩色文本的文档,并且颜色不在我的控制之下。所以我需要一种方法,包括“主题颜色”到这个
Edit2:看来我的问题的答案就在这里:
或者基本上在链接的页面中:
我将自己解决这个问题,希望从主题颜色中获得正确的颜色值。现在得到了令人满意的结果。 我首先做的是
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);
}
}
}
}
}
}
private void bkwinblenden_DoWork(对象发送方,DoWorkEventArgs e)
{
var args=(列表)e.参数;
var pfad=(字符串)args[0];
var color=(myListItem)args[1];
使用(var docx=WordprocessingDocument.Open(pfad,true))
{
var-ind=0;
var maxnum=docx.MainDocumentPart.Document.subjections().Count();
foreach(在docx.MainDocumentPart.Document.subjections()中运行rText)
{
报告进度(100*ind/maxnum);
var vanish=new vanish(){Val=OnOffValue.FromBoolean(true)};
if(rText.RunProperties==null)
{
var runProp=new RunProperties{invision=invision};
rText.RunProperties=runProp;
}
其他的
{
if(rText.RunProperties.vash==null)
rText.RunProperties.vash=消失;
其他的
{
rText.RunProperties.Vanish.Val=OnOffValue.FromBoolean(真);
}
}
if(rText.RunProperties.Color!=null)
{
if(rText.RunProperties.Color.Val==Color.Farbe)
{
如果(!string.IsNullOrEmpty(color.Design))
{
if(rText.RunProperties.Color.ThemeColor.Value.ToString()==Color.Design)
{
rText.RunProperties.Vanish.Val=OnOffValue.FromBoolean(false);
}
}
其他的
{
rText.RunProperties.Vanish.Val=OnOffValue.FromBoolean(false);
}
}
}
}
}
}
试试:Font.TextColor。RGB@CindyMeister没有区别。不适用于主题颜色。我发现VBA代码很难筛选。有趣的是,对我来说,它同时选择了“颜色轮”和主题颜色。。。不管怎样,很高兴你找到了一个解决办法,而且看起来很好:——)@Cindymister Pick?是 啊但正确吗?尝试使用Word中的读取值查找该颜色的文本。这适用于主题颜色吗?如果是:您使用的是什么Word版本?一旦我们升级到更高的Office版本可能会很有趣。如果我理解正确,那么是的,它确实有效。Word 2010。这是相关的代码行(宏记录)。颜色值与TextColor.RGB返回的内容匹配,并运行录制的宏:.Find.Font.color=-603923969@CindyMeister谢谢你到目前为止的想法。我想我最好尝试一种使用OpenXMLSDK的新方法。我将更容易地访问使用过的主题,并使用自定义ListViewItem类来存储所有必要的值。毕竟,我需要对彩色物品做些什么,而我一直遇到灰色主题的麻烦,因为它们后来顽固地拒绝在文档中找到。Grrrr…:)
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);
}
}
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);
}
}
}
}
}
}