翻譯|使用教程|編輯:胡濤|2022-06-14 11:33:04.427|閱讀 272 次
概述: 本文主要向大家介紹如何在 C# 中刪除分頁符,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在 Word 文檔中,用戶可以添加新的分頁符或刪除現有的分頁符。此示例顯示如何使用 Spire.Doc 從 word 文檔中刪除分頁符。Spire.Doc支持從.docx、.doc、RTF等格式的word文檔中去除分頁符。
首先確保Spire.Doc for .NET已正確安裝,然后通過以下路徑在下載的 Bin 文件夾中添加 Spire.Doc.dll 作為參考:“.. \Spire.Doc\Bin\NET4.0\ Spire.Doc .dll”。以下是如何在 C# 中刪除分頁符的詳細信息。
//Create a new word document and load from the file. Document document = new Document(); document.LoadFromFile("sample.docx"); // Traverse every paragraph of the first section of the document for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++) { Paragraph p = document.Sections[0].Paragraphs[j]; // Traverse every child object of a paragraph for (int i = 0; i < p.ChildObjects.Count; i++) { DocumentObject obj = p.ChildObjects[i]; //Find the page break object if (obj.DocumentObjectType == DocumentObjectType.Break) { Break b = obj as Break; // Remove the page break object from paragraph p.ChildObjects.Remove(b); //save the document to file. document.SaveToFile("result.docx");
請查看有效截圖:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; namespace RemovePageBreak { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("sample.docx", FileFormat.Docx); for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++) { Paragraph p = document.Sections[0].Paragraphs[j]; for (int i = 0; i < p.ChildObjects.Count; i++) { DocumentObject obj = p.ChildObjects[i]; if (obj.DocumentObjectType == DocumentObjectType.Break) { Break b = obj as Break; p.ChildObjects.Remove(b); } } } document.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx"); } } }
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn