How to get font by name or add new fonts to a presentation


#1

Hello,

C#

///


/// Gets font id by his name in the specified presentation.
///

/// Presentation object.
/// Name of the fornt.
/// Id of the font.
static int GetFontId(Presentation ppt, string fontName)
{
// Find font by name. It doesn’t check Charset, Family and etc.
for (int i = 0; i < ppt.Fonts.Count; i++)
if (fontName.CompareTo(ppt.Fonts[ i ].FontName) == 0)
return i;
// Add new font by cloning first font of the presentation,
// change necessary values and return new Id
FontEntity newFont = new FontEntity(ppt, ppt.Fonts[0]);
newFont.FontName = fontName;
return ppt.Fonts.Add(newFont);
}

VB.Net

'/
'/ Gets font id by his name in the specified presentation.
'/

'/ Presentation object.
'/ Name of the fornt.
'/ Id of the font.
Shared Function GetFontId(ByVal ppt As Presentation, ByVal fontName As String) As Integer
’ Find font by name. It doesn’t check Charset, Family and etc.
Dim i As Integer
For i = 0 To ppt.Fonts.Count - 1
If fontName.CompareTo(ppt.Fonts(i).FontName) = 0 Then
Return i
End If
Next
’ Add new font by cloning first font of the presentation,
’ change necessary values and return new Id
Dim NewFont As FontEntity = New FontEntity(ppt, ppt.Fonts(0))
NewFont.FontName = fontName
Return ppt.Fonts.Add(NewFont)
End Function