翻譯|使用教程|編輯:李顯亮|2020-05-26 09:30:57.687|閱讀 1497 次
概述:Aspose.Words For .NET是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。在本文中,我們將學(xué)習(xí)段落處理相關(guān)知識(shí)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
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已經(jīng)更新至v20.5,提供顯示/隱藏語(yǔ)法和拼寫錯(cuò)誤的功能,引入了可在文檔內(nèi)部使用水印的新幫助程序類,添加了為OOXML文檔設(shè)置壓縮級(jí)別的功能,點(diǎn)擊下載體驗(yàn)
DocumentBuilder.Writeln 也可以在文檔中插入文本字符串,但除此之外,它還會(huì)添加一個(gè)段落分隔符。該DocumentBuilder.Font 屬性還指定當(dāng)前字體格式,而該 屬性確定當(dāng)前段落格式 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);
如果要計(jì)算任何Word文檔的段落中的行數(shù),則可以使用以下代碼示例。
// 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)."); }
當(dāng)前段落格式由DocumentBuilder.ParagraphFormat屬性返回的ParagraphFormat對(duì)象表示。該對(duì)象封裝了Microsoft Word中可用的各種段落格式設(shè)置屬性。您可以通過(guò)調(diào)用ParagraphFormat.ClearFormatting輕松將段落格式重置為默認(rèn)樣式,即默認(rèn)樣式為普通樣式,左對(duì)齊,無(wú)縮進(jìn),無(wú)間距,無(wú)邊框和無(wú)陰影 。下例顯示了如何設(shè)置段落格式。
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);
一些格式對(duì)象(例如Font或ParagraphFormat)支持樣式。單個(gè)內(nèi)置或用戶定義的樣式由Style對(duì)象表示,該對(duì)象包含相應(yīng)的樣式屬性,例如名稱,基本樣式,樣式的字體和段落格式,等等。
此外, Style 對(duì)象提供Style.StyleIdentifier 屬性,該 屬性返回由Style.StyleIdentifier 枚舉值表示的與語(yǔ)言環(huán)境無(wú)關(guān)的樣式標(biāo)識(shí)符 。關(guān)鍵是Microsoft Word中內(nèi)置樣式的名稱針對(duì)不同的語(yǔ)言進(jìn)行了本地化。使用樣式標(biāo)識(shí)符,無(wú)論文檔語(yǔ)言是什么,都可以找到正確的樣式。枚舉值對(duì)應(yīng)于Microsoft Word內(nèi)置樣式,例如Normal,Heading 1,Heading 2等。所有用戶定義的樣式均分配有 StyleIdentifier.User值。下例顯示了如何應(yīng)用段落樣式。
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);
一些格式對(duì)象(例如Font或ParagraphFormat)支持樣式。單個(gè)內(nèi)置或用戶定義的樣式由Style對(duì)象表示,該對(duì)象包含相應(yīng)的樣式屬性,例如名稱,基本樣式,樣式的字體和段落格式,等等。
可以使用Ctrl + Alt +在MS Word中輸入鍵盤快捷鍵將樣式分隔符添加到段落的末尾。此功能允許在一個(gè)邏輯打印段落中使用兩種不同的段落樣式。如果要使特定標(biāo)題開(kāi)頭的某些文本出現(xiàn)在目錄中,但又不想整個(gè)標(biāo)題出現(xiàn)在目錄中,則可以使用此功能。下面的代碼示例演示如何插入樣式分隔符以放置不同的段落樣式。
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表示。這是按索引或按邊框類型訪問(wèn)的Border對(duì)象的集合。邊框類型由BorderType枚舉表示。枚舉的某些值適用于多個(gè)或僅一個(gè)文檔元素。例如,BorderType.Bottom適用于段落或表格單元格,而B(niǎo)orderType.DiagonalDown僅指定表格單元格中的對(duì)角線邊框。
邊框集合和每個(gè)單獨(dú)的邊框都具有相似的屬性,例如顏色,線條樣式,線條寬度,距文本的距離以及可選的陰影。它們由相同名稱的屬性表示。您可以通過(guò)組合屬性值來(lái)實(shí)現(xiàn)不同的邊框類型。此外,BorderCollection和Border對(duì)象都允許您通過(guò)調(diào)用Border.ClearFormatting方法將這些值重置為默認(rèn)值。請(qǐng)注意,當(dāng)邊框?qū)傩灾刂脼槟J(rèn)值時(shí),邊框是不可見(jiàn)的。該 著色 類包含文檔元素的材質(zhì)屬性。您可以設(shè)置所需的明暗處理紋理以及應(yīng)用于元素的背景和前景的顏色。
陰影紋理設(shè)置有 TextureIndex 枚舉值,該值允許將各種圖案應(yīng)用到 Shading 對(duì)象。例如,要為文檔元素設(shè)置背景色,請(qǐng)使用TextureIndex.TextureSolid值并適當(dāng)設(shè)置前景色。下例顯示了如何對(duì)段落應(yīng)用邊框和陰影。下例顯示了如何對(duì)段落應(yīng)用邊框和陰影。
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);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn