翻譯|使用教程|編輯:李顯亮|2020-01-07 09:51:48.180|閱讀 1284 次
概述:接下來我們將進入關于“樣式處理”的介紹,在Aspose.Words中學會如何根據樣式提取內容。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words For .Net是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
接下來我們將進入關于“樣式處理”的介紹,在Aspose.Words中學會如何根據樣式提取內容。
>>Aspose.Words for .NET迎來2020第一次更新v20.1,支持插入LINQ Reporting Engine標簽,點擊下載體驗
如何根據樣式提取內容
簡單地說,從Word文檔中檢索基于樣式的內容對于識別、列出和計數段落以及使用特定樣式格式化的文本非常有用。例如,可能需要識別文檔中特定類型的內容,例如示例、標題、引用、關鍵字、圖名和案例研究。
同時,我們還可以用于利用由文檔使用的樣式定義的文檔結構,將文檔重新用于其他輸出,例如HTML。實際上,這就是Aspose文檔的構建方式,將Aspose.Words進行了測試。使用Aspose.Words構建的工具將獲取源Word文檔,并將其分為特定標題級別的主題。使用Aspose.Words生成XML文件,該文件用于構建左側顯示的導航樹。然后,Aspose.Words將每個主題轉換為HTML。
使用Aspose.Words檢索Word文檔中以特定樣式設置格式的文本的解決方案通常是經濟且直接的。為了說明Aspose.Words如何輕松處理基于樣式的內容,讓我們看一個示例。在此示例中,我們將從示例Word文檔中檢索具有特定段落樣式和字符樣式格式的文本,這將涉及以下內容:
具體來說,將從此示例Word文檔中檢索以“標題1”段落樣式和“強烈強調”字符樣式設置格式的文本。在此示例中,使用“標題1”段落樣式設置格式的文本是“插入標簽”,“快速樣式”和“主題”,使用“強烈強調”字體設置的文本是藍色的幾種實例,斜體,粗體文本,例如“畫廊”和“整體外觀”。
在Aspose中,基于樣式的查詢的實現非常簡單。word文檔對象模型,因為它只是使用了已經存在的工具。這個解決方案實現了兩個類方法:
這兩種方法非常相似,唯一的區別是節點類型和段落和運行節點中樣式信息的表示形式。下面是段落bystylename的一個實現:在下面的例子中找到所有使用指定格式的段落。
// 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; }
還需要指出的是,段落集合不會立即產生開銷,因為只有當訪問段落中的項目時,段落才會被加載到該集合中。然后,我們需要做的就是使用標準的foreach運算符瀏覽集合,并將具有指定樣式的段落添加到paragraphsWithStyle數組中。段落樣式名稱可以在Paragraph.ParagraphFormat 對象的Style.Name 屬性中找到。 盡管我們使用NodeType.Run 檢索運行節點,但RunsByStyleName的實現幾乎相同。Run 對象的Font.Style 屬性用于訪問運行節點。下面的示例查找所有以指定樣式設置格式的運行。
// 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; }
在實現這兩個查詢時,您所需要做的就是傳遞一個文檔對象并指定要檢索的內容的樣式名稱:下面的示例將運行查詢并顯示結果。
// 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);
最終結果
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn