文檔首頁>>Aspose中文文檔>>從文檔中刪除分頁符
從文檔中刪除分頁符
Aspose.Words是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
使用Aspose.Words
在 Aspose.Words 中,使用ControlChar類的PageBreak字段查找所有分頁符。
以下代碼示例演示如何從文檔中刪除分頁符:
Document doc = new Document(MyDir + "Remove page breaks.docx"); // Retrieve all paragraphs in the document. NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true); foreach (Paragraph para in paragraphs) { // If the paragraph has a page break set before, then clear it. if (para.ParagraphFormat.PageBreakBefore) para.ParagraphFormat.PageBreakBefore = false; // Check all runs in the paragraph for page breaks and remove them. foreach (Run run in para.Runs) if (run.Text.Contains(ControlChar.PageBreak)) run.Text = run.Text.Replace(ControlChar.PageBreak, string.Empty); } doc.Save(ArtifactsDir + "Remove page breaks - Aspose.Words.docx");
點(diǎn)擊復(fù)制
使用 Open XML SDK
需要添加的命名空間:
using System.Collections.Generic; using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
點(diǎn)擊復(fù)制
以下代碼示例演示如何從文檔中刪除分頁符:
public void RemovePageBreaksFeature() { using (WordprocessingDocument myDoc = WordprocessingDocument.Open(MyDir + "Remove page breaks.docx", true)) { MainDocumentPart mainPart = myDoc.MainDocumentPart; List<Break> breaks = mainPart.Document.Descendants<Break>().ToList(); foreach (Break b in breaks) b.Remove(); using (Stream stream = File.Create(ArtifactsDir + "Remove page breaks - OpenXML.docx")) { mainPart.Document.Save(stream); } } }
點(diǎn)擊復(fù)制