通过 .NET COM 互操作设置 System.Drawing.Color

1. 问题背景

在尝试使用 Aspose.Words 库执行 COM 互操作时,遇到了一个关键问题:无法设置颜色。理论上,可以通过向 DocumentBuilder.Font.Color 赋值来设置颜色,但尝试时却出现了 OLE 错误 0x80131509。

以下代码示例演示了这个问题:

python 复制代码
from win32com.client import Dispatch
Doc = Dispatch("Aspose.Words.Document")
Builder = Dispatch("Aspose.Words.DocumentBuilder")
Builder.Document = Doc
print Builder.Font.Size
print Builder.Font.Color

执行这段代码后,输出结果为:

python 复制代码
12.0
Traceback (most recent call last):
  File "aaa.py", line 6, in <module>
    print Builder.Font.Color
  File "D:\Python26\lib\site-packages\win32com\client\dynamic.py", line 501, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2146233079, 'OLE error 0x80131509', None, None)
```python

尝试使用像 Font.Color = 0xff0000 这样的方式设置颜色也会出现同样的错误消息。

然而,以下代码在 C# 中可以正常工作:

```python
using Aspose.Words;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.Font.Color = System.Drawing.Color.Blue;
            builder.Write("aaa");
            doc.Save("c:\\1.doc");
        }
    }
}
```python

这表明问题很可能出在 COM 互操作上。

## 2. 解决方案

要解决这个问题,可以使用以下两种方法之一:

* **方法一:使用 Aspose.Words .NET 库**

  这种方法是使用 Aspose.Words 的 .NET 库,这是一种用于 .NET 平台的 Word 处理库。它提供了与 COM 互操作类似的功能,但可以避免 COM 互操作中出现的问题。

  要使用 Aspose.Words .NET 库设置颜色,可以按照以下步骤操作:

  1. 创建一个 Document 对象。
  2. 创建一个 DocumentBuilder 对象,并将其与 Document 对象关联。
  3. 设置 DocumentBuilder.Font.Color 属性。
  4. 将文档保存到文件中。

  以下代码示例演示了如何使用 Aspose.Words .NET 库设置颜色:

```python
using Aspose.Words;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.Font.Color = System.Drawing.Color.Blue;
            builder.Write("aaa");
            doc.Save("c:\\1.doc");
        }
    }
}
  • 方法二:使用 System.Drawing.ColorConverter

    这种方法是使用 System.Drawing.ColorConverter 类,它可以将字符串或十六进制值转换为 System.Drawing.Color 对象。然后,可以使用此 System.Drawing.Color 对象来设置 DocumentBuilder.Font.Color 属性。

    以下代码示例演示了如何使用 System.Drawing.ColorConverter 类设置颜色:

python 复制代码
from win32com.client import Dispatch
Doc = Dispatch("Aspose.Words.Document")
Builder = Dispatch("Aspose.Words.DocumentBuilder")
Builder.Document = Doc
print Builder.Font.Size
Converter = ColorConverter()
Color = Converter.ConvertFromString("Blue")
Builder.Font.Color = Color
print Builder.Font.Color

执行这段代码后,输出结果为:

python 复制代码
12.0
16776960

这表明通过 System.Drawing.ColorConverter 类设置颜色成功了。

相关推荐
Y编程小白35 分钟前
PostgreSQL在Linux中的部署和安装教程
数据库·postgresql
CsharpDev-奶豆哥2 小时前
ASP.NET中for和foreach使用指南
windows·microsoft·c#·asp.net·.net
TiAmo zhang3 小时前
SQL Server 2019实验 │ 数据库和表的创建、修改与删除
数据库·oracle
disanleya3 小时前
MySQL默认密码不安全?如何首次登录并强化?
数据库·mysql·安全
花开富贵贼富贵3 小时前
MySQL 核心高级特性
运维·数据库·mysql
hello 早上好3 小时前
深入 Spring 依赖注入底层原理
数据库·sql·spring
API快乐传递者4 小时前
抓取淘宝商品详情商品数据API接口调用说明文档|获取淘宝商品价格主图数据等
数据库
济南java开发,求内推4 小时前
Redis一个服务器部署多个节点
服务器·数据库·redis
花菜会噎住4 小时前
Django视图与路由全解析:从URL到页面,一篇讲透
数据库·django·sqlite·函数
-雷阵雨-4 小时前
MySQL——数据库约束
数据库·mysql