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

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

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


插入段落

DocumentBuilder.Writeln 也可以在文档中插入文本字符串,但除此之外,它还会添加一个段落分隔符。该DocumentBuilder.Font 属性还指定当前字体格式,而该 属性确定当前段落格式 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 formattingFont font = builder.Font;font.Size = 16;font.Bold = true;font.Color = System.Drawing.Color.Blue;font.Name = "Arial";font.Underline = Underline.Dash;// Specify paragraph formattingParagraphFormat 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);

计算段落的行数

如果要计算任何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).");            }

段落格式

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

Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);// Set paragraph formatting propertiesParagraphFormat paragraphFormat = builder.ParagraphFormat;paragraphFormat.Alignment = ParagraphAlignment.Center;paragraphFormat.LeftIndent = 50;paragraphFormat.RightIndent = 50;paragraphFormat.SpaceAfter = 25;// Output textbuilder.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 对象提供Style.StyleIdentifier 属性,该 属性返回由Style.StyleIdentifier 枚举值表示的与语言环境无关的样式标识符 。关键是Microsoft Word中内置样式的名称针对不同的语言进行了本地化。使用样式标识符,无论文档语言是什么,都可以找到正确的样式。枚举值对应于Microsoft Word内置样式,例如Normal,Heading 1,Heading 2等。所有用户定义的样式均分配有 StyleIdentifier.User值。下例显示了如何应用段落样式。

Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);// Set paragraph stylebuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title;builder.Write("Hello");dataDir = dataDir + "DocumentBuilderApplyParagraphStyle_out.doc";doc.Save(dataDir);

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

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

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

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

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

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

Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);// Set paragraph bordersBorderCollection 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 shadingShading 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);


还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年4月23日
下一篇 2020年4月23日

相关推荐

发表回复

登录后才能评论