與段落合作
段落是由組成一個邏輯ブロック並結束於特殊字符的文字組成的,這個特殊字符是 段落換行。 在 Aspose.Words 中,段落用 Paragraph 類別來表示。
插入一段
要將新段落插入到文件中,事實上你需要將段落斷字元插入到它。DocumentBuilder.Writeln 不僅將文本串插入到文件中,還會加入一段斷字元。
當前的字體格式是透過 Font 属性所指定,而當前的段落格式則是透過 ParagraphFormat 属性來決定的。 在下面的部分,我們將更詳細地說明段落格式化。
接下來這個程式碼範例展示了如何將一段落插入到文件中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// 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); |
格式段落
目前的段落格式化是透過 ParagraphFormat 物件來表示的,而這個物件是由 ParagraphFormat 屬性所回傳出來的。 此物件封裝了 Microsoft Word 中可用的各種段落格式設定屬性。 您可以輕鬆地將段落格式重置為預設格式–普通風格、左對齊、無缩进、无间距、无边框、无阴影,只需呼叫 ClearFormatting 即可。
以下範例程式碼示範如何設定段落格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
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物件公開 StyleIdentifier 屬性,其會返回由 StyleIdentifier 列舉值所表示的無地區設定風格識別碼。 事實上,內建樣式在 Microsoft Word 中的名稱是針對不同語言進行本地化的。 透過样式标识,你可以不管文件語言,找到正确的样式。 列舉值對應於內置的 Microsoft Word 樣式,例如 Normal、* Heading 1* 、 *Heading 2* 等。 所有由使用者定義的風格都設定於 **StyleIdentifier.User** 列舉值。
以下範例顯示如何應用段落格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
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); |
將風格分隔符插入以使用不同的段落風格
在 Microsoft Word 中可以使用鍵盤快捷方式 Ctrl+Alt+Enter 在段落結尾添加書式分隔符。 這個功能讓你可以在一個邏輯的印刷段落中用兩種不同的段落式。 如果你想要某個特定標題的開始部分在目錄中出現,但你不想讓整個標題都出現在目錄中,那你可以使用這個功能。
接下來的程式碼範例示範如何插入樣式分隔符,以適應不同的段落樣式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
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); |
識別段落風格分隔符
Aspose.Words 在 BreakIsStyleSeparator 公有屬性上暴露 Paragraph
類別,以辨識使用書式分隔符的段落,如下例所示:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_RenderingAndPrinting(); | |
// Initialize document. | |
string fileName = "TestFile.doc"; | |
Document doc = new Document(dataDir + fileName); | |
foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true)) | |
{ | |
if (paragraph.BreakIsStyleSeparator) | |
{ | |
Console.WriteLine("Separator Found!"); | |
} | |
} |
在段落上應用邊框和陰影
Aspose.Words 中的邊框由 BorderCollection 類別表示 – 這是一組 Border 物件,可以使用索引或邊框類型來存取。 邊界類型由 BorderType 列舉來表示。 有些枚舉值適用於多個或只有一個文檔元素。 例如,BorderType.Bottom適用於段落或資料表細胞,而BorderType.DiagonalDown僅指定資料表細胞中的對角線邊界。
邊界集和每個獨立的邊界都有類似的屬性,例如顏色、線條樣式、線條寬度、從文字的距離以及可選的陰影。 他們被同名的屬性所代表。 您可以透過結合屬性值來取得不同邊框類型。 此外,BorderCollection 和 Border 物件允許您透過呼叫 ClearFormatting 方法將這些值重設為預設值。
Aspose.Words 也有 Shading 類別,它包含文檔元素的陰影屬性。 您可以透過 TextureIndex 列舉值設定所需陰影紋理和應用在元素背景與前景的顏色。 TextureIndex 也讓您能將不同圖案應用在 Shading 物件上。 例如,若要設定文件元素的背景顏色,請使用 TextureIndex.TextureSolid 的值並設定前景遮蔽顏色。
接下來的程式碼範例展示了如何將邊框和陰影應用到段落上:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
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); |
計數段落行數
如果您想要在任何 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)."); | |
} |