Aspose.Words for .NET使用章節(jié)教程(2):如何處理文檔分段——Aspose.Words中的分段
Aspose.Words For .Net是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
接下來(lái)我們將進(jìn)入“如何使用Aspose.Words以編程方式處理文檔分段”的介紹。在生成文檔時(shí),使用section非常有用。您可以組合文檔,根據(jù)從多個(gè)模板文檔復(fù)制的多個(gè)部分構(gòu)建輸出文檔,或者根據(jù)某些應(yīng)用程序邏輯刪除不需要的部分,從而有效地將公共模板文檔過(guò)濾到特定場(chǎng)景。
Aspose.Words中的部分
文檔的各節(jié)由Section和SectionCollection類表示。Section對(duì)象是Document節(jié)點(diǎn)的直接子節(jié)點(diǎn),可以通過(guò)Document.Sections屬性訪問(wèn)。
▲獲得一分段
每個(gè)分段由一個(gè)Section對(duì)象表示,該對(duì)象可以通過(guò)索引從Document.Sections集合中獲取。默認(rèn)頁(yè)邊距、頁(yè)眉/頁(yè)腳距離和列間距取決于模擬MS Word行為的當(dāng)前區(qū)域。例如,現(xiàn)在英語(yǔ)(美國(guó))和英語(yǔ)(英國(guó))的所有頁(yè)邊距都是1英寸。左,右,上邊距為2.5厘米; 德國(guó)底部邊距為2厘米。如果沒(méi)有為提及參數(shù)設(shè)置顯式值,則新默認(rèn)值用于新文檔和加載文檔。
下面的代碼示例顯示了如何訪問(wèn)指定索引處的節(jié):
//指向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
▲添加一個(gè)分段
Document對(duì)象提供了可以使用Document.Sections訪問(wèn)的節(jié)集合。這將返回包含文檔部分的SectionCollection對(duì)象。然后,您可以使用此對(duì)象上的SectionCollection.Add方法將一個(gè)節(jié)添加到文檔的末尾。下面的代碼示例顯示了如何將一個(gè)部分添加到文檔的末尾:
Document doc = new Document(dataDir); Section sectionToAdd = new Section(doc); doc.Sections.Add(sectionToAdd);
▲刪除一個(gè)分段
以與上面討論的相同方式,使用Document.Sections檢索文檔的部分。然后,可以使用SectionCollection.Remove刪除指定的節(jié)或SectionCollection.RemoveAt以刪除指定索引處的節(jié)。 下面的代碼示例顯示了如何刪除指定索引處的節(jié):
Document doc = new Document(dataDir); doc.Sections.RemoveAt(0);
下面的代碼示例展示了如何從文檔中刪除所有部分:
Document doc = new Document(dataDir); doc.Sections.Clear();
▲添加分段內(nèi)容
如果要復(fù)制和插入除了節(jié)分隔符和節(jié)屬性之外的節(jié)的主要文本,請(qǐng)使用Section.PrependContent或Section.AppendContent為要復(fù)制的內(nèi)容傳遞Section對(duì)象。如果沒(méi)有創(chuàng)建新的分段,頁(yè)眉和頁(yè)腳不會(huì)被復(fù)制。前一種方法在該部分的開頭插入內(nèi)容的副本,而后者在該部分的末尾插入內(nèi)容的副本。下面的代碼示例顯示了如何附加現(xiàn)有部分的內(nèi)容:
//文檔目錄的路徑。 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]; //復(fù)制第1部分的內(nèi)容并將其插入指定部分的開頭。 Section sectionToPrepend = doc.Sections[0]; section.PrependContent(sectionToPrepend); //復(fù)制第二部分的內(nèi)容并將其插入指定部分的末尾。 Section sectionToAppend = doc.Sections[1]; section.AppendContent(sectionToAppend);
▲刪除分段內(nèi)容
要?jiǎng)h除節(jié)的主要文本,請(qǐng)使用Section.ClearContent。要?jiǎng)h除節(jié)中的頁(yè)眉和頁(yè)腳,請(qǐng)調(diào)用Section.ClearHeadersFooters。下面的示例顯示了如何刪除節(jié)的主要內(nèi)容:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document doc = new Document(dataDir + "Document.doc"); Section section = doc.Sections[0]; section.ClearContent();
▲克隆一分段
使用Section.Clone方法創(chuàng)建特定節(jié)的副本。下面的示例顯示了如何創(chuàng)建特定部分的副本:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithSections(); Document doc = new Document(dataDir + "Document.doc"); Section cloneSection = doc.Sections[0].Clone();
▲在文檔之間復(fù)制分段
將一個(gè)文檔完全或部分復(fù)制到另一個(gè)文檔是一項(xiàng)非常流行的任務(wù) 這是實(shí)現(xiàn)這一點(diǎn)的“模式”。在插入來(lái)自其他文檔的任何節(jié)點(diǎn)之前,必須使用Document.ImportNode方法導(dǎo)入該節(jié)點(diǎn)。該Document.ImportNode方法使原始節(jié)點(diǎn)的副本,并更新所有的內(nèi)部文檔特定的屬性,如清單和樣式,使他們的目標(biāo)文檔中有效。 下面的示例顯示了如何在文檔之間復(fù)制分段:
//文檔目錄的路徑。 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正版授權(quán)可聯(lián)系哦~
ASPOSE技術(shù)交流QQ群已開通,各類資源及時(shí)分享,歡迎交流討論!(掃描下方二維碼加入群聊)