Office 2007 [及更高版本]互操作:检索RGB颜色 [英] Office 2007 [and higher] interop: retrieve RGB-color

查看:126
本文介绍了Office 2007 [及更高版本]互操作:检索RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


更新:如果您需要确定Office文档(格式2007)中的rgb-color,请看下面的答案.


UPDATE: If you need to determine rgb-color in office document (format 2007) look at my answer below.

具有:

  • VS2010 PIA中的Interop.Word.dll ver.14,
  • VS2010 Express Edition
  • MS Word 2010(版本14)
  • 不使用Interop手动在提到的Word中制作的
  • .docx文件.文件包含几个带有彩色角单元格的表.
  • Interop.Word.dll ver.14 from VS2010 PIA,
  • VS2010 Express Edition
  • MS Word 2010 (ver.14)
  • .docx-file made in mentioned Word manually without Interop. File contains several tables with colored corner cells.

目的: 用Interop构建另一个.docx文件时,这些表将根据其角上的颜色填充渐变色.
出现问题的地方:我需要将表格角落的颜色从WdColor转换为System.Drawing.Color以计算渐变.因此,我使用单元格的Shading.BackgroundPatternColor属性.而且我发现有时它包含正确的BGR 24位值,有时却没有.
第二种情况仅在单元格具有主题调色板颜色之一时出现(标准和rgb-调色板颜色效果很好,但是主题调色板颜色会导致问题).例如,当我设置0x00F2F2F2(最浅的灰色)颜色时,它正确地存储在.docx文件存档内的document.xml中,但Shading.BackgroundPatternColor属性设置为0xDC00F2FF.因此ColorTranslator.FromOle返回不同的颜色.
顺便说一句,枚举中的灰色没有WdColor. .Net Reflector导致的最浅的灰色wdColorGray05 = 0xF3F3F3.这意味着并非所有调色板默认颜色都对应于枚举颜色.
另外,如果我在Word的RGB调色板中手动设置了相同的颜色(即242、242、242),请保存文件并通过Interop再次打开-颜色将正确设置为0x00F2F2F2
问题:有人遇到过这个问题吗?如何从Shading.BackgroundPatternColor属性正确检索RGB颜色?为什么此属性与document.xml中存储的值不对应?

Purpose: To build another .docx-file with Interop contained those tables filled with gradient color based on colors in its corners.
Where problem appears: I need to transform colors in tables corners from WdColor to System.Drawing.Color to calculate gradient. So I work with cell's Shading.BackgroundPatternColor property. And I found that sometimes it contains correct BGR 24-bit value and sometimes it doesn't.
The second case appears only when cell has one of the theme palette colors (standard and rgb-palette colors works well, but theme palette colors lead to problem). For example, when I set 0x00F2F2F2 (the lightest gray) color then it stores in document.xml inside .docx-file archive correctly but Shading.BackgroundPatternColor property is set to 0xDC00F2FF. So ColorTranslator.FromOle returns different color.
Btw, there's no WdColor for this gray in enum. The lighest gray wdColorGray05 = 0xF3F3F3 due to .Net Reflector. It means that not all palette default colors correspond to enum colors.
Also if I set manually the same color in RGB-palette in Word (ie. 242, 242, 242), save file and open again by Interop - color will be set properly as 0x00F2F2F2!
Question: Anybody had that problem? How to properly retrieve RGB color from Shading.BackgroundPatternColor property? Why this property doesn't correspond to value stored in document.xml?

推荐答案

这是我第二次在从Office文档中检索RGB颜色时遇到问题.第一次是Excel 2007 .xlsx文件格式,现在是Word 2010 .docx(不过仍然是2007格式). 因此,经过一番搜寻,我决定为所有将遇到同样麻烦的人回答我自己的问题.

It's second time I have problems with retrieving RGB-colors from office documents. First time it was Excel 2007 .xlsx file format, now it's Word 2010 .docx (still 2007 format though). So after a little search I decided to answer my own question for all those fellows who will have the same trouble.

有关更深入的解释和示例,我将带您到文章,该文章对我有帮助很多.由于C#开发人员是在VBA上编写的,因此本文中使用的示例可能较难阅读,因此我在的实现中附加了链接. rgb颜色检索器.

For more deep explanation and examples I send you to the article which helped me a lot . Since examples used in this article probably will be harder to read for C# developers since they're written on VBA I attached link to my implementation of rgb color retriever.

所以.如果您打开一个Office程序(尤其是Excel或Word),则可以设置大多数对象,文本,背景等的颜色.并且会显示一个对话框来选择它.在Office 2007或更高版本中,您将看到一组10种standard颜色和一组60种颜色based on theme.如果单击更多颜色..",则可以从预定的颜色集中或从RGB调色板中选择颜色.

