Word控件Spire.Doc 【页眉页脚】教程(11): 从C#中的word文档中删除页脚

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=N2N8https://www.evget.com/product/3368/download

word文档最多有3种页眉和页脚(首页、偶数页和奇数页),Spire.Doc支持在C#中插入页脚和页眉到word文档。在下面的示例中,我们将加载一个带有页眉和页脚的文档,我们将从所有部分中删除所有页脚,但保留页眉。

第 1 步:创建一个新的 word 文档实例并从文件中加载示例文档。

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

第 2 步:从文件中获取第一部分。

Section section = doc.Sections[0];

第 3 步:遍历word文档,清除所有不同类型的页脚。

foreach (Paragraph para in section.Paragraphs)
{
foreach (DocumentObject obj in para.ChildObjects)
{
HeaderFooter footer;
footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage];
if (footer != null)
footer.ChildObjects.Clear();

footer = section.HeadersFooters[HeaderFooterType.FooterOdd];
if (footer != null)
footer.ChildObjects.Clear();

footer = section.HeadersFooters[HeaderFooterType.FooterEven];
if (footer != null)
footer.ChildObjects.Clear();

}

第 4 步:将文档保存到文件。

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

删除页脚但保留页眉在 word 文档上的完整代码:

static void Main(string[] args)
{

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Section section = doc.Sections[0];

foreach (Paragraph para in section.Paragraphs)
{
foreach (DocumentObject obj in para.ChildObjects)
{
HeaderFooter footer;
footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage];
if (footer != null)
footer.ChildObjects.Clear();

footer = section.HeadersFooters[HeaderFooterType.FooterOdd];
if (footer != null)
footer.ChildObjects.Clear();

footer = section.HeadersFooters[HeaderFooterType.FooterEven];
if (footer != null)
footer.ChildObjects.Clear();

}

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

}

以上便如锁定标题以防止在 C# 中编辑 word 文档,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/130056440
今日推荐