0
votes

I am developing an application that has to recreate how PowerPoint fits text (shrink-to-fit) in order to determine the necessary font scale needed to allow a set to text to fit inside a given textbox in PowerPoint.

I am usng the Aspose library to generate the slides, however the font scale computed by Aspose is often incorrect, so when the presentation is first opened, some text may be displayed larger than expected, causing long text to flow out of the textbox and over into other adjacent textboxes.

At first I tried using GDI MeasureString() and manual word-wrapping algorithm, but this was wildly inaccurate. I found that PowerPoint uses DirectWrite to render text to screen, so I tried using ShaprDX and TextLayout and got much better results.

My algorithm involves repeatedly trying to layout the text in a given textbox and reducing the font scale until it fits.

However I recently discovered that PowerPoint seems to have a wider character spacing than SharpDX does, not much, but after some letters it results in text wrapping earlier in PowerPoint than in SharpDX.

Sample image

If you look at the sample image, I took a screenshot of the PowerPoint slide at 100%, and a screenshot of SharpDX rendered to a form. I have overlaid the SharpDX image on top of the PowerPoint image.

The SharpDX text is longer, because it already "fits" at the given font size.

Text: Αποτελέσματα έναντι Παγκόσμιο Πρότυπο Υψηλών Επιδόσεων Font: Arial 11pt Textbox size: 444px x 24px (4.63" x 0.26") 0 margins all around

In PowerPoint, without shrink to fit, this text will wrap.

In SharpDX the text does not wrap.

I am converting the font size from points to pixels using the formula:

pixels = points x 96f / 72f;

I haven't found a way to set the character spacing in SharpDX. Is this even possible?

1
Welcome to SO. Please read What topics can I ask about and How to ask a good question And the perfect question And how to create a Minimal, Complete and Verifiable example SO is not a free Coding or Code Conversion or Debugging or Tutorial or Library Finding service Here at SO we fix your attempts, we do not code things for youMingebag
In the PowerPoint object model, any textrange has BoundHeight, BoundTop, BoundLeft, and BoundWidth properties that will tell you how large the text "sets". If Aspose gives you access to these properties, you can keep reducing or enlarging the text size until you have a match.Steve Rindsberg

1 Answers

0
votes

I have observed your requirements and like to share that Aspose.Slides only use the internal properties of PowerPoint for text wrapping like shrinking text w.r.t shape or adjusting shape w.r.t to amount of text in the shape. There is no such access to bounds. However, you can get the x, y, width and heights of the shape encapsulating text in it.

Presentation pres=new Presentation("TEST.pptx");
ISlide slide=pres.Slides[0];
//Shape holding text is an AutoShape
IAutoShape ashp=(IAutoShape)slide.Shapes[0]; 

float x=ashp.X;
float y=ashp.Y;
float width=ashp.Width;
float height =ashp.Height;

Please note that the values returned are mapped to 72 pixels per inch. i.e. A standard slide with 10'' X 7.5 '' has size in Aspose.Slides equivalent to 720 x 540. I hope this will be helpful.

I work as a Developer/Evangelist at Aspose.