MikeTWebb
MikeTWebb

Reputation: 9279

Aspose.Words...calculating width of text in pixels

I have an MVC3 C# .Net web app. I am using Aspose.Words to create an MS Word document. I have a requirement to not include tables in the document. However, on several lines of the document the alignment of the text is mis-aligned depending on the width of the text.
For example:

This looks good

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

This does not

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

It should be

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

Based on the width of the first bit of text, I need to calculate the width in pixels (I think) and insert a TAB if necessary.

Any ideas how to do this?

Upvotes: 0

Views: 1689

Answers (2)

gremwell
gremwell

Reputation: 1457

The following code example returns the bounding rectangle of the current entity relative to the page top left corner.

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

Upvotes: 0

Ehsan
Ehsan

Reputation: 4464

you can use Graphics.MeasureString function which gives you the width of your string in pixels based on your font. for more info go Here

Cheers,

Ehsan

Upvotes: 1

Related Questions