处理段落
段落是一组字符组合成一个逻辑块,并以一个特殊字符–aparagraph break结尾。 在Aspose.Words中,段落由Paragraph类表示。
插入一段
要在文档中插入一个新的段落,实际上,您需要在其中插入一个段落中断字符。 DocumentBuilder.Writeln不仅在文档中插入一串文本,而且还添加一个段落中断。
当前字体格式也由Font属性指定,当前段落格式由ParagraphFormat属性确定。 在下一节中,我们将详细介绍段落格式。
下面的代码示例演示如何将段落插入到文档中:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument(); | |
// Initialize document. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Specify font formatting | |
System::SharedPtr<Font> font = builder->get_Font(); | |
font->set_Size(16); | |
font->set_Bold(true); | |
font->set_Color(System::Drawing::Color::get_Blue()); | |
font->set_Name(u"Arial"); | |
font->set_Underline(Underline::Dash); | |
// Specify paragraph formatting | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_FirstLineIndent(8); | |
paragraphFormat->set_Alignment(ParagraphAlignment::Justify); | |
paragraphFormat->set_KeepTogether(true); | |
builder->Writeln(u"A whole paragraph."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderInsertParagraph.doc"; | |
doc->Save(outputPath); |
格式段落
当前段落格式由ParagraphFormat属性返回的ParagraphFormat对象表示。 此对象封装了Microsoft Word中可用的各种段落格式属性。 您可以通过调用ClearFormatting轻松地将段落格式重置为默认为正常样式,左对齐无缩进,无间距,无边框和无阴影。
下面的代码示例演示如何设置段落格式:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph formatting properties | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_Alignment(ParagraphAlignment::Center); | |
paragraphFormat->set_LeftIndent(50); | |
paragraphFormat->set_RightIndent(50); | |
paragraphFormat->set_SpaceAfter(25); | |
// Output text | |
builder->Writeln(u"I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping."); | |
builder->Writeln(u"I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetParagraphFormatting.doc"; | |
doc->Save(outputPath); |
应用段落样式
一些格式化对象(如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-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph style | |
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Title); | |
builder->Write(u"Hello"); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.ApplyParagraphStyle.doc"; | |
doc->Save(outputPath); |
插入样式分隔符以放置不同的段落样式
可以使用Ctrl+Alt+Enter键盘快捷键将样式分隔符添加到段落的末尾MS字。 此功能允许在一个逻辑打印段落中使用两种不同的段落样式。 如果您希望某个特定标题开头的某些文本出现在内容列表中,但不希望整个标题出现在内容列表中,则可以使用此功能。
下面的代码示例演示如何插入样式分隔符以适应不同的段落样式:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Style> paraStyle = builder->get_Document()->get_Styles()->Add(StyleType::Paragraph, u"MyParaStyle"); | |
paraStyle->get_Font()->set_Bold(false); | |
paraStyle->get_Font()->set_Size(8); | |
paraStyle->get_Font()->set_Name(u"Arial"); | |
// Append text with "Heading 1" style. | |
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Heading1); | |
builder->Write(u"Heading 1"); | |
builder->InsertStyleSeparator(); | |
// Append text with another style. | |
builder->get_ParagraphFormat()->set_StyleName(paraStyle->get_Name()); | |
builder->Write(u"This is text with some other formatting "); | |
System::String outputPath = outputDataDir + u"InsertStyleSeparator.doc"; | |
// Save the document to disk. | |
doc->Save(outputPath); |
识别段落样式分隔符
Aspose.Words在Paragraph
类中提供了一个公共属性BreakIsStyleSeparator,允许标识样式分隔符段落,如下面的示例所示:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String inputDataDir = GetInputDataDir_RenderingAndPrinting(); | |
// Initialize document. | |
System::String fileName = u"TestFile.doc"; | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + fileName); | |
for (System::SharedPtr<Paragraph> paragraph : System::IterateOver<Paragraph>(doc->GetChildNodes(NodeType::Paragraph, true))) | |
{ | |
if (paragraph->get_BreakIsStyleSeparator()) | |
{ | |
std::cout << "Separator Found!" << std::endl; | |
} | |
} |
对段落应用边框和阴影
Aspose.Words中的边框由BorderCollection类表示-这是通过索引或边框类型访问的Border对象的集合。 边框类型由BorderType
枚举表示。 枚举的某些值适用于多个或仅一个文档元素。 例如,BorderType.Bottom适用于段落或表格单元格,而BorderType.DiagonalDown仅指定表格单元格中的对角线边框。
边框集合和每个单独的边框都具有类似的属性,如颜色,线条样式,线条宽度,与文本的距离和可选阴影。 它们由同名属性表示。 您可以通过组合属性值来实现不同的边框类型。 此外,BorderCollection和Border对象都允许您通过调用ClearFormatting方法将这些值重置为默认值。
Aspose.Words也有Shading类包含文档元素的着色属性。 您可以设置所需的着色纹理以及应用于元素背景和前景的颜色。
着色纹理使用TextureIndex枚举值设置,该值允许将各种模式应用于Shading对象。 例如,要为文档元素设置背景颜色,请使用TextureIndex.TextureSolid值并根据需要设置前景阴影颜色。
下面的代码示例演示如何将边框和底纹应用于段落:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph borders | |
System::SharedPtr<BorderCollection> borders = builder->get_ParagraphFormat()->get_Borders(); | |
borders->set_DistanceFromText(20); | |
borders->idx_get(BorderType::Left)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Right)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Top)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Bottom)->set_LineStyle(LineStyle::Double); | |
// Set paragraph shading | |
System::SharedPtr<Shading> shading = builder->get_ParagraphFormat()->get_Shading(); | |
shading->set_Texture(TextureIndex::TextureDiagonalCross); | |
shading->set_BackgroundPatternColor(System::Drawing::Color::get_LightCoral()); | |
shading->set_ForegroundPatternColor(System::Drawing::Color::get_LightSalmon()); | |
builder->Write(u"I'm a formatted paragraph with double border and nice shading."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.ApplyBordersAndShadingToParagraph.doc"; | |
doc->Save(outputPath); |