翻譯|使用教程|編輯:李顯亮|2019-07-17 11:09:17.800|閱讀 930 次
概述:我們將進入“如何使用Aspose.Words以編程方式處理文檔分段”的介紹。在生成文檔時,使用section非常有用。您可以組合文檔,根據從多個模板文檔復制的多個部分構建輸出文檔,或者根據某些應用程序邏輯刪除不需要的部分,從而有效地將公共模板文檔過濾到特定場景。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words For .Net是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
接下來我們將進入“如何使用Aspose.Words以編程方式處理文檔分段”的介紹。在生成文檔時,使用section非常有用。您可以組合文檔,根據從多個模板文檔復制的多個部分構建輸出文檔,或者根據某些應用程序邏輯刪除不需要的部分,從而有效地將公共模板文檔過濾到特定場景。
文檔的各節由Section和SectionCollection類表示。Section對象是Document節點的直接子節點,可以通過Document.Sections屬性訪問。
每個分段由一個Section對象表示,該對象可以通過索引從Document.Sections集合中獲取。默認頁邊距、頁眉/頁腳距離和列間距取決于模擬MS Word行為的當前區域。例如,現在英語(美國)和英語(英國)的所有頁邊距都是1英寸。左,右,上邊距為2.5厘米; 德國底部邊距為2厘米。如果沒有為提及參數設置顯式值,則新默認值用于新文檔和加載文檔。
下面的代碼示例顯示了如何訪問指定索引處的節:
//指向documents目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document doc = new Document(dataDir + "Document.doc"); Section section = doc.Sections[0]; section.PageSetup.LeftMargin = 90; // 3.17 cm section.PageSetup.RightMargin = 90; // 3.17 cm section.PageSetup.TopMargin = 72; // 2.54 cm section.PageSetup.BottomMargin = 72; // 2.54 cm section.PageSetup.HeaderDistance = 35.4; // 1.25 cm section.PageSetup.FooterDistance = 35.4; // 1.25 cm section.PageSetup.TextColumns.Spacing = 35.4; // 1.25 cm
Document對象提供了可以使用Document.Sections訪問的節集合。這將返回包含文檔部分的SectionCollection對象。然后,您可以使用此對象上的SectionCollection.Add方法將一個節添加到文檔的末尾。下面的代碼示例顯示了如何將一個部分添加到文檔的末尾:
Document doc = new Document(dataDir); Section sectionToAdd = new Section(doc); doc.Sections.Add(sectionToAdd);
以與上面討論的相同方式,使用Document.Sections檢索文檔的部分。然后,可以使用SectionCollection.Remove刪除指定的節或SectionCollection.RemoveAt以刪除指定索引處的節。 下面的代碼示例顯示了如何刪除指定索引處的節:
Document doc = new Document(dataDir); doc.Sections.RemoveAt(0);
下面的代碼示例展示了如何從文檔中刪除所有部分:
Document doc = new Document(dataDir); doc.Sections.Clear();
如果要復制和插入除了節分隔符和節屬性之外的節的主要文本,請使用Section.PrependContent或Section.AppendContent為要復制的內容傳遞Section對象。如果沒有創建新的分段,頁眉和頁腳不會被復制。前一種方法在該部分的開頭插入內容的副本,而后者在該部分的末尾插入內容的副本。下面的代碼示例顯示了如何附加現有部分的內容:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document doc = new Document(dataDir + "Section.AppendContent.doc"); // This is the section that we will append and prepend to. Section section = doc.Sections[2]; //復制第1部分的內容并將其插入指定部分的開頭。 Section sectionToPrepend = doc.Sections[0]; section.PrependContent(sectionToPrepend); //復制第二部分的內容并將其插入指定部分的末尾。 Section sectionToAppend = doc.Sections[1]; section.AppendContent(sectionToAppend);
要刪除節的主要文本,請使用Section.ClearContent。要刪除節中的頁眉和頁腳,請調用Section.ClearHeadersFooters。下面的示例顯示了如何刪除節的主要內容:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document doc = new Document(dataDir + "Document.doc"); Section section = doc.Sections[0]; section.ClearContent();
使用Section.Clone方法創建特定節的副本。下面的示例顯示了如何創建特定部分的副本:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document doc = new Document(dataDir + "Document.doc"); Section cloneSection = doc.Sections[0].Clone();
將一個文檔完全或部分復制到另一個文檔是一項非常流行的任務 這是實現這一點的“模式”。在插入來自其他文檔的任何節點之前,必須使用Document.ImportNode方法導入該節點。該Document.ImportNode方法使原始節點的副本,并更新所有的內部文檔特定的屬性,如清單和樣式,使他們的目標文檔中有效。 下面的示例顯示了如何在文檔之間復制分段:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document srcDoc = new Document(dataDir + "Document.doc"); Document dstDoc = new Document(); Section sourceSection = srcDoc.Sections[0]; Section newSection = (Section)dstDoc.ImportNode(sourceSection, true); dstDoc.Sections.Add(newSection); dataDir = dataDir + "Document.Copy_out.doc"; dstDoc.Save(dataDir);
*想要獲取Aspose.Words正版授權可聯系哦~
ASPOSE技術交流QQ群已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn