Aspose.Words for .NET樣式處理教程——如何根據(jù)樣式提取內(nèi)容
Aspose.Words For .Net是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
接下來我們將進入關(guān)于“樣式處理”的介紹,在Aspose.Words中學會如何根據(jù)樣式提取內(nèi)容。
>>Aspose.Words for .NET迎來2020第一次更新v20.1,支持插入LINQ Reporting Engine標簽,點擊下載體驗
如何根據(jù)樣式提取內(nèi)容
簡單地說,從Word文檔中檢索基于樣式的內(nèi)容對于識別、列出和計數(shù)段落以及使用特定樣式格式化的文本非常有用。例如,可能需要識別文檔中特定類型的內(nèi)容,例如示例、標題、引用、關(guān)鍵字、圖名和案例研究。
同時,我們還可以用于利用由文檔使用的樣式定義的文檔結(jié)構(gòu),將文檔重新用于其他輸出,例如HTML。實際上,這就是Aspose文檔的構(gòu)建方式,將Aspose.Words進行了測試。使用Aspose.Words構(gòu)建的工具將獲取源Word文檔,并將其分為特定標題級別的主題。使用Aspose.Words生成XML文件,該文件用于構(gòu)建左側(cè)顯示的導(dǎo)航樹。然后,Aspose.Words將每個主題轉(zhuǎn)換為HTML。
使用Aspose.Words檢索Word文檔中以特定樣式設(shè)置格式的文本的解決方案通常是經(jīng)濟且直接的。為了說明Aspose.Words如何輕松處理基于樣式的內(nèi)容,讓我們看一個示例。在此示例中,我們將從示例Word文檔中檢索具有特定段落樣式和字符樣式格式的文本,這將涉及以下內(nèi)容:
- 使用Document類打開Word文檔。文檔中的所有段落和所有運行。
- 僅選擇所需的段落和運行。
具體來說,將從此示例Word文檔中檢索以“標題1”段落樣式和“強烈強調(diào)”字符樣式設(shè)置格式的文本。在此示例中,使用“標題1”段落樣式設(shè)置格式的文本是“插入標簽”,“快速樣式”和“主題”,使用“強烈強調(diào)”字體設(shè)置的文本是藍色的幾種實例,斜體,粗體文本,例如“畫廊”和“整體外觀”。
在Aspose中,基于樣式的查詢的實現(xiàn)非常簡單。word文檔對象模型,因為它只是使用了已經(jīng)存在的工具。這個解決方案實現(xiàn)了兩個類方法:
- hsbystylename——這個方法檢索文檔中具有特定樣式名稱的段落的數(shù)組。
- RunsByStyleName—此方法檢索文檔中具有特定樣式名稱的運行的數(shù)組。
這兩種方法非常相似,唯一的區(qū)別是節(jié)點類型和段落和運行節(jié)點中樣式信息的表示形式。下面是段落bystylename的一個實現(xiàn):在下面的例子中找到所有使用指定格式的段落。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET public static ArrayList ParagraphsByStyleName(Document doc, string styleName) { // Create an array to collect paragraphs of the specified style. ArrayList paragraphsWithStyle = new ArrayList(); // Get all paragraphs from the document. NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true); // Look through all paragraphs to find those with the specified style. foreach (Paragraph paragraph in paragraphs) { if (paragraph.ParagraphFormat.Style.Name == styleName) paragraphsWithStyle.Add(paragraph); } return paragraphsWithStyle; }
還需要指出的是,段落集合不會立即產(chǎn)生開銷,因為只有當訪問段落中的項目時,段落才會被加載到該集合中。然后,我們需要做的就是使用標準的foreach運算符瀏覽集合,并將具有指定樣式的段落添加到paragraphsWithStyle數(shù)組中。段落樣式名稱可以在Paragraph.ParagraphFormat 對象的Style.Name 屬性中找到。 盡管我們使用NodeType.Run 檢索運行節(jié)點,但RunsByStyleName的實現(xiàn)幾乎相同。Run 對象的Font.Style 屬性用于訪問運行節(jié)點。下面的示例查找所有以指定樣式設(shè)置格式的運行。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET public static ArrayList RunsByStyleName(Document doc, string styleName) { // Create an array to collect runs of the specified style. ArrayList runsWithStyle = new ArrayList(); // Get all runs from the document. NodeCollection runs = doc.GetChildNodes(NodeType.Run, true); // Look through all runs to find those with the specified style. foreach (Run run in runs) { if (run.Font.Style.Name == styleName) runsWithStyle.Add(run); } return runsWithStyle; }
在實現(xiàn)這兩個查詢時,您所需要做的就是傳遞一個文檔對象并指定要檢索的內(nèi)容的樣式名稱:下面的示例將運行查詢并顯示結(jié)果。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithStyles(); string fileName = "TestFile.doc"; // Open the document. Document doc = new Document(dataDir + fileName); // Define style names as they are specified in the Word document. const string paraStyle = "Heading 1"; const string runStyle = "Intense Emphasis"; // Collect paragraphs with defined styles. // Show the number of collected paragraphs and display the text of this paragraphs. ArrayList paragraphs = ParagraphsByStyleName(doc, paraStyle); Console.WriteLine(string.Format("Paragraphs with \"{0}\" styles ({1}):", paraStyle, paragraphs.Count)); foreach (Paragraph paragraph in paragraphs) Console.Write(paragraph.ToString(SaveFormat.Text)); // Collect runs with defined styles. // Show the number of collected runs and display the text of this runs. ArrayList runs = RunsByStyleName(doc, runStyle); Console.WriteLine(string.Format("\nRuns with \"{0}\" styles ({1}):", runStyle, runs.Count)); foreach (Run run in runs) Console.WriteLine(run.Range.Text);
最終結(jié)果
還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時,我們很高興為您提供查詢和咨詢。