How to change fontstyle?

I want to change the fontstyle of a textframe in a rect. I have no Problem to change the font properties like bold or italic. Here is the code I have written:

private void CreateLegend(Slide slide, List<MyColumn> columnData, int leftOffset, int top, int height)

{

int left = leftOffset;

for (int i = 0; i < columnData.Count; i++)

{

MyColumn tmpColumn = columnDataIdea [I];

Rectangle rect = slide.Shapes.AddRectangle(left, top, (int)tmpColumn.Width, height);

rect.LineFormat.ShowLines = true;

rect.AddTextFrame(tmpColumn.name);

// Schrift setzen

// I want to set the fontstyle here, but how?

rect.TextFrame.Paragraphs[0].Portions[0].FontBold = true;

rect.TextFrame.Paragraphs[0].Portions[0].FontItalic = tmpColumn.font.Italic;

rect.TextFrame.Paragraphs[0].Portions[0].FontHeight = tmpColumn.font.Size;

rect.TextFrame.Paragraphs[0].Portions[0].FontColor = tmpColumn.font.Color;

// Hintergrundfarbe setzen

rect.FillFormat.Type = FillType.Solid;

rect.FillFormat.ForeColor = tmpColumn.backcolor;

// Damit die Spalten nebeneinander sitzen

left += (int)tmpColumn.Width;

}

}

Sven

Hello Sven

Add this line in your code to add different font

rect.TextFrame.Paragraphs[0].Portions[0].FontIndex=GetFontIndex(fontName, pres);

Also add this function and pass it the font name and your presentation object

int GetFontIndex(string fontName, Presentation pres)
{
    for (int fontIndex = 0; fontIndex < pres.Fonts.Count; fontIndex++)
    {
        if (pres.Fonts[fontIndex].FontName == fontName)
            return fontIndex;
    }

    FontEntity newFnt = new FontEntity(pres, pres.Fonts[0]);
    newFnt.FontName = fontName;

    return pres.Fonts.Add(newFnt);
}

Shakeel
Support Engineer
Aspose Tyumen Team

Thanks, it works nice.