So. If you open one of the Office program (particularly Excel or Word) you can set color of the most objects, text, background etc. And there's a dialog showed to select it. In Office 2007 or higher you'll see a set of 10 standard colors and set of 60 colors based on theme. If you click on 'More colors..' you'll be able to select color from predetermined color set or from RGB-palette.

您从该对话框中选择颜色的方式确定了存储颜色的格式.存储颜色值的属性是一个32位整数,其中第一个最高有效字节(称为FormatByte)用于格式说明,另一个24位用于颜色值或其他任何值(称为这些24位ColorValue).以下是可能的格式规范:

The way you've chosen a color from that dialog determines format for storing the color. The property which stores color value is a 32-bit integer where 1st the most significant byte (let's call it FormatByte) is for format specification and the other 24-bit for color value or anything else (let's call these 24-bits ColorValue). Here're possible format specifications:

  • FormatByte == 0x00
    ColorValue是常见的BGR值.在C#中,您可以通过ColorTranslator.FromOle(ColorValue);检索RGB.当您选择标准颜色或更多颜色.."对话框中的一种颜色(预定或调色板)时,将使用此格式.

  • FormatByte == 0x00
    ColorValue is common BGR-value. In C# you can retrieve RGB by ColorTranslator.FromOle(ColorValue);. This format is used when you've selected standard colors, or one of the colors from 'More colors..' dialog (predetermined or palette).

FormatByte == 0xFF
ColorValue将为0x000000.它是wdColorAutomatic值.这是一种对比色,这就是我所了解的一切(在我的情况下,背景始终是白色,字体始终是黑色).没有更多的研究.

FormatByte == 0xFF
ColorValue will be 0x000000. It's wdColorAutomatic value. It's a contrast color and that's all what I know about it (in my case It always was white for background and black for font). Haven't researched it more.

FormatByte == 0x80
ColorValue的范围为[0x000000,0x000018].您可以在文档的ActiveX控件中遇到的这些颜色.这是系统KnownColors(有一个包含该值的c#超集-System.Drawing.KnownColor).如果我理解正确,您也可以通过ColorTranslator.FromOle(_color);检索RGB,其中_color均为32位属性值,因为由于ColorTranslator.FromOle()的实现反映了它检查颜色是否来自KnownColor枚举.但是在解析要批准的Office文件时,我从来没有遇到过这些值.

FormatByte == 0x80
ColorValue will be in range [0x000000, 0x000018]. These colors you can meet in ActiveX controls in a document. It's a system KnownColors (there's a c# superset - System.Drawing.KnownColor which contains that values). If I understood right you can retrieve RGB also by ColorTranslator.FromOle(_color);, where _color is all 32-bit property value, because due to reflected implementation of ColorTranslator.FromOle() it checks if color is from KnownColor enum. But I've never been faced with that values while parsing Office files to approve.

FormatByte in range [0xD4, 0xDF]
在这种情况下,您要处理颜色based on theme.它表示为基础颜色和色调或阴影偏移的索引.

FormatByte in range [0xD4, 0xDF]
In that case you deal with color based on theme. It represents as index of base color and tint or shade shift.

让我们更深入地研究最后一种情况,因为困难更多.
如您所见,FormatByte的前半部分始终为0xD,另一半从0x40xF.此后半部分是10种基色之一的索引.

Let's take a deeper look at the last case because there're much more difficulties.
As you see the first half of FormatByte is always 0xD and the other half varies from 0x4 to 0xF. This second half is an index of one of the 10 base colors.

Word在该索引中有一个wdThemeColorIndex枚举,可以将其转换为更基本的Office msoThemeColorSchemeIndex枚举(它放置在Microsoft.Office.Core.dll中,可以从tabPage .COM链接为Microsoft Office XX.0 Object Library,其中XX.0-版本为办公室).您可以查看上面链接的有关翻译工作表或VBA函数的文章,或者我对C#方法的实现.从此msoThemeColorSchemeIndex,我们可以通过ActiveDocument.DocumentTheme获得RGB属性.
然后,我们从ColorValue中检索色调或阴影,将基色转换为HSL(色相,饱和度,亮度),对其应用色调或阴影,然后将结果转换回RGB.

There's a wdThemeColorIndex enumeration in Word for that index and it can be translated to more basic Office msoThemeColorSchemeIndex enumeration (it's placed in Microsoft.Office.Core.dll which can be linked from tabPage .COM as Microsoft Office XX.0 Object Library where XX.0 - version of Office). You can take a look to article linked above for the translation sheet or VBA function or to my implementation to C# method. From this msoThemeColorSchemeIndex we can obtain RGB property by ActiveDocument.DocumentTheme.
Then we retrieve tint or shade from ColorValue, translate base color to HSL (hue, saturation, lightness), apply tint or shade to it and translate result back to RGB.

这篇关于Office 2007 [及更高版本]互操作:检索RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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