使用段落

段落是组合成逻辑块的一组字符,并以特殊字符(段落分隔符)结尾。在 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);

应用段落样式

某些格式化对象(例如 FontParagraphFormat)支持样式。一种内置或用户定义的样式由 Style 对象表示,该对象包含适当的样式属性,如名称、基本样式、字体、样式段落格式设置等。

此外,Style 对象公开 StyleIdentifier 属性,该属性返回由 StyleIdentifier 枚举值表示的与区域设置无关的样式标识符。事实上,Microsoft Word 中内置样式的名称已针对不同语言进行了本地化。使用样式标识符,无论文档语言如何,您都可以找到正确的样式。枚举值对应内置的 Microsoft Word 样式,例如 Normal标题 1标题 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 在 Paragraph 类上公开 BreakIsStyleSeparator 公共属性,以标识带有样式分隔符的段落,如下例所示:

// 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 仅指定表格单元格中的对角边框。

边框集合和每个单独的边框都具有相似的属性,例如颜色、线条样式、线条宽度、距文本的距离和可选的阴影。它们由同名的属性表示。您可以通过组合属性值来获得不同的边框类型。此外,BorderCollectionBorder 对象允许您通过调用 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).");
}