We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to shrink the font size of text inside the text frame dynamically when it overflows

Hi Team,

I would like to shrink the size of the text in a text frame when the text given inside the frame is overflowing out of the size of the slide. Below is the export code that I am using to frame the text frame content for exporting the slide.

protected override void Export(ISlide slide, int currentSlideIndex)
{
var frameShape = AddShapeUtils.AddHtmlFrame(slide, Data.Text, X, Y, Width, Height);

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(Data.Text);

var htmlParagraphs = htmlDoc.DocumentNode.ChildNodes.Where(x => !x.Name.Contains("#text"));
var paragraphs = ((IAutoShape)frameShape).
TextFrame.Paragraphs;

Color defaultFgColor = DesignSettings.GetValueOrDefault("@UISetting-OnScreenReport-Text-Font-Color").GetColor();
FontData defaultFontType =
new FontData(DesignSettings.GetValueOrDefault("@UISetting-OnScreenReport-Text-Font-Family").Split(’,’)[0]);

for (int i = 0; i < paragraphs.Count; i++)
{
counter = 0;
VisitNode(htmlParagraphs.ElementAt(i), paragraphs[i].Portions, Color.White, defaultFgColor, defaultFontType, 13, DesignSettings);
}
AddShape(frameShape);
}

private int counter = 0;
public void VisitNode(HtmlNode node, IPortionCollection portions, Color backgroundColor, Color foregroundColor, FontData fontType, float fontSize, Dictionary<string,string> designSettings)
{
string bg = “background-color:”;
string fg = “color:”;
string font = “font-family:”;
string fs = “font-size:”;

if (node.HasAttributes && node.Attributes.Contains(“style”) && node.Attributes[“style”].Value.IndexOf(bg) != -1)
{
backgroundColor = node.Attributes[“style”].Value.Substring(bg.Length).GetColor();
}

if (node.HasAttributes && node.Attributes.Contains(“style”) && node.Attributes[“style”].Value.IndexOf(fg) == 0)
{
foregroundColor = node.Attributes[“style”].Value.Substring(fg.Length).GetColor();
}

if (node.HasAttributes && node.Attributes.Contains(“style”) && node.Attributes[“style”].Value.IndexOf(font) != -1)
{
var titleFontsArray = node.Attributes[“style”].Value.Substring(font.Length).Split(’,’);
fontType = new FontData(titleFontsArray[0]);
}

if (node.HasAttributes && node.Attributes.Contains(“style”) && node.Attributes[“style”].Value.IndexOf(fs) != -1)
{
string s = node.Attributes[“style”].Value;
fontSize = s.Substring(fs.Length, s.Length - fs.Length - 2).GetFontSize();
}
if (node.Name == “#text”)
{
portions = FormatPortions(portions);
portions[counter].PortionFormat.FillFormat.FillType = FillType.Solid;
portions[counter].PortionFormat.FillFormat.SolidFillColor.Color = foregroundColor;
portions[counter].PortionFormat.LatinFont = fontType;
portions[counter].PortionFormat.FontHeight = fontSize;

if (node.InnerText != “\n\t”)
{
portions[counter++].PortionFormat.HighlightColor.Color = backgroundColor;
}
}

foreach (HtmlNode childNode in node.ChildNodes)
VisitNode(childNode, portions, backgroundColor,foregroundColor, fontType, fontSize, designSettings);
}


Thanks,
Priya

Hi Priya,


Thank you for sharing the code.

However, due to missing methods and variables, I am unable to use the code shared by you. You can use the AutofitType property of the textframe to make the content auto fit as per the size of the text frame. e.g.

txtFrame.TextFrameFormat.AutofitType = TextAutofitType.Normal;

If you still face the issue, please share your sample application with us and we will get back to you soon.

Thanks & Regards,

That worked like a piece of cake. Thanks alot. :slight_smile: :slight_smile: