您可以使用 Aspose.Words 中的 Font 类和 MeasurementUtils 类来实现该功能。

首先,您需要创建一个新的 Font 对象,设置固定字体和字体大小。然后,使用 MeasurementUtils 类中的 MeasureString 方法来计算字符串在给定字体和字号下的宽度,返回值将是以像素为单位的浮点数。最后,将像素值转换为磅值,通过将像素值除以 96(1 英寸包含 96 个像素)来实现。

以下是示例代码:

// 创建固定字体
Font font = new Font('Arial', 12);

// 计算字符串宽度(以像素为单位)
float widthInPixels = MeasurementUtils.MeasureString('Your string', font);

// 将像素值转换为磅值
float widthInPoints = widthInPixels / 96 * 72; // 1 英寸 = 96 像素,1 磅 = 72 点

// 输出宽度值
Console.WriteLine('String width in points: ' + widthInPoints);
C# Aspose.Words: 计算字符串在 Word 中的宽度(单位:磅)

原文地址: https://www.cveoy.top/t/topic/nKca 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录