Spire.Presentation支持修改图表的元素。 本文将详细介绍如何使用Spire.Presentation修改PPT文档中图表类别坐标轴以及图例的文本字体。
首先,我们看一下原始文档中图表图例文本的字体。
完整代码:
C#
Presentation presentation = new Presentation();
//加载文件
presentation.LoadFromFile(@"Sample.pptx", FileFormat.Pptx2013);
//获取图表
IChart chart = presentation.Slides[0].Shapes[0] as IChart;
//修改图例段落的填充方式
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
//修改图例段落的填充颜色
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.KnownColor = KnownColors.Red;
//修改图例段落字体大小
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 15;
//修改图例段落的字体
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("宋体");
//修改类别坐标轴段落的填充类型
chart.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
//修改类别坐标轴段落的填充颜色
chart.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.KnownColor = KnownColors.Blue;
//修改类别坐标轴段落字体大小
chart.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 18;
//修改类别坐标轴段落字体
chart.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("宋体");
//保存文件
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
VB.NET
Dim presentation As New Presentation()
'加载文件
presentation.LoadFromFile("Sample.pptx", FileFormat.Pptx2013)
'获取图表
Dim chart As IChart = TryCast(presentation.Slides(0).Shapes(0), IChart)
'修改图例段落的填充方式
chart.ChartLegend.TextProperties.Paragraphs(0).DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid
'修改图例段落的填充颜色
chart.ChartLegend.TextProperties.Paragraphs(0).DefaultCharacterProperties.Fill.SolidColor.KnownColor = KnownColors.Red
'修改图例段落字体大小
chart.ChartLegend.TextProperties.Paragraphs(0).DefaultCharacterProperties.FontHeight = 15
'修改图例段落的字体
chart.ChartLegend.TextProperties.Paragraphs(0).DefaultCharacterProperties.LatinFont = New TextFont("宋体")
'修改类别坐标轴段落的填充类型
chart.PrimaryCategoryAxis.TextProperties.Paragraphs(0).DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid
'修改类别坐标轴段落的填充颜色
chart.PrimaryCategoryAxis.TextProperties.Paragraphs(0).DefaultCharacterProperties.Fill.SolidColor.KnownColor = KnownColors.Blue
'修改类别坐标轴段落字体大小
chart.PrimaryCategoryAxis.TextProperties.Paragraphs(0).DefaultCharacterProperties.FontHeight = 18
'修改类别坐标轴段落字体
chart.PrimaryCategoryAxis.TextProperties.Paragraphs(0).DefaultCharacterProperties.LatinFont = New TextFont("宋体")
'保存文件
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010)
结果展示: