在 .NET 中管理 PowerPoint 文本段落
概述
Aspose.Slides for .NET 将文本表示为文本框、段落和文字段的层次结构:
- ITextFrame 表示形状中的文本容器,并提供对其段落集合的访问。
- IParagraph 表示文本框中的一个段落,并提供对其文字段和段落级格式的访问。
- IPortion 表示段落内的一个文字运行。每个文字段可以拥有自己的文本和字符级格式。
因此,一个段落可以通过使用多个文字段来包含不同字体、颜色、大小和其他格式的文本。
创建和格式化段落
在形状的文本框中创建段落,并使用文字段来格式化各个文字运行。本节展示如何在每个段落中组合多个文字段并应用不同的颜色、字号和样式。
使用多个文字段创建段落
下面的 C# 示例在其 IParagraph.Portions 集合中创建了三个段落,每个段落包含三个文字运行。它复用默认段落及其空文字段,然后添加其余的段落和文字段。格式通过 IPortion.PortionFormat 应用:每个段落的第一个文字段为红色、粗体、15 磅;第二个为蓝色、斜体、18 磅。第三个文字段保留默认格式:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;
var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);
var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
var paragragaph = textFrame.Paragraphs[paragraphIndex];
var portionCount = paragragaph.Portions.Count;
for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
var portion = paragragaph.Portions[portionIndex];
portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";
if (portionIndex == 0)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
portion.PortionFormat.FontBold = NullableBool.True;
portion.PortionFormat.FontHeight = 15;
}
else if (portionIndex == 1)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontHeight = 18;
}
}
}
presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
创建项目符号和编号列表
将段落文本组织为符号、数字或自定义图像的列表。本节示例涵盖项目符号格式、多级列表以及自定义起始编号。
创建项目符号或编号列表
项目符号和编号使相关项目更易于浏览。在 Aspose.Slides 中,列表设置通过 IBulletFormat 定义。
此 C# 示例向同一文本框中添加符号项目符号和编号项。IBulletFormat.Type 选择项目符号类型, IBulletFormat.Char 提供符号, IBulletFormat.NumberedBulletStyle 选择编号样式。两项均使用黑色项目符号,高度为 100%,段落缩进为 25 磅:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);
var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);
presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
使用图片项目符号
图片项目符号让您使用自定义图像替代符号或数字。
此 C# 示例将图像作为 IPPImage 添加到演示文稿的图像集合,并将其用作段落项目符号。它将 IBulletFormat.Type 设置为使用图片项目符号,并通过 IBulletFormat.Picture 分配图像。IBulletFormat.Height 设置为 100% 以相对于段落文本调整项目符号大小:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);
presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);
创建多级列表
将 IParagraphFormat.Depth 设置为在列表的不同层级放置段落。顶层的深度为 0。
此 C# 示例为四个段落分别分配 0 到 3 的 IParagraphFormat.Depth 值,从而在同一文本框中创建层级结构。每个段落使用符号项目符号, IBulletFormat.Char 在圆形项目符号和连字符之间交替,以区分相邻层级:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;
var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;
var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;
var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);
presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);
自定义起始值的编号列表
使用 IBulletFormat.NumberedBulletStartWith 设置编号段落的初始显示数字。
此 C# 示例将 IBulletFormat.Type 设置为使用编号,并为三个段落分别指定 2、3、7 的 IBulletFormat.NumberedBulletStartWith 值。起始值对每个段落单独配置,使显示的数字可以跳过:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);
var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);
控制段落布局和结束属性
调整段落边距和缩进以控制首行和换行行的位置。您还可以分别格式化段落结束标记,而不影响可见的文字段。
设置首行缩进
使用 IParagraphFormat.Indent 属性控制段落的首行缩进。此属性仅移动首行相对于段落左边距的距离。正值将在右侧移动首行,而其余行保持与段落正文对齐。
需要整体移动段落时使用 IParagraphFormat.MarginLeft。仅需移动首行时使用 IParagraphFormat.Indent。
下面的示例为三个段落设置相同的 IParagraphFormat.MarginLeft 为 20 磅,并将 IParagraphFormat.Indent 设为 0、20、40 磅。首行的起始位置逐渐增大,而换行行保持相同的左边距:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;
var secondParagraph = new Paragraph { Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;
var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);
结果:

设置悬挂缩进
悬挂缩进是一种段落布局,首行位于其余行的左侧。在 Aspose.Slides 中,可通过 IParagraphFormat.Indent 属性实现。将 Indent 设为负值即可相对于段落正文向左移动首行。
实际使用中,IParagraphFormat.MarginLeft 定义段落正文的左侧位置,IParagraphFormat.Indent 定义首行相对于该边距的位置。要创建悬挂缩进,请将正的 MarginLeft 与负的 Indent 组合使用。
此格式对于参考文献、文献目录、术语表等需要换行行与段落正文对齐而非与首字符对齐的段落非常有用。
下面的 C# 示例将 IParagraphFormat.MarginLeft 分别设为 40 和 60 磅,同时将 IParagraphFormat.Indent 设为 -20 和 -30 磅。每个段落的首行都位于换行行的左侧;第二段落的悬挂缩进更深:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;
var secondParagraph = new Paragraph { Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
结果:

设置段落结束运行属性
IParagraph.EndParagraphPortionFormat 属性控制段落结束标记的格式。下面的示例创建一个 PortionFormat,其中 IBasePortionFormat.FontHeight 为 48 磅, IBasePortionFormat.LatinFont 为 Times New Roman。它将此格式分配给第二段落的结束标记,而不改变可见文字段的格式:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));
var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);
统计渲染行数
有关影响自动换行和行尾标点的段落规则,请参见 控制换行 和 控制悬挂标点。
使用 IParagraph.GetLinesCount 可统计段落布局后占用的行数,包括自动换行。这在检查演示文稿模板中的文本长度和布局时非常有用。
段落是 ITextFrame.Paragraphs 中的一个项目,可能占据多行渲染。段落内部的显式换行符会强制另起一行但不创建新段落。自动换行根据可用宽度生成行,而不会在文本中插入显式换行符。因此,仅统计段落或换行符字符并不能得到渲染行数。
下面的示例创建一个文本形状,统计其行数,缩小形状宽度,然后用更短的字符串替换文本。示例启用 ITextFrameFormat.WrapText 并禁用 ITextFrameFormat.AutofitType,以便形状宽度控制换行而不会自动缩小文本或调整形状大小。每次测量均使用 IParagraph.GetLinesCount。形状尺寸以磅为单位。最后,示例再添加一个段落并累计整个文本框的行数:
using System;
using Aspose.Slides;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;
var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");
shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");
paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");
var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);
var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");
在此文本及这些尺寸下,缩窄形状会增加行数,而使用短字符串替换文本会减少行数。精确计数会因字体可用性与替代、字号、边距、缩进、换行以及自动适应设置而有所不同;请使用目标环境的字体和布局设置进行模板检查。
仅行数并不能决定文本是否溢出容器。可用高度、行高、段落和行间距以及自动适应行为同样重要;即使是单行,在关闭换行时也可能超过可用宽度。
导入和导出段落内容
使用 HTML 将文本导入演示文稿,或将现有段落导出供外部使用。本节还展示如何将单个段落渲染为图像,包括表格单元格中的段落。
将 HTML 文本导入段落
使用 ParagraphCollection.AddFromHtml 将 HTML 标记转换为文本框中的段落和文字段。
此 C# 示例从文件读取 HTML 并将标记传递给 ParagraphCollection.AddFromHtml。它清除文本框的默认段落,使框中仅包含导入的内容。矩形形状在幻灯片四周留有 10 磅的边距:
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();
using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);
presentation.Save("html_text.pptx", SaveFormat.Pptx);
将段落文本导出为 HTML
使用 ParagraphCollection.ExportToHtml 将选定范围的段落导出为 HTML。当在网站上复用演示文稿文本时,您可能希望保留本地强调而不携带演示文稿的字体、字号或文本框样式。设置 TextToHtmlConversionOptions.TextInheritanceLimit 可控制导出器在格式继承上追溯的深度。
下载 示例演示文稿 并将其放置在应用程序工作目录中。其第一张幻灯片包含一个文本形状,文本为 “Inherited text; explicit red 18pt”。输入特意使用了多个格式层级:
- 演示文稿的默认文本样式提供 Calibri,字号 28 pt。
- 文本框的一级文本样式提供蓝色文字和斜体。
- 段落显式设置为粗体。
- 第二个文字段 “explicit red 18pt” 显式设置为红色文字,字号 18 pt。
下面的完整示例将相同的段落导出三次。它将 TextToHtmlConversionOptions 传递给导出方法,并将每个结果写入单独的 UTF-8 HTML 文件:
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("text-inheritance.pptx");
var textShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraphs = textShape.TextFrame.Paragraphs;
var modes = new[] { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
foreach (var mode in modes)
{
var options = new TextToHtmlConversionOptions { TextInheritanceLimit = mode };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText($"{mode}.html", html, Encoding.UTF8);
}
生成的 HTML 对第一个文字段 “Inherited text; ” 包含如下 CSS。表格列出了相关声明;其他生成属性为简洁起见已省略。
| 输出文件 | 第一个文字段的相关 CSS | 外观 |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
蓝色、粗体、斜体,28 pt Calibri(若字体可用)。 |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
蓝色、粗体、斜体;字体族和字号由页面或浏览器提供。 |
ParagraphOnly.html |
font-weight:bold |
粗体;字体族、字号、颜色和斜体由页面或浏览器提供。 |
第二个文字段在所有三种输出中均保留 color:#ff0000、font-size:18pt 和 font-weight:bold。在 All 和 TextBox 中仍保留斜体,而 ParagraphOnly 中没有。限制继承因此不会移除显式分配给段落或文字段的格式。
在同一浏览器中打开这些文件进行比较。在两种受限模式下,省略的 CSS 属性由目标页面的样式或浏览器默认值解析,因此实际外观取决于 HTML 的使用环境。
生成 HTML 剪贴板片段
将 AddClipboardFragmentHeader 设置为 false 可生成普通 HTML,设置为 true 则包含剪贴板头部。两种调用均返回字符串。头部提供了 surrounding HTML 的 UTF-8 字节偏移 (StartHTML、EndHTML) 以及要粘贴的内容偏移 (StartFragment、EndFragment)。
此示例将两个格式化段落分别导出为普通 HTML (html) 和 HTML 剪贴板片段 (clipboardHtml)。将片段写入系统剪贴板是另一步骤。接受原始 HTML 剪贴板字节的剪贴板 API 需要 UTF-8 且无 BOM。
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
var paragraphs = shape.TextFrame.Paragraphs;
paragraphs.Clear();
paragraphs.AddFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
var options = new TextToHtmlConversionOptions { AddClipboardFragmentHeader = false };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
options.AddClipboardFragmentHeader = true;
var clipboardHtml = paragraphs.ExportToHtml(0, paragraphs.Count, options);
将段落渲染为图像
IParagraph.GetImage 可直接渲染单个段落并返回 IImage。使用 IImage.Save 将结果保存到文件或流。无需渲染包含的形状或手动裁剪位图。
如果段落在其父集合中找不到、没有有效的渲染边界或无法渲染,IParagraph.GetImage 可能返回 null。在保存之前检查返回值,并在使用后释放图像。
按默认比例渲染段落
示例使用包含三段文字的单张幻灯片演示文稿,第一形状为文本框。

下面的示例选取索引为 1 的第二段落,调用不带比例参数的 IParagraph.GetImage。它先检查形状是否包含所需段落,并在渲染返回图像后调用 IImage.Save 将其以 PNG 格式保存。using 语句在使用后释放图像。
using System;
using Aspose.Slides;
using var presentation = new Presentation("sample.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape &&
textShape.TextFrame != null &&
textShape.TextFrame.Paragraphs.Count > 1)
{
var paragraph = textShape.TextFrame.Paragraphs[1];
using var paragraphImage = paragraph.GetImage();
if (paragraphImage != null)
{
paragraphImage.Save("paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
}
else
{
Console.WriteLine("The expected text shape or paragraph was not found.");
}
结果:

在表格单元格中按比例渲染段落
使用接受 float scaleX 和 float scaleY 参数的 IParagraph.GetImage 重载,可设置水平和垂直比例因子。下面的示例创建一个表格,在其第一个单元格中将段落以默认宽高的两倍渲染,并将结果保存为 PNG 图像。
using System;
using Aspose.Slides;
var scaleX = 2f;
var scaleY = 2f;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";
using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
比例因子为 1 时保持默认像素尺寸。例如,水平和垂直均为 2 时,生成的图像宽高约为默认的两倍,像素数约为四倍。较大的因子通常可获得更锐利的文字,适用于放大或高分辨率输出,但会增加内存占用和文件大小。因子低于 1 会生成更小、细节更少的图像。使用相同的因子可保持段落的宽高比;不同的水平和垂直因子会独立拉伸输出。
当需要包含形状填充、边框或其他视觉上下文时,使用 IShape.GetImage 渲染整个形状仍然有用。仅需段落图像时,请使用 IParagraph.GetImage。
常见问题解答
我可以完全禁用文本框内的换行吗?
可以。将 ITextFrameFormat.WrapText 设置为禁用换行,使行不会在文本框边缘断开。
如何获取特定段落在幻灯片上的精确边界?
使用 IParagraph.GetRect 获取段落的边界矩形。IPortion.GetRect 提供单个文字段的边界。
段落对齐方式(左、右、居中或两端对齐)在哪里控制?
IParagraphFormat.Alignment 是段落级设置,适用于整个段落,而不受单个文字段格式的影响。
要在每行内部垂直对齐不同字号的文字段,请参见 在行内对齐字体。
我可以为段落的一部分设置校对语言吗?
可以。为各个文字段设置 IBasePortionFormat.LanguageId,即可在同一段落中包含多种语言的文本。