翻譯|使用教程|編輯:李顯亮|2020-05-26 09:30:57.687|閱讀 1497 次
概述:Aspose.Words For .NET是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。在本文中,我們將學習段落處理相關知識。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words For .NET是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
>>Aspose.Words for .NET已經更新至v20.5,提供顯示/隱藏語法和拼寫錯誤的功能,引入了可在文檔內部使用水印的新幫助程序類,添加了為OOXML文檔設置壓縮級別的功能,點擊下載體驗
DocumentBuilder.Writeln 也可以在文檔中插入文本字符串,但除此之外,它還會添加一個段落分隔符。該DocumentBuilder.Font 屬性還指定當前字體格式,而該 屬性確定當前段落格式 DocumentBuilder.ParagraphFormat 。 下例顯示了如何將段落插入文檔。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); // Initialize document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting Font font = builder.Font; font.Size = 16; font.Bold = true; font.Color = System.Drawing.Color.Blue; font.Name = "Arial"; font.Underline = Underline.Dash; // Specify paragraph formatting ParagraphFormat paragraphFormat = builder.ParagraphFormat; paragraphFormat.FirstLineIndent = 8; paragraphFormat.Alignment = ParagraphAlignment.Justify; paragraphFormat.KeepTogether = true; builder.Writeln("A whole paragraph."); dataDir = dataDir + "DocumentBuilderInsertParagraph_out.doc"; doc.Save(dataDir);
如果要計算任何Word文檔的段落中的行數,則可以使用以下代碼示例。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); string fileName = "Properties.doc"; Document document = new Document(dataDir + fileName); var collector = new LayoutCollector(document); var it = new LayoutEnumerator(document); foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true)) { var paraBreak = collector.GetEntity(paragraph); object stop = null; var prevItem = paragraph.PreviousSibling; if (prevItem != null) { var prevBreak = collector.GetEntity(prevItem); if (prevItem is Paragraph) { it.Current = collector.GetEntity(prevItem); // para break it.MoveParent(); // last line stop = it.Current; } else if (prevItem is Table) { var table = (Table)prevItem; it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break it.MoveParent(); // cell it.MoveParent(); // row stop = it.Current; } else { throw new Exception(); } } it.Current = paraBreak; it.MoveParent(); // We move from line to line in a paragraph. // When paragraph spans multiple pages the we will follow across them. var count = 1; while (it.Current != stop) { if (!it.MovePreviousLogical()) break; count++; } const int MAX_CHARS = 16; var paraText = paragraph.GetText(); if (paraText.Length > MAX_CHARS) paraText = $"{paraText.Substring(0, MAX_CHARS)}..."; Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s)."); }
當前段落格式由DocumentBuilder.ParagraphFormat屬性返回的ParagraphFormat對象表示。該對象封裝了Microsoft Word中可用的各種段落格式設置屬性。您可以通過調用ParagraphFormat.ClearFormatting輕松將段落格式重置為默認樣式,即默認樣式為普通樣式,左對齊,無縮進,無間距,無邊框和無陰影 。下例顯示了如何設置段落格式。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set paragraph formatting properties ParagraphFormat paragraphFormat = builder.ParagraphFormat; paragraphFormat.Alignment = ParagraphAlignment.Center; paragraphFormat.LeftIndent = 50; paragraphFormat.RightIndent = 50; paragraphFormat.SpaceAfter = 25; // Output text builder.Writeln("I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping."); builder.Writeln("I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like."); dataDir = dataDir + "DocumentBuilderSetParagraphFormatting_out.doc"; doc.Save(dataDir);
一些格式對象(例如Font或ParagraphFormat)支持樣式。單個內置或用戶定義的樣式由Style對象表示,該對象包含相應的樣式屬性,例如名稱,基本樣式,樣式的字體和段落格式,等等。
此外, Style 對象提供Style.StyleIdentifier 屬性,該 屬性返回由Style.StyleIdentifier 枚舉值表示的與語言環境無關的樣式標識符 。關鍵是Microsoft Word中內置樣式的名稱針對不同的語言進行了本地化。使用樣式標識符,無論文檔語言是什么,都可以找到正確的樣式。枚舉值對應于Microsoft Word內置樣式,例如Normal,Heading 1,Heading 2等。所有用戶定義的樣式均分配有 StyleIdentifier.User值。下例顯示了如何應用段落樣式。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set paragraph style builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title; builder.Write("Hello"); dataDir = dataDir + "DocumentBuilderApplyParagraphStyle_out.doc"; doc.Save(dataDir);
一些格式對象(例如Font或ParagraphFormat)支持樣式。單個內置或用戶定義的樣式由Style對象表示,該對象包含相應的樣式屬性,例如名稱,基本樣式,樣式的字體和段落格式,等等。
可以使用Ctrl + Alt +在MS Word中輸入鍵盤快捷鍵將樣式分隔符添加到段落的末尾。此功能允許在一個邏輯打印段落中使用兩種不同的段落樣式。如果要使特定標題開頭的某些文本出現在目錄中,但又不想整個標題出現在目錄中,則可以使用此功能。下面的代碼示例演示如何插入樣式分隔符以放置不同的段落樣式。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Style paraStyle = builder.Document.Styles.Add(StyleType.Paragraph, "MyParaStyle"); paraStyle.Font.Bold = false; paraStyle.Font.Size = 8; paraStyle.Font.Name = "Arial"; // Append text with "Heading 1" style. builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; builder.Write("Heading 1"); builder.InsertStyleSeparator(); // Append text with another style. builder.ParagraphFormat.StyleName = paraStyle.Name; builder.Write("This is text with some other formatting "); dataDir = dataDir + "InsertStyleSeparator_out.doc"; // Save the document to disk. doc.Save(dataDir);
邊框由BorderCollection表示。這是按索引或按邊框類型訪問的Border對象的集合。邊框類型由BorderType枚舉表示。枚舉的某些值適用于多個或僅一個文檔元素。例如,BorderType.Bottom適用于段落或表格單元格,而BorderType.DiagonalDown僅指定表格單元格中的對角線邊框。
邊框集合和每個單獨的邊框都具有相似的屬性,例如顏色,線條樣式,線條寬度,距文本的距離以及可選的陰影。它們由相同名稱的屬性表示。您可以通過組合屬性值來實現不同的邊框類型。此外,BorderCollection和Border對象都允許您通過調用Border.ClearFormatting方法將這些值重置為默認值。請注意,當邊框屬性重置為默認值時,邊框是不可見的。該 著色 類包含文檔元素的材質屬性。您可以設置所需的明暗處理紋理以及應用于元素的背景和前景的顏色。
陰影紋理設置有 TextureIndex 枚舉值,該值允許將各種圖案應用到 Shading 對象。例如,要為文檔元素設置背景色,請使用TextureIndex.TextureSolid值并適當設置前景色。下例顯示了如何對段落應用邊框和陰影。下例顯示了如何對段落應用邊框和陰影。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set paragraph borders BorderCollection borders = builder.ParagraphFormat.Borders; borders.DistanceFromText = 20; borders[BorderType.Left].LineStyle = LineStyle.Double; borders[BorderType.Right].LineStyle = LineStyle.Double; borders[BorderType.Top].LineStyle = LineStyle.Double; borders[BorderType.Bottom].LineStyle = LineStyle.Double; // Set paragraph shading Shading shading = builder.ParagraphFormat.Shading; shading.Texture = TextureIndex.TextureDiagonalCross; shading.BackgroundPatternColor = System.Drawing.Color.LightCoral; shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon; builder.Write("I'm a formatted paragraph with double border and nice shading."); dataDir = dataDir + "DocumentBuilderApplyBordersAndShadingToParagraph_out.doc"; doc.Save(dataDir);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn