在 PowerPoint 文档中添加、修改和移除页脚对于提升演示文稿的专业性和可读性至关重要。通过添加页脚,可以包含诸如演示文稿标题、作者、日期或页码等关键信息,有助于观众更好地理解演示内容和组织结构。修改页脚可以更新信息或个性化演示,使其更具吸引力和实用性。另外,移除页脚也是必要的,特别是在某些情况下,如打印演示时需要更简洁的布局或在特定场合不需要特定信息显示在每一页的底部。本文将介绍如何使用 Spire.Presentation for .NET 在 C# 项目中添加、修改和移除 Powerpoint 文档中的页脚。
安装 Spire.Presentation for .NET
首先,您需要添加 Spire.Presentation for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire. PresentationC# 添加 Powerpoint 文档中的页脚
Spire.Presentation 通过在 PowerPoint 文档的页面底部添加页脚占位符、页码占位符和时间占位符,实现在所有页面上统一添加相同的页脚内容。以下是详细的步骤:
- 创建一个 Presentation 对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 Powerpoint 文档。
- 使用 Presentation.FooterVisible = true 设置页脚可见,并设置下页脚文本。
- 使用 Presentation.SlideNumberVisible = true 设置幻灯片编号可见,并遍历每张幻灯片,检查是否存在页码占位符,如果是,则修改文本为 "第X页" 模式。
- 使用 Presentation.DateTimeVisible = true 设置日期时间可见。
- 使用 Presentation.SetDateTime() 方法设置日期的格式。
- 通过 Presentation.SaveToFile() 方法保存文档到指定路径。
- C#
using Spire.Presentation;
namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
             // 创建一个 Presentation 对象
            Presentation presentation = new Presentation();
            // 从文件加载演示文稿
            presentation.LoadFromFile("示例1.pptx");
            // 设置页脚可见
            presentation.FooterVisible = true;
            // 设置页脚文本为 "Spire.Presentation"
            presentation.SetFooterText("Spire.Presentation");
            // 设置幻灯片编号可见
            presentation.SlideNumberVisible = true;
            // 遍历演示文稿中的每一张幻灯片
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape.Placeholder != null)
                    {
                        // 如果是页码占位符
                        if (shape.Placeholder.Type.Equals(PlaceholderType.SlideNumber))
                        {
                            TextParagraph textParagraph = (shape as IAutoShape).TextFrame.TextRange.Paragraph;
                            String text = textParagraph.Text;
                            // 修改幻灯片编号文本为 "第X页"
                            textParagraph.Text = "第" + text + "页";
                        }
                    }
                }
            }
            // 设置日期时间可见
            presentation.DateTimeVisible = true;
            // 设置日期时间格式为 "yyyy年MM月dd日"
            presentation.SetDateTime(DateTime.Now, "yyyy年MM月dd日");
            // 将修改后的演示文稿保存到文件
            presentation.SaveToFile("添加页脚.pptx", FileFormat.Pptx2016);
            // 释放 Presentation 对象资源
            presentation.Dispose();
        }
    }
}
C# 修改 Powerpoint 文档中的页脚
要修改 PowerPoint 文档中的页脚,首先需要逐一检查每张幻灯片中的形状,接着识别页脚占位符、页码占位符等,最后针对每种占位符类型设定特定的内容和格式。以下是详细的步骤:
- 创建一个 Presentation 对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 Powerpoint 文档。
- 使用 Presentation.Slides[index] 属性获取一张幻灯片。
- 使用 for 循环遍历幻灯片中的形状,逐一检查每个形状是否为占位符,以确定是否为页脚、页码占位符等,然后修改下它的内容或格式。
- 通过 Presentation.SaveToFile() 方法保存文档到指定路径。
- C#
using Spire.Presentation;
namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
             // 创建一个Presentation对象
            Presentation presentation = new Presentation();
            // 从文件加载演示文稿
            presentation.LoadFromFile("示例2.pptx");
            // 获取第一页幻灯片
            ISlide slide = presentation.Slides[0];
            // 遍历幻灯片中的形状
            for (int i = 0; i < slide.Shapes.Count; i++)
            {
                // 检查形状是否为占位符
                if (slide.Shapes[i].Placeholder != null)
                {
                    // 获取占位符类型
                    PlaceholderType type = slide.Shapes[i].Placeholder.Type;
                    // 如果是页脚占位符
                    if (type == PlaceholderType.Footer)
                    {
                        // 将形状转换为IAutoShape类型
                        IAutoShape autoShape = (IAutoShape)slide.Shapes[i];
                        // 设置文本内容为"E-ICEBLUE"
                        autoShape.TextFrame.Text = "E-ICEBLUE";
                        // 修改文本的字体
                        ChangeFont(autoShape.TextFrame.Paragraphs[0]);
                    }
                    // 如果是页码占位符
                    if (type == PlaceholderType.SlideNumber)
                    {
                        // 将形状转换为IAutoShape类型
                        IAutoShape autoShape = (IAutoShape)slide.Shapes[i];
                        // 修改文本的字体
                        ChangeFont(autoShape.TextFrame.Paragraphs[0]);
                    }
                }
            }
            // 将修改后的演示文稿保存到文件
            presentation.SaveToFile("修改页脚.pptx", FileFormat.Pptx2016);
            // 释放Presentation对象资源
            presentation.Dispose();
        }
    static void ChangeFont(TextParagraph paragraph)
    {
            // 循环遍历段落中的每个文本域
            foreach (TextRange textRange in paragraph.TextRanges)
            {
                // 设置文本样式为斜体
                textRange.IsItalic = TriState.True;
                // 设置文本字体
                textRange.EastAsianFont = new TextFont("宋体");
                // 设置文本字号为12
                textRange.FontHeight = 34;
                // 设置文本的颜色
                textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                textRange.Fill.SolidColor.Color = System.Drawing.Color.LightSkyBlue;
            }
    }
  }  
}
C# 从 Powerpoint 文档中移除页脚
要删除掉 Powerpoint 文档中的页脚首先要检索到幻灯片中的页脚占位符、页码占位符、时间占位符等,然后从幻灯片的形状集合中将其移除。以下是详细的步骤:
- 创建一个 Presentation 对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 Powerpoint 文档。
- 使用 Presentation.Slides[index] 属性获取一张幻灯片。
- 使用 for 循环遍历幻灯片中的形状,检查是否为占位符,如果是页脚占位符、页码占位符、时间占位符,则从幻灯片中移除它。
- 通过 Presentation.SaveToFile() 方法保存文档到指定路径。
- C#
using Spire.Presentation;
namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个Presentation对象
            Presentation presentation = new Presentation();
            // 从文件加载演示文稿
            presentation.LoadFromFile("示例2.pptx");
            // 获取第一页幻灯片
            ISlide slide = presentation.Slides[0];
            // 遍历幻灯片中的形状
            for (int i = slide.Shapes.Count-1; i>=0; i--)
            {
                // 检查形状是否为占位符
                if (slide.Shapes[i].Placeholder != null)
                {
                    // 获取占位符类型
                    PlaceholderType type = slide.Shapes[i].Placeholder.Type;
                    // 如果是页脚占位符
                    if (type == PlaceholderType.Footer)
                    {
                        // 从幻灯片中移除它
                        slide.Shapes.RemoveAt(i);
                    }
                    // 如果是页码占位符
                    if (type == PlaceholderType.SlideNumber)
                    {
                        // 从幻灯片中移除它
                        slide.Shapes.RemoveAt(i);
                    }
                    // 如果是时间占位符
                    if (type == PlaceholderType.DateAndTime)
                    {
                        // 从幻灯片中移除它
                        slide.Shapes.RemoveAt(i);
                    }
                }
            }
            // 将修改后的演示文稿保存到文件
            presentation.SaveToFile("移除页脚.pptx", FileFormat.Pptx2016);
            // 释放Presentation对象资源
            presentation.Dispose();
        }
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



