Free Support Forum - aspose.com

Graphics Reference for MeasureString

I am working with the latest demo of Aspose Words (downloaded Friday). I have a good chuck of a project done and I am stuck on one last thing.

I'm creating reports in Word format for a client. The reports are in a columnar format and the columns are a very specific width. I need to be able to measure a string before I add it to see if it will fit and if not, then I can shorten the string until it does. I'm at a loss trying to get a reference to a System.Drawing.Graphics object so I can do the measuring (MeasureString or MeasureCharacterRanges). Everything I can see is getting a Graphics object from an event argument of some kind depending on the project. Is there a way to get the Graphics object in Words? OR, is there a better way to measure a string in Words?

Any help would be appreciated.

Thank you,

Trent

I am afraid that measuring string width is not possible right now by means of Aspose.Words. But you can do it in your own code by using .NET GDI+ capabilities. Try searching the web for c# gdi+ "string width". There will be plenty of articles coming out on this topic. For example, this one:

http://www.codeproject.com/cs/media/measurestring.asp

As for Aspose.Words, its rendering and drawing capabilities are currently in development. We are going to introduce a rich drawing and rendering API in one of the future versions.

Best regards,

Thank you for your quick reply. The article you cited is one that I have been through today a few times. You'll notice that in the article, to call the methods, you pass in a reference to a Graphics object. I don't know if I'm missing something simple, but how can I get a Graphics object? All I can tell is that the Graphics object is created off of some particular context, like printing a page or rengering some text to a graphic area, like a windows form. Does the document have a graphics context?

I'm lost!

Thanks again,

Trent

Aspose.Words does not have any visual context at the moment. All you need is to create a graphics context in your project and use it for measuring strings. For this you can use a newly created bitmap object for example:

Graphics g = Graphics.FromImage(myBitmap);

Please check MSDN article called "Creating Graphical Images with GDI+" for more information.

Best regards,