如何使用C#知道ms word文档的背景颜色

发布于 2024-09-24 11:16:28 字数 1405 浏览 0 评论 0原文

我正在使用 Microsoft Office 12.0 对象库C# Office (word) 自动化。 我打开了一个“1.doc”文件,我需要检查该文件是否有背景颜色。

注意:我的意思是通过以下步骤应用背景颜色:

  • 打开MS Word 2003,然后打开一个文档。
  • 转到:格式菜单 -> 背景并选择颜色。

这里我在 C# 中拥有:

Object oMissing = System.Reflection.Missing.Value;

        //OBJECTS OF FALSE AND TRUE
        Object oTrue = true;
        Object oFalse = false;
        Object fileName = "c:\\1.doc";

        //CREATING OBJECTS OF WORD AND DOCUMENT
        Word.Application oWord = new Word.Application();
        Word.Document oWordDoc = new Word.Document();

        //MAKING THE APPLICATION VISIBLE
        oWord.Visible = true;

        //ADDING A NEW DOCUMENT TO THE APPLICATION
        oWordDoc = oWord.Documents.Open(
            ref fileName, ref oMissing, ref oFalse, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oTrue, ref oMissing, ref oMissing, ref oMissing);
        Console.WriteLine(oWordDoc.Background.Fill.ForeColor.RGB);
        Console.WriteLine(oWordDoc.Background.Fill.BackColor.RGB);

我不知道 ForeColor 或 BackColor 是否代表我需要的颜色,我尝试选择不同的背景颜色并执行上面的代码,每次我得到一个不同的整数值,如 (10092543, 255对于红色,....)但它没有意义,并且 BackColor 永远不会更改并固定为值(16777215)。 非常感谢。

I'm using C# office (word) automation by Microsoft Office 12.0 Object Library.
And I opened a "1.doc" file and I need to check if this file has a background color or not.

Note: I mean the background color applied by the following steps:

  • Open MS Word 2003, and open a document.
  • Go to : Format menu -> Background and choose color.

And here what I have in C#:

Object oMissing = System.Reflection.Missing.Value;

        //OBJECTS OF FALSE AND TRUE
        Object oTrue = true;
        Object oFalse = false;
        Object fileName = "c:\\1.doc";

        //CREATING OBJECTS OF WORD AND DOCUMENT
        Word.Application oWord = new Word.Application();
        Word.Document oWordDoc = new Word.Document();

        //MAKING THE APPLICATION VISIBLE
        oWord.Visible = true;

        //ADDING A NEW DOCUMENT TO THE APPLICATION
        oWordDoc = oWord.Documents.Open(
            ref fileName, ref oMissing, ref oFalse, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oTrue, ref oMissing, ref oMissing, ref oMissing);
        Console.WriteLine(oWordDoc.Background.Fill.ForeColor.RGB);
        Console.WriteLine(oWordDoc.Background.Fill.BackColor.RGB);

I do not know if the ForeColor or BackColor represent the color that i need, I tried to choose different background colors and executed the above code and every time I got a different integer value like (10092543, 255 for red, ....) but It does not make sense, and the BackColor is never changed and fixed at the value (16777215).
Many Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-10-01 11:16:28

你走在正确的轨道上。页面背景确实是Background 对象的前景色。您看到的不同值对应于 RGB 颜色值的整数表示形式。

如果您对不同的颜色组件感兴趣,可以使用以下代码:

Color color = Color.FromArgb(oWordDoc.Background.Fill.ForeColor.RGB);
int red = color.R;
int green = color.G;
int blue = color.B;

更新

Office 对象模型使用的颜色似乎使用与System.Drawing.Color不同的内部格式code> 因此,当您使用上面的示例代码时,通道可能会混淆(我忘记检查 Word.ColorFormat.RGB 的实际格式)。

您始终可以使用以下代码自行检索不同的颜色通道:

int wordColor = oWordDoc.Background.Fill.ForeColor.RGB;
int channel1 = (int)((wordColor >> 0x10) & 0xffL);
int channel2 = (int)((wordColor >> 0x8) & 0xffL);
int channel3 = (int)(wordColor & 0xffL);

You are on the right track. The page background is indeed the foreground color of the Background object. The different values that you see correspond to the integer representation of the RGB color values.

If you are interested in the different color components you can use the following code:

Color color = Color.FromArgb(oWordDoc.Background.Fill.ForeColor.RGB);
int red = color.R;
int green = color.G;
int blue = color.B;

Update

The color used by the Office object model seems to use a different internal format than System.Drawing.Color so the channels might get mixed up when you use the above sample code (I forgot to check the actual format of Word.ColorFormat.RGB).

You can always retrieve the different color channels yourself using the following code:

int wordColor = oWordDoc.Background.Fill.ForeColor.RGB;
int channel1 = (int)((wordColor >> 0x10) & 0xffL);
int channel2 = (int)((wordColor >> 0x8) & 0xffL);
int channel3 = (int)(wordColor & 0xffL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文