How to change all font names in PowerPoint using C# in ASP.NET Core?
Syncfusion Presentation is a .NET PowerPoint Library used to create, read, edit and convert PowerPoint files to PDF and images programmatically without Microsoft Office or interop dependencies. Using this library, you can change all font names in PowerPoint Presentation using C#.
Steps to change all font names in PowerPoint using C#
- Create a new C# Console application project
- Install the Syncfusion.Presentation.Net.Core NuGet package as a reference to your project from Nuget.org.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Presentation;
- Use the following code snippet to change all font names in a Presentation.
C#
using (FileStream inputStream = new(Path.GetFullPath(@"../../../Template.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open an existing PowerPoint presentation.
using (IPresentation pptxDoc = Presentation.Open(inputStream))
{
//Replace font of the all text contents in the presentation.
ReplaceAllFonts(pptxDoc, "Times New Roman");
//Save the presentation
using (FileStream outputStream = new FileStream(@"../../../Result.pptx", FileMode.Create, FileAccess.Write)){
pptxDoc.Save(outputStream);
}
}
}
- Use the following code snippet to iterate through all slides in a Presentation.
C#
/// <summary>
/// Iterate through each slide in the Presentation.
/// </summary>
void ReplaceAllFonts(IPresentation pptxDoc, string replacingFontName)
{
//Iterate in master slides.
foreach (IMasterSlide masterSlide in pptxDoc.Masters)
{
foreach (ILayoutSlide layoutSlide in masterSlide.LayoutSlides)
{
//Iterate in layout slide.
IterateShapes(layoutSlide.Shapes, replacingFontName);
}
IterateShapes(masterSlide.Shapes, replacingFontName);
}
//Iterate in slides.
foreach (ISlide slide in pptxDoc.Slides)
{
//Iterate in layout slide.
IterateShapes(slide.LayoutSlide.Shapes, replacingFontName);
IterateShapes(slide.Shapes, replacingFontName);
}
}
- Use the following code snippet to iterate all shapes in each slide.
C#
/// <summary>
/// Iterate through each shapes in the slide.
/// </summary>
void IterateShapes(IShapes shapes, string replacingFontName)
{
//Iterate through each shape in the slide.
foreach (IShape shape in shapes)
{
//Identify the slide item type of the shape.
switch (shape.SlideItemType)
{
case SlideItemType.AutoShape:
//Replace font of the text content in textbody of the shape.
ReplaceSpecificFontInTextPart(shape.TextBody, replacingFontName);
break;
//Identify the table from the shape collection.
case SlideItemType.Table:
ITable table = (ITable)shape;
//Iterate through each row from the table.
foreach (IRow row in table.Rows)
{
//Iterate through each cell in the row.
foreach (ICell cell in row.Cells)
{
//Replace font of the text content in the textbody of the cell.
ReplaceSpecificFontInTextPart(cell.TextBody, replacingFontName);
}
}
break;
case SlideItemType.Placeholder:
ReplaceSpecificFontInTextPart(shape.TextBody, replacingFontName);
break;
case SlideItemType.GroupShape:
IGroupShape groupShape = (IGroupShape)shape;
//Iterate the shape collection in a group shape.
foreach (IShape childShape in groupShape.Shapes)
{
//Replace font of the text content in the textbody of the child shapes.
ReplaceSpecificFontInTextPart(childShape.TextBody, replacingFontName);
}
break;
}
}
}
- Use the following code snippet to change the font for all paragraphs and text parts.
C#
/// <summary>
/// Iterate through each textpart of the paragraph and set the font name.
/// </summary>
void ReplaceSpecificFontInTextPart(ITextBody textBody, string replacingFontName)
{
if (!string.IsNullOrEmpty(textBody.Text))
{
//Iterate through each paragraph of the textbody.
foreach (IParagraph paragraph in textBody.Paragraphs)
{
paragraph.EndParagraphFont.FontName = replacingFontName;
//Iterate through each textpart of the paragraph.
foreach (ITextPart textPart in paragraph.TextParts)
{
//Replace the font name.
textPart.Font.FontName = replacingFontName;
}
}
}
}
A complete working sample to change all font names in a Presentation file using C# can be downloaded from Github.
See here to learn more about working with charts in PowerPoint presentation. Take a moment to peruse the documentation, where you can find basic presentation document processing options along with features like clone and merge slides and encrypt and decrypt PowerPoint presentation and most importantly PDF and image conversion with code examples.
Explore more about the rich set of Syncfusion PowerPoint Framework features.
Conclusion
I hope you enjoyed learning how to change all font names in a PowerPoint using C# in ASP.NET Core.
You can refer to our .NET PowerPoint Library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!