从字处理文档中移除页眉和页脚
本主题演示如何使用 Open XML SDK for Office 中的类以编程方式删除字处理文档中的所有页眉和页脚。 它包含演示此任务的示例 RemoveHeadersAndFooters
方法。
RemoveHeadersAndFooters 方法
可以使用 RemoveHeadersAndFooters
方法从字处理文档中删除所有页眉和页脚信息。 请注意,您不仅必须从文档存储中删除页眉和页脚部分,还必须从文档中删除对这些部分的引用。 该示例代码演示了操作中的两个步骤。 方法 RemoveHeadersAndFooters
接受单个参数,这是一个字符串,指示要修改的文件的路径。
static void RemoveHeadersAndFooters(string filename)
该方法的完整代码列表位于示例代码部分。
调用示例方法
若要调用示例方法,请传递一个字符串表示第一个参数,其中包含要修改的文档的文件名,如下面的代码所示。
string filename = args[0];
RemoveHeadersAndFooters(filename);
代码的工作方式
方法 RemoveHeadersAndFooters
适用于指定的文档,删除所有页眉和页脚部分以及对这些部分的引用。 代码首先使用 Open 方法打开文档,并指示应打开文档以 (最终 true 参数) 进行读/写访问。 给定打开的文档,代码使用 MainDocumentPart 属性导航到main文档,并将引用存储在名为 的docPart
变量中。
// Given a document name, remove all of the headers and footers
// from the document.
using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
{
if (doc.MainDocumentPart is null)
{
throw new ArgumentNullException("MainDocumentPart is null.");
}
// Get a reference to the main document part.
var docPart = doc.MainDocumentPart;
确认页眉/页脚是否存在
给定对文档部件的引用,接下来的代码将确定它是否有任何工作要做,即文档是否包含任何页眉或页脚。 为了决定,代码调用 Count
文档部件的 HeaderParts 和 FooterParts 属性的 方法,如果任一返回大于 0 的值,代码将继续。
请注意, HeaderParts
和 FooterParts
属性分别返回 IEnumerable<T> 或 HeaderPartFooterPart 对象的 。
// Count the header and footer parts and continue if there
// are any.
if (docPart.HeaderParts.Count() > 0 || docPart.FooterParts.Count() > 0)
{
移除页眉和页脚部分
给定对页眉和页脚部分的引用集合,可以编写代码来单独删除每个部分,但由于 Open XML SDK,这不是必需的。 相反,可以调用 DeleteParts 方法,传入要删除的部件的集合。此简单方法提供了删除部件集合的快捷方式。 因此,以下几行代码取代了循环,否则必须自行编写。
// Remove the header and footer parts.
docPart.DeleteParts(docPart.HeaderParts);
docPart.DeleteParts(docPart.FooterParts);
删除页眉和页脚引用
此时,该代码已删除页眉和页脚部分,但文档仍包含对这些部分的孤立引用。 代码必须检索对文档内容的引用(即对主文档部分中包含的 XML 内容的引用)才能移除孤立引用。
为了删除搁浅的引用,代码首先检索 HeaderReference 元素的集合,将该集合转换为 List
,然后循环访问该集合,为找到的每个元素调用 Remove() 方法。 请注意,代码将 方法返回Descendants()的 转换为 IEnumerable
,List
以便它可以从列表中删除项,并且 HeaderReference Open XML SDK 提供的类型使在 XML 内容中引用类型的HeaderReference
元素变得容易。 (如果没有其他帮助,则必须直接处理 XML 内容的详细信息。) 删除所有标头后,代码将使用页脚元素重复此操作。
// Get a reference to the root element of the main
// document part.
Document document = docPart.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();
}
示例代码
下面是 C# 和 Visual Basic 中的完整 RemoveHeadersAndFooters
代码示例。
// Remove all of the headers and footers from a document.
static void RemoveHeadersAndFooters(string filename)
{
// Given a document name, remove all of the headers and footers
// from the document.
using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
{
if (doc.MainDocumentPart is null)
{
throw new ArgumentNullException("MainDocumentPart is null.");
}
// Get a reference to the main document part.
var docPart = doc.MainDocumentPart;
// Count the header and footer parts and continue if there
// are any.
if (docPart.HeaderParts.Count() > 0 || docPart.FooterParts.Count() > 0)
{
// Remove the header and footer parts.
docPart.DeleteParts(docPart.HeaderParts);
docPart.DeleteParts(docPart.FooterParts);
// Get a reference to the root element of the main
// document part.
Document document = docPart.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();
}
}
}
}