从 Word 文档中删除页眉和页脚
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
使用 Aspose.WordsAspose.Words 提供用于处理 Microsoft Word 文档中的页眉和页脚的 API。我们可以使用Section.HeadersFooters对象来获取文档部分中页眉/页脚的集合。HeaderFooter类是节页眉或页脚文本容器。也就是说,HeaderFooter是一个节级节点,并且只能是节的子节点。一个节中只能有一个或每个HeaderFooterType的HeaderFooter。
以下代码示例演示如何从 Word 文档中删除页眉和页脚:
Document doc = new Document(MyDir + "Document.docx"); foreach (Section section in doc) { section.HeadersFooters.RemoveAt(0); // Odd pages use the primary footer. HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterPrimary]; footer?.Remove(); } doc.Save(ArtifactsDir + "Remove header and footer - Aspose.Words.docx");
点击复制
要使用该代码示例,您必须安装 Open XML SDK 2.5。然后您必须在项目中显式引用以下程序集:
- WindowsBase
- DocumentFormat.OpenXml(由 Open XML SDK 安装)
需要使用的命名空间:
using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
点击复制
RemoveHeadersAndFooters方法适用于您指定的文档,删除所有页眉和页脚部分以及对这些部分的引用。该代码首先使用 Open 方法打开文档,并指示应打开文档以进行读/写访问(最后一个 true 参数)。给定打开的文档,代码使用MainDocumentPart属性导航到主文档,并将引用存储在名为 docPart 的变量中。
给定对文档部分的引用,代码接下来确定是否有任何工作要做,即文档是否包含任何页眉或页脚。这个简单的方法DeleteParts提供了删除零件集合的快捷方式。
为了删除搁浅的引用,代码首先检索HeaderReference元素的集合,将该集合转换为 List,然后循环访问该集合,为找到的每个元素调用Remove方法。
以下代码示例演示如何从 Word 文档中删除页眉和页脚:
public void RemoveHeaderFooterFeature() { using (WordprocessingDocument doc = WordprocessingDocument.Open(MyDir + "Document.docx", true)) { var mainDocumentPart = doc.MainDocumentPart; // Count the header and footer parts and continue if there are any. if (mainDocumentPart.HeaderParts.Any() || mainDocumentPart.FooterParts.Any()) { // Remove the header and footer parts. mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts); mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts); // Get a reference to the root element of the main document part. Document document = mainDocumentPart.Document; // Remove all references to the headers and footers. // First, create a list of all descendants of type HeaderReference. // Then, navigate the list and call remove on each item to delete the reference. var headers = document.Descendants<HeaderReference>().ToList(); foreach (var header in headers) header.Remove(); // First, create a list of all descendants of type FooterReference. // Then, navigate the list and call remove on each item to delete the reference. var footers = document.Descendants<FooterReference>().ToList(); foreach (var footer in footers) footer.Remove(); using (Stream stream = File.Create(ArtifactsDir + "Remove header and footer - OpenXML.docx")) { document.Save(stream); } } } } public void RemoveHeaderFooterFeature() { using (WordprocessingDocument doc = WordprocessingDocument.Open(MyDir + "Document.docx", true)) { var mainDocumentPart = doc.MainDocumentPart; // Count the header and footer parts and continue if there are any. if (mainDocumentPart.HeaderParts.Any() || mainDocumentPart.FooterParts.Any()) { // Remove the header and footer parts. mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts); mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts); // Get a reference to the root element of the main document part. Document document = mainDocumentPart.Document; // Remove all references to the headers and footers. // First, create a list of all descendants of type HeaderReference. // Then, navigate the list and call remove on each item to delete the reference. var headers = document.Descendants<HeaderReference>().ToList(); foreach (var header in headers) header.Remove(); // First, create a list of all descendants of type FooterReference. // Then, navigate the list and call remove on each item to delete the reference. var footers = document.Descendants<FooterReference>().ToList(); foreach (var footer in footers) footer.Remove(); using (Stream stream = File.Create(ArtifactsDir + "Remove header and footer - OpenXML.docx")) { document.Save(stream); } } } }
点击复制