在 powerpoint 文档中添加、修改和删除页脚,可以帮助整理演示文稿中的相关信息。页脚通常用于显示标题、作者、日期或页码等内容,也可以根据实际需求进行修改或移除,使页面更加简洁。
本文将介绍如何在 c# 中对 powerpoint 文档中的页脚进行添加、修改和删除操作,并演示相关实现方法。
安装相关组件
首先,将项目所需的 dll 文件添加为 .net 项目的引用。相关文件可以从对应的安装包中获取,也可以通过 nuget 进行安装。
pm> install-package spire.presentation
使用 c# 在 powerpoint 文档中添加页脚
可以通过设置演示文稿的相关属性,在 powerpoint 页面底部添加统一的页脚、页码和日期信息。具体步骤如下:
- 创建一个 presentation 对象。
- 使用 loadfromfile() 方法加载 powerpoint 文档。
- 将 footervisible 设置为
true,显示页脚并设置页脚文本。 - 将 slidenumbervisible 设置为
true,显示幻灯片编号,然后遍历每张幻灯片,查找页码占位符,并将其修改为 “slide x” 格式。 - 将 datetimevisible 设置为
true,显示日期和时间。 - 使用 setdatetime() 方法设置日期格式。
- 使用 savetofile() 方法保存修改后的文档。
完整示例代码如下:
using spire.presentation;
namespace spirepresentationdemo
{
internal class program
{
static void main(string[] args)
{
// 创建 presentation 对象
presentation presentation = new presentation();
// 从文件加载演示文稿
presentation.loadfromfile("sample1.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;
// 将幻灯片编号修改为 "slide x" 格式
textparagraph.text = "slide " + text;
}
}
}
}
// 设置显示日期和时间
presentation.datetimevisible = true;
// 设置日期和时间格式
presentation.setdatetime(datetime.now, "mm/dd/yyyy");
// 将修改后的演示文稿保存到文件
presentation.savetofile("addfooter.pptx", fileformat.pptx2016);
// 释放 presentation 对象资源
presentation.dispose();
}
}
}使用 c# 修改 powerpoint 文档中的页脚
修改 powerpoint 文档中的页脚时,需要逐一检查每张幻灯片中的形状,识别页脚、页码等占位符,然后根据需要调整对应的内容或格式。具体步骤如下:
- 创建一个 presentation 对象。
- 使用 loadfromfile() 方法加载 powerpoint 文档。
- 使用 slides[index] 属性获取指定幻灯片。
- 使用 for 循环遍历幻灯片中的各个形状,判断其是否为页脚、页码等占位符,并根据需要修改相应的内容或格式。
- 使用 savetofile() 方法保存修改后的文档。
完整示例代码如下:
using spire.presentation;
namespace spirepresentationdemo
{
internal class program
{
static void main(string[] args)
{
// 创建 presentation 对象
presentation presentation = new presentation();
// 从文件加载演示文稿
presentation.loadfromfile("sample2.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";
// 修改文本字体
changefont1(autoshape.textframe.paragraphs[0]);
}
// 如果是幻灯片编号占位符
if (type == placeholdertype.slidenumber)
{
// 将形状转换为 iautoshape 类型
iautoshape autoshape = (iautoshape)slide.shapes[i];
// 修改文本字体
changefont1(autoshape.textframe.paragraphs[0]);
}
}
}
// 将修改后的演示文稿保存到文件
presentation.savetofile("modifyfooter.pptx", fileformat.pptx2016);
// 释放 presentation 对象资源
presentation.dispose();
}
static void changefont1(textparagraph paragraph)
{
// 遍历段落中的每个文本范围
foreach (textrange textrange in paragraph.textranges)
{
// 设置文本样式为斜体
textrange.isitalic = tristate.true;
// 设置文本字体
textrange.eastasianfont = new textfont("times new roman");
// 设置文本字号为 34
textrange.fontheight = 34;
// 设置文本颜色
textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
textrange.fill.solidcolor.color = system.drawing.color.lightskyblue;
}
}
}
}使用 c# 删除 powerpoint 文档中的页脚
删除 powerpoint 文档中的页脚时,可以先在幻灯片中查找页脚、页码和日期等占位符,再将不需要的占位符从幻灯片中移除。具体步骤如下:
- 创建一个 presentation 对象。
- 使用 loadfromfile() 方法加载 powerpoint 文档。
- 使用 slides[index] 属性获取指定幻灯片。
- 使用 for 循环遍历幻灯片中的形状,检查其是否为占位符。如果属于页脚、页码或日期占位符,则将其从幻灯片中移除。
- 使用 savetofile() 方法保存修改后的文档。
完整示例代码如下:
using spire.presentation;
namespace spirepresentationdemo
{
internal class program
{
static void main(string[] args)
{
// 创建一个 presentation 对象
presentation presentation = new presentation();
// 从文件加载演示文稿
presentation.loadfromfile("sample2.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 || type == placeholdertype.slidenumber || type == placeholdertype.dateandtime)
{
// 从幻灯片中移除该占位符
slide.shapes.removeat(i);
}
}
}
// 将修改后的演示文稿保存到文件
presentation.savetofile("removefooter.pptx", fileformat.pptx2016);
// 释放 presentation 对象占用的资源
presentation.dispose();
}
}
}总结
本文介绍了如何使用 spire.presentation for .net 删除 powerpoint 演示文稿中的页脚、幻灯片编号以及日期和时间信息。通过加载演示文稿并遍历幻灯片中的形状,可以检查各个形状是否为占位符,并根据占位符类型定位需要删除的内容。最后,将修改后的演示文稿保存为新的 powerpoint 文件。该方法操作简单,适用于需要批量清理或自定义 powerpoint 页面信息的场景。
到此这篇关于c#代码实现在powerpoint文档中添加、修改或删除页脚的文章就介绍到这了,更多相关c#操作powerpoint页脚内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论