• Word格式处理控件Aspose.Words for .NET教程——插入段落并调整样式


    Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

    >>Aspose.Words for .NET已经更新至最新版,提供显示/隐藏语法和拼写错误的功能,引入了可在文档内部使用水印的新帮助程序类,添加了为OOXML文档设置压缩级别的功能,点击下载体验

    了解更多Aspose文档管理产品  Aspose技术交流群(761297826)


    插入段落

    1. DocumentBuilder.Writeln 也可以在文档中插入文本字符串,但除此之外,它还会添加一个段落分隔符。该DocumentBuilder.Font 属性还指定当前字体格式,而该 属性确定当前段落格式 DocumentBuilder.ParagraphFormat 。 下例显示了如何将段落插入文档。
    2. // The path to the documents directory.
    3. string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
    4. // Initialize document.
    5. Document doc = new Document();
    6. DocumentBuilder builder = new DocumentBuilder(doc);
    7. // Specify font formatting
    8. Font font = builder.Font;
    9. font.Size = 16;
    10. font.Bold = true;
    11. font.Color = System.Drawing.Color.Blue;
    12. font.Name = "Arial";
    13. font.Underline = Underline.Dash;
    14. // Specify paragraph formatting
    15. ParagraphFormat paragraphFormat = builder.ParagraphFormat;
    16. paragraphFormat.FirstLineIndent = 8;
    17. paragraphFormat.Alignment = ParagraphAlignment.Justify;
    18. paragraphFormat.KeepTogether = true;
    19. builder.Writeln("A whole paragraph.");
    20. dataDir = dataDir + "DocumentBuilderInsertParagraph_out.doc";
    21. doc.Save(dataDir);
     
    

    计算段落的行数

    如果要计算任何Word文档的段落中的行数,则可以使用以下代码示例。

    1. // The path to the documents directory.
    2. string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
    3. string fileName = "Properties.doc";
    4. Document document = new Document(dataDir + fileName);
    5. var collector = new LayoutCollector(document);
    6. var it = new LayoutEnumerator(document);
    7. foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true))
    8. {
    9. var paraBreak = collector.GetEntity(paragraph);
    10. object stop = null;
    11. var prevItem = paragraph.PreviousSibling;
    12. if (prevItem != null)
    13. {
    14. var prevBreak = collector.GetEntity(prevItem);
    15. if (prevItem is Paragraph)
    16. {
    17. it.Current = collector.GetEntity(prevItem); // para break
    18. it.MoveParent(); // last line
    19. stop = it.Current;
    20. }
    21. else if (prevItem is Table)
    22. {
    23. var table = (Table)prevItem;
    24. it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break
    25. it.MoveParent(); // cell
    26. it.MoveParent(); // row
    27. stop = it.Current;
    28. }
    29. else
    30. {
    31. throw new Exception();
    32. }
    33. }
    34. it.Current = paraBreak;
    35. it.MoveParent();
    36. // We move from line to line in a paragraph.
    37. // When paragraph spans multiple pages the we will follow across them.
    38. var count = 1;
    39. while (it.Current != stop)
    40. {
    41. if (!it.MovePreviousLogical())
    42. break;
    43. count++;
    44. }
    45. const int MAX_CHARS = 16;
    46. var paraText = paragraph.GetText();
    47. if (paraText.Length > MAX_CHARS)
    48. paraText = $"{paraText.Substring(0, MAX_CHARS)}...";
    49. Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s).");
    50. }

    段落格式

    当前段落格式由DocumentBuilder.ParagraphFormat属性返回的ParagraphFormat对象表示。该对象封装了Microsoft Word中可用的各种段落格式设置属性。您可以通过调用ParagraphFormat.ClearFormatting轻松将段落格式重置为默认样式,即默认样式为普通样式,左对齐,无缩进,无间距,无边框和无阴影 。下例显示了如何设置段落格式。

    1. Document doc = new Document();
    2. DocumentBuilder builder = new DocumentBuilder(doc);
    3. // Set paragraph formatting properties
    4. ParagraphFormat paragraphFormat = builder.ParagraphFormat;
    5. paragraphFormat.Alignment = ParagraphAlignment.Center;
    6. paragraphFormat.LeftIndent = 50;
    7. paragraphFormat.RightIndent = 50;
    8. paragraphFormat.SpaceAfter = 25;
    9. // Output text
    10. builder.Writeln("I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping.");
    11. builder.Writeln("I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like.");
    12. dataDir = dataDir + "DocumentBuilderSetParagraphFormatting_out.doc";
    13. doc.Save(dataDir);

    应用段落样式

    一些格式对象(例如Font或ParagraphFormat)支持样式。单个内置或用户定义的样式由Style对象表示,该对象包含相应的样式属性,例如名称,基本样式,样式的字体和段落格式,等等。

    此外, Style 对象提供Style.StyleIdentifier 属性,该 属性返回由Style.StyleIdentifier 枚举值表示的与语言环境无关的样式标识符 。关键是Microsoft Word中内置样式的名称针对不同的语言进行了本地化。使用样式标识符,无论文档语言是什么,都可以找到正确的样式。枚举值对应于Microsoft Word内置样式,例如Normal,Heading 1,Heading 2等。所有用户定义的样式均分配有 StyleIdentifier.User值。下例显示了如何应用段落样式。

    1. Document doc = new Document();
    2. DocumentBuilder builder = new DocumentBuilder(doc);
    3. // Set paragraph style
    4. builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title;
    5. builder.Write("Hello");
    6. dataDir = dataDir + "DocumentBuilderApplyParagraphStyle_out.doc";
    7. doc.Save(dataDir);

    插入样式分隔符以放置不同的段落样式

    一些格式对象(例如Font或ParagraphFormat)支持样式。单个内置或用户定义的样式由Style对象表示,该对象包含相应的样式属性,例如名称,基本样式,样式的字体和段落格式,等等。

    可以使用Ctrl + Alt +在MS Word中输入键盘快捷键将样式分隔符添加到段落的末尾。此功能允许在一个逻辑打印段落中使用两种不同的段落样式。如果要使特定标题开头的某些文本出现在目录中,但又不想整个标题出现在目录中,则可以使用此功能。下面的代码示例演示如何插入样式分隔符以放置不同的段落样式。

    1. Document doc = new Document();
    2. DocumentBuilder builder = new DocumentBuilder(doc);
    3. Style paraStyle = builder.Document.Styles.Add(StyleType.Paragraph, "MyParaStyle");
    4. paraStyle.Font.Bold = false;
    5. paraStyle.Font.Size = 8;
    6. paraStyle.Font.Name = "Arial";
    7. // Append text with "Heading 1" style.
    8. builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
    9. builder.Write("Heading 1");
    10. builder.InsertStyleSeparator();
    11. // Append text with another style.
    12. builder.ParagraphFormat.StyleName = paraStyle.Name;
    13. builder.Write("This is text with some other formatting ");
    14. dataDir = dataDir + "InsertStyleSeparator_out.doc";
    15. // Save the document to disk.
    16. doc.Save(dataDir);

    在段落上应用边框和底纹

    边框由BorderCollection表示。这是按索引或按边框类型访问的Border对象的集合。边框类型由BorderType枚举表示。枚举的某些值适用于多个或仅一个文档元素。例如,BorderType.Bottom适用于段落或表格单元格,而BorderType.DiagonalDown仅指定表格单元格中的对角线边框。

    边框集合和每个单独的边框都具有相似的属性,例如颜色,线条样式,线条宽度,距文本的距离以及可选的阴影。它们由相同名称的属性表示。您可以通过组合属性值来实现不同的边框类型。此外,BorderCollection和Border对象都允许您通过调用Border.ClearFormatting方法将这些值重置为默认值。请注意,当边框属性重置为默认值时,边框是不可见的。该 着色 类包含文档元素的材质属性。您可以设置所需的明暗处理纹理以及应用于元素的背景和前景的颜色。

    阴影纹理设置有 TextureIndex 枚举值,该值允许将各种图案应用到 Shading 对象。例如,要为文档元素设置背景色,请使用TextureIndex.TextureSolid值并适当设置前景色。下例显示了如何对段落应用边框和阴影。下例显示了如何对段落应用边框和阴影。

    1. Document doc = new Document();
    2. DocumentBuilder builder = new DocumentBuilder(doc);
    3. // Set paragraph borders
    4. BorderCollection borders = builder.ParagraphFormat.Borders;
    5. borders.DistanceFromText = 20;
    6. borders[BorderType.Left].LineStyle = LineStyle.Double;
    7. borders[BorderType.Right].LineStyle = LineStyle.Double;
    8. borders[BorderType.Top].LineStyle = LineStyle.Double;
    9. borders[BorderType.Bottom].LineStyle = LineStyle.Double;
    10. // Set paragraph shading
    11. Shading shading = builder.ParagraphFormat.Shading;
    12. shading.Texture = TextureIndex.TextureDiagonalCross;
    13. shading.BackgroundPatternColor = System.Drawing.Color.LightCoral;
    14. shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon;
    15. builder.Write("I'm a formatted paragraph with double border and nice shading.");
    16. dataDir = dataDir + "DocumentBuilderApplyBordersAndShadingToParagraph_out.doc";
    17. doc.Save(dataDir);

  • 相关阅读:
    大数据flink篇之二-基础实例wordcount
    mysql--快速熟悉项目的数据库模型
    高校教务系统登录页面JS分析——华东交通大学
    如何用Postman实现自动化测试(含视频讲解)
    范西特-泽尼克定理的证明
    基于单片机温湿度PM2.5报警系统
    [题] 前缀和 (含输入输出的耗时对比)
    react-route的路由
    竞赛选题 深度学习火车票识别系统
    工商管理论文要怎么写?
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/125890843