從 Word 文檔中刪除頁眉和頁腳
Aspose.Words是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
使用 Aspose.WordsAspose.Words 提供用于處理 Microsoft Word 文檔中的頁眉和頁腳的 API。我們可以使用Section.HeadersFooters對象來獲取文檔部分中頁眉/頁腳的集合。HeaderFooter類是節(jié)頁眉或頁腳文本容器。也就是說,HeaderFooter是一個節(jié)級節(jié)點,并且只能是節(jié)的子節(jié)點。一個節(jié)中只能有一個或每個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 參數(shù))。給定打開的文檔,代碼使用MainDocumentPart屬性導航到主文檔,并將引用存儲在名為 docPart 的變量中。
給定對文檔部分的引用,代碼接下來確定是否有任何工作要做,即文檔是否包含任何頁眉或頁腳。這個簡單的方法DeleteParts提供了刪除零件集合的快捷方式。
為了刪除擱淺的引用,代碼首先檢索HeaderReference元素的集合,將該集合轉換為 List,然后循環(huán)訪問該集合,為找到的每個元素調用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); } } } }
點擊復制