Aspose.Words ...以像素为单位计算文本宽度

时间:2012-04-24 14:47:28

标签: text alignment words aspose

我有一个MVC3 C#.Net网络应用程序。我正在使用Aspose.Words来创建MS Word文档。我要求不在文档中包含表格。但是,在文档的几行中,文本的对齐方式根据文本的宽度而不对齐 例如:

看起来不错

Proposal Name: My Proposal         Date:04/24/2012

这不是

Proposal Name: My Prop         Date:04/24/2012

应该是

Proposal Name: My Prop             Date:04/24/2012

根据文本第一位的宽度,我需要以像素为单位计算宽度(我认为)并在必要时插入TAB。

任何想法如何做到这一点?

2 个答案:

答案 0 :(得分:1)

你可以使用Graphics.MeasureString函数,它根据你的字体为你提供字符串的宽度。有关详细信息,请转到Here

干杯,

伊赫桑

答案 1 :(得分:0)

以下代码示例返回当前实体相对于页面左上角的边界矩形。

Document doc = new Document(MyDir + "in.docx");

LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    var renderObject = layoutCollector.GetEntity(para);
    layoutEnumerator.Current = renderObject;
    RectangleF location = layoutEnumerator.Rectangle;
    Console.WriteLine(location);
}

src:https://www.aspose.com/community/forums/thread/541215/replace-run-text-with-string-of-spaces-of-same-pixel-length.aspx