在 .NET 中管理 PowerPoint 文本段落
Aspose.Slides 提供了在 C# 中处理 PowerPoint 文本、段落和部分所需的所有接口和类。
- Aspose.Slides 提供了 ITextFrame 接口,允许您添加表示段落的对象。
ITextFame对象可以拥有一个或多个段落(每个段落通过回车创建)。 - Aspose.Slides 提供了 IParagraph 接口,允许您添加表示部分的对象。
IParagraph对象可以拥有一个或多个部分(iPortions 对象的集合)。 - Aspose.Slides 提供了 IPortion 接口,允许您添加表示文本及其格式属性的对象。
IParagraph 对象能够通过其底层的 IPortion 对象处理具有不同格式属性的文本。
添加包含多个段落和多个部分的文本框
以下步骤展示如何添加一个包含 3 个段落且每个段落包含 3 个部分的文本框:
- 创建一个 Presentation 类的实例。
- 通过索引访问相应幻灯片的引用。
- 向幻灯片添加一个矩形 IAutoShape。
- 获取与该 IAutoShape 关联的 ITextFrame。
- 创建两个 IParagraph 对象,并将它们添加到 ITextFrame 的
IParagraphs集合中。 - 为每个新建的
IParagraph创建三个 IPortion 对象(默认段落创建两个 Portion 对象),并将每个IPortion添加到相应IParagraph的 IPortion 集合中。 - 为每个 Portion 设置一些文本。
- 使用
IPortion对象暴露的格式属性,为每个 Portion 应用所需的格式设置。 - 保存修改后的演示文稿。
此 C# 代码实现了添加包含部分的段落的步骤:
// 实例化一个表示 PPTX 文件的 Presentation 类
using (Presentation pres = new Presentation())
{
// 访问第一张幻灯片
ISlide slide = pres.Slides[0];
// 添加一个矩形 IAutoShape
IAutoShape ashp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
// 访问 AutoShape 的 TextFrame
ITextFrame tf = ashp.TextFrame;
// 使用不同的文本格式创建段落和部分
IParagraph para0 = tf.Paragraphs[0];
IPortion port01 = new Portion();
IPortion port02 = new Portion();
para0.Portions.Add(port01);
para0.Portions.Add(port02);
IParagraph para1 = new Paragraph();
tf.Paragraphs.Add(para1);
IPortion port10 = new Portion();
IPortion port11 = new Portion();
IPortion port12 = new Portion();
para1.Portions.Add(port10);
para1.Portions.Add(port11);
para1.Portions.Add(port12);
IParagraph para2 = new Paragraph();
tf.Paragraphs.Add(para2);
IPortion port20 = new Portion();
IPortion port21 = new Portion();
IPortion port22 = new Portion();
para2.Portions.Add(port20);
para2.Portions.Add(port21);
para2.Portions.Add(port22);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
tf.Paragraphs[i].Portions[j].Text = "Portion0" + j.ToString();
if (j == 0)
{
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.FillType = FillType.Solid;
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
tf.Paragraphs[i].Portions[j].PortionFormat.FontBold = NullableBool.True;
tf.Paragraphs[i].Portions[j].PortionFormat.FontHeight = 15;
}
else if (j == 1)
{
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.FillType = FillType.Solid;
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
tf.Paragraphs[i].Portions[j].PortionFormat.FontItalic = NullableBool.True;
tf.Paragraphs[i].Portions[j].PortionFormat.FontHeight = 18;
}
}
// 保存修改后的演示文稿
pres.Save("multiParaPort_out.pptx", SaveFormat.Pptx);
}
管理段落项目符号
项目符号列表帮助您快速、高效地组织和呈现信息。带项目符号的段落更易于阅读和理解。
- 创建一个 Presentation 类的实例。
- 通过索引访问相应幻灯片的引用。
- 向选定的幻灯片添加一个 autoshape。
- 访问该 autoshape 的 TextFrame。
- 删除
TextFrame中的默认段落。 - 使用 Paragraph 类创建第一个段落实例。
- 将段落的子弹
Type设置为Symbol并指定子弹字符。 - 设置段落的
Text。 - 为子弹设置段落
Indent。 - 为子弹设置颜色。
- 为子弹设置高度。
- 将新段落添加到
TextFrame的段落集合中。 - 添加第二个段落并重复步骤 7 至 13 的过程。
- 保存演示文稿。
此 C# 代码演示如何添加段落项目符号:
// 实例化一个表示 PPTX 文件的 Presentation 类
using (Presentation pres = new Presentation())
{
// 访问第一张幻灯片
ISlide slide = pres.Slides[0];
// 添加并访问 AutoShape
IAutoShape aShp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// 访问 AutoShape 的文本框
ITextFrame txtFrm = aShp.TextFrame;
// 删除默认段落
txtFrm.Paragraphs.RemoveAt(0);
// 创建段落
Paragraph para = new Paragraph();
// 设置段落项目符号样式和符号
para.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
// 设置段落文本
para.Text = "Welcome to Aspose.Slides";
// 设置项目符号缩进
para.ParagraphFormat.Indent = 25;
// 设置项目符号颜色
para.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
para.ParagraphFormat.Bullet.Color.Color = Color.Black;
para.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True; // 将 IsBulletHardColor 设置为 true 以使用自定义项目符号颜色
// 设置项目符号高度
para.ParagraphFormat.Bullet.Height = 100;
// 将段落添加到文本框
txtFrm.Paragraphs.Add(para);
// 创建第二个段落
Paragraph para2 = new Paragraph();
// 设置段落项目符号类型和样式
para2.ParagraphFormat.Bullet.Type = BulletType.Numbered;
para2.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
// 添加段落文本
para2.Text = "This is numbered bullet";
// 设置项目符号缩进
para2.ParagraphFormat.Indent = 25;
para2.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
para2.ParagraphFormat.Bullet.Color.Color = Color.Black;
para2.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True; // 将 IsBulletHardColor 设置为 true 以使用自定义项目符号颜色
// 设置项目符号高度
para2.ParagraphFormat.Bullet.Height = 100;
// 将段落添加到文本框
txtFrm.Paragraphs.Add(para2);
// 保存修改后的演示文稿
pres.Save("Bullet_out.pptx", SaveFormat.Pptx);
}
管理图片项目符号
项目符号列表帮助您快速、高效地组织和呈现信息。图片段落同样易于阅读和理解。
- 创建一个 Presentation 类的实例。
- 通过索引访问相应幻灯片的引用。
- 向幻灯片添加一个 autoshape。
- 访问该 autoshape 的 TextFrame。
- 删除
TextFrame中的默认段落。 - 使用 Paragraph 类创建第一个段落实例。
- 在 IPPImage 中加载图片。
- 将子弹类型设置为 Picture 并指定图片。
- 设置段落的
Text。 - 为子弹设置段落
Indent。 - 为子弹设置颜色。
- 为子弹设置高度。
- 将新段落添加到
TextFrame的段落集合中。 - 添加第二个段落并按照前述步骤重复操作。
- 保存修改后的演示文稿。
此 C# 代码演示如何添加和管理图片项目符号:
// 实例化一个表示 PPTX 文件的 Presentation 类
Presentation presentation = new Presentation();
// 访问第一张幻灯片
ISlide slide = presentation.Slides[0];
// 实例化用于项目符号的图像
IImage image = Images.FromFile("bullets.png");
IPPImage ippxImage = presentation.Images.AddImage(image);
image.Dispose();
// 添加并访问 AutoShape
IAutoShape autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// 访问 AutoShape 文本框
ITextFrame textFrame = autoShape.TextFrame;
// 删除默认段落
textFrame.Paragraphs.RemoveAt(0);
// 创建新段落
Paragraph paragraph = new Paragraph();
paragraph.Text = "Welcome to Aspose.Slides";
// 设置段落项目符号样式和图像
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = ippxImage;
// 设置项目符号高度
paragraph.ParagraphFormat.Bullet.Height = 100;
// 将段落添加到文本框
textFrame.Paragraphs.Add(paragraph);
// 将演示文稿保存为 PPTX 文件
presentation.Save("ParagraphPictureBulletsPPTX_out.pptx", SaveFormat.Pptx);
// 将演示文稿保存为 PPT 文件
presentation.Save("ParagraphPictureBulletsPPT_out.ppt", SaveFormat.Ppt);
管理多级项目符号
项目符号列表帮助您快速、高效地组织和呈现信息。多级项目符号同样易于阅读和理解。
- 创建一个 Presentation 类的实例。
- 通过索引访问相应幻灯片的引用。
- 在新幻灯片中添加一个 autoshape。
- 访问该 autoshape 的 TextFrame。
- 删除
TextFrame中的默认段落。 - 使用 Paragraph 类创建第一个段落实例,并将深度设为 0。
- 通过
Paragraph类创建第二个段落实例,并将深度设为 1。 - 通过
Paragraph类创建第三个段落实例,并将深度设为 2。 - 通过
Paragraph类创建第四个段落实例,并将深度设为 3。 - 将新段落添加到
TextFrame的段落集合中。 - 保存修改后的演示文稿。
此 C# 代码演示如何添加和管理多级项目符号:
// 实例化一个表示 PPTX 文件的 Presentation 类
using (Presentation pres = new Presentation())
{
// 访问第一张幻灯片
ISlide slide = pres.Slides[0];
// 添加并访问 AutoShape
IAutoShape aShp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// 访问已创建的 AutoShape 的文本框
ITextFrame text = aShp.AddTextFrame("");
// 清除默认段落
text.Paragraphs.Clear();
// 添加第一段落
IParagraph para1 = new Paragraph();
para1.Text = "Content";
para1.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para1.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para1.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para1.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// 设置项目符号级别
para1.ParagraphFormat.Depth = 0;
// 添加第二段落
IParagraph para2 = new Paragraph();
para2.Text = "Second Level";
para2.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para2.ParagraphFormat.Bullet.Char = '-';
para2.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para2.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// 设置项目符号级别
para2.ParagraphFormat.Depth = 1;
// 添加第三段落
IParagraph para3 = new Paragraph();
para3.Text = "Third Level";
para3.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para3.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para3.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para3.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// 设置项目符号级别
para3.ParagraphFormat.Depth = 2;
// 添加第四段落
IParagraph para4 = new Paragraph();
para4.Text = "Fourth Level";
para4.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para4.ParagraphFormat.Bullet.Char = '-';
para4.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para4.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// 设置项目符号级别
para4.ParagraphFormat.Depth = 3;
// 将段落添加到集合
text.Paragraphs.Add(para1);
text.Paragraphs.Add(para2);
text.Paragraphs.Add(para3);
text.Paragraphs.Add(para4);
// 将演示文稿保存为 PPTX 文件
pres.Save("MultilevelBullet.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
管理自定义编号列表的段落
IBulletFormat 接口提供了 NumberedBulletStartWith 属性等,可用于管理具有自定义编号或格式的段落。
- 创建一个 Presentation 类的实例。
- 访问包含该段落的幻灯片。
- 向幻灯片添加一个 autoshape。
- 访问该 autoshape 的 TextFrame。
- 删除
TextFrame中的默认段落。 - 使用 Paragraph 类创建第一个段落实例,并将 NumberedBulletStartWith 设置为 2。
- 通过
Paragraph类创建第二个段落实例,并将NumberedBulletStartWith设置为 3。 - 通过
Paragraph类创建第三个段落实例,并将NumberedBulletStartWith设置为 7。 - 将新段落添加到
TextFrame的段落集合中。 - 保存修改后的演示文稿。
此 C# 代码演示如何添加和管理具有自定义编号或格式的段落:
using (var presentation = new Presentation())
{
var shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// 访问已创建的 AutoShape 的文本框
// 删除默认的现有段落
// 第一个列表
var paragraph1 = new Paragraph { Text = "bullet 2" };
paragraph1.ParagraphFormat.Depth = 4;
paragraph1.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
paragraph1.ParagraphFormat.Bullet.Type = BulletType.Numbered;
textFrame.Paragraphs.Add(paragraph1);
var paragraph2 = new Paragraph { Text = "bullet 3" };
paragraph2.ParagraphFormat.Depth = 4;
paragraph2.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
paragraph2.ParagraphFormat.Bullet.Type = BulletType.Numbered;
textFrame.Paragraphs.Add(paragraph2);
var paragraph5 = new Paragraph { Text = "bullet 7" };
paragraph5.ParagraphFormat.Depth = 4;
paragraph5.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
paragraph5.ParagraphFormat.Bullet.Type = BulletType.Numbered;
textFrame.Paragraphs.Add(paragraph5);
presentation.Save("SetCustomBulletsNumber-slides.pptx", SaveFormat.Pptx);
}
设置段落缩进
- 创建一个 Presentation 类的实例。
- 通过索引访问相应幻灯片的引用。
- 向幻灯片添加一个矩形 autoshape。
- 向矩形 autoshape 添加一个包含三段的 TextFrame。
- 隐藏矩形的线条。
- 通过各段落的 BulletOffset 属性设置每个 Paragraph 的缩进。
- 将修改后的演示文稿写入为 PPT 文件。
此 C# 代码演示如何设置段落缩进:
// 实例化 Presentation 类
Presentation pres = new Presentation();
// 获取第一张幻灯片
ISlide sld = pres.Slides[0];
// 添加矩形形状
IAutoShape rect = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 500, 150);
// 向矩形添加 TextFrame
ITextFrame tf = rect.AddTextFrame("This is first line \rThis is second line \rThis is third line");
// 设置文本以适应形状
tf.TextFrameFormat.AutofitType = TextAutofitType.Shape;
// 隐藏矩形的线条
rect.LineFormat.FillFormat.FillType = FillType.Solid;
// 获取 TextFrame 中的第一个段落并设置其缩进
IParagraph para1 = tf.Paragraphs[0];
// 设置段落项目符号样式和符号
para1.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para1.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para1.ParagraphFormat.Alignment = TextAlignment.Left;
para1.ParagraphFormat.Depth = 2;
para1.ParagraphFormat.Indent = 30;
// 获取 TextFrame 中的第二个段落并设置其缩进
IParagraph para2 = tf.Paragraphs[1];
para2.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para2.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para2.ParagraphFormat.Alignment = TextAlignment.Left;
para2.ParagraphFormat.Depth = 2;
para2.ParagraphFormat.Indent = 40;
// 获取 TextFrame 中的第三个段落并设置其缩进
IParagraph para3 = tf.Paragraphs[2];
para3.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para3.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para3.ParagraphFormat.Alignment = TextAlignment.Left;
para3.ParagraphFormat.Depth = 2;
para3.ParagraphFormat.Indent = 50;
// 将演示文稿写入磁盘
pres.Save("InOutDent_out.pptx", SaveFormat.Pptx);
设置悬挂缩进
此 C# 代码演示如何为段落设置悬挂缩进:
using (Presentation pres = new Presentation())
{
var autoShape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 50, 250, 550, 150);
Paragraph para1 = new Paragraph
{
Text = "Example"
};
Paragraph para2 = new Paragraph
{
Text = "Set Hanging Indent for Paragraph"
};
Paragraph para3 = new Paragraph
{
Text = "This C# code shows you how to set the hanging indent for a paragraph: "
};
para2.ParagraphFormat.MarginLeft = 10f;
para3.ParagraphFormat.MarginLeft = 20f;
autoShape.TextFrame.Paragraphs.Add(para1);
autoShape.TextFrame.Paragraphs.Add(para2);
autoShape.TextFrame.Paragraphs.Add(para3);
pres.Save("pres.pptx", SaveFormat.Pptx);
}
管理段落结束属性
- 创建一个 Presentation 类的实例。
- 通过位置获取包含该段落的幻灯片引用。
- 向幻灯片添加一个矩形 autoshape。
- 向矩形添加一个包含两段的 TextFrame。
- 为段落设置
FontHeight和字体类型。 - 为段落设置 End 属性。
- 将修改后的演示文稿写入为 PPTX 文件。
此 C# 代码演示如何为 PowerPoint 中的段落设置 End 属性:
using (Presentation pres = new Presentation("Test.pptx"))
{
IAutoShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
Paragraph para1 = new Paragraph();
para1.Portions.Add(new Portion("Sample text"));
Paragraph para2 = new Paragraph();
para2.Portions.Add(new Portion("Sample text 2"));
PortionFormat endParagraphPortionFormat = new PortionFormat();
endParagraphPortionFormat.FontHeight = 48;
endParagraphPortionFormat.LatinFont = new FontData("Times New Roman");
para2.EndParagraphPortionFormat = endParagraphPortionFormat;
shape.TextFrame.Paragraphs.Add(para1);
shape.TextFrame.Paragraphs.Add(para2);
pres.Save("pres.pptx", SaveFormat.Pptx);
}
将 HTML 文本导入段落
Aspose.Slides 为将 HTML 文本导入段落提供了增强支持。
- 创建一个 Presentation 类的实例。
- 通过索引访问相应幻灯片的引用。
- 向幻灯片添加一个 autoshape。
- 为
autoshape添加并访问其 ITextFrame。 - 删除
ITextFrame中的默认段落。 - 使用 TextReader 读取源 HTML 文件。
- 使用 Paragraph 类创建第一个段落实例。
- 将读取的 TextReader 中的 HTML 内容添加到 TextFrame 的 ParagraphCollection。
- 保存修改后的演示文稿。
此 C# 代码实现了将 HTML 文本导入段落的步骤:
// 创建空的演示文稿实例
using (Presentation pres = new Presentation())
{
// 访问演示文稿的默认第一张幻灯片
ISlide slide = pres.Slides[0];
// 添加 AutoShape 用于承载 HTML 内容
IAutoShape ashape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, pres.SlideSize.Size.Width - 20, pres.SlideSize.Size.Height - 10);
ashape.FillFormat.FillType = FillType.NoFill;
// 为形状添加文本框
ashape.AddTextFrame("");
// 清除已添加文本框中的所有段落
ashape.TextFrame.Paragraphs.Clear();
// 使用流读取器加载 HTML 文件
TextReader tr = new StreamReader("file.html");
// 将 HTML 流读取器中的文本添加到文本框中
ashape.TextFrame.Paragraphs.AddFromHtml(tr.ReadToEnd());
// 保存演示文稿
pres.Save("output_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
将段落文本导出为 HTML
Aspose.Slides 为将段落(包含在段落中的)文本导出为 HTML 提供了增强支持。
- 创建一个 Presentation 类的实例并加载所需的演示文稿。
- 通过索引访问相应幻灯片的引用。
- 访问包含待导出为 HTML 的文本的形状。
- 访问该形状的 TextFrame。
- 创建
StreamWriter实例并添加新的 HTML 文件。 - 为 StreamWriter 提供起始索引并导出所需的段落。
此 C# 代码演示如何将 PowerPoint 段落文本导出为 HTML:
// 加载演示文稿文件
using (Presentation pres = new Presentation("ExportingHTMLText.pptx"))
{
// 访问演示文稿的默认第一张幻灯片
ISlide slide = pres.Slides[0];
// 访问所需的索引
int index = 0;
// 访问已添加的形状
IAutoShape ashape = (IAutoShape)slide.Shapes[index];
StreamWriter sw = new StreamWriter("output_out.html", false, Encoding.UTF8);
// 将段落数据写入 HTML,指定段落起始索引和要复制的段落数
sw.Write(ashape.TextFrame.Paragraphs.ExportToHtml(0, ashape.TextFrame.Paragraphs.Count, null));
sw.Close();
}
将段落保存为图像
本节中,我们将通过两个示例演示如何将由 IParagraph 接口表示的文本段落保存为图像。两个示例均包括使用 IShape 接口的 GetImage 方法获取包含段落的形状图像,计算段落在形状内的边界,并将其导出为位图图像。这些方法可帮助您从 PowerPoint 演示文稿中提取特定文本部分并保存为单独的图像,以便在各种场景中进一步使用。
假设我们有一个名为 sample.pptx 的演示文稿,包含一张幻灯片,第一形状是包含三段的文本框。

示例 1
本示例获取第二段作为图像。为此,我们先提取演示文稿第一张幻灯片中形状的图像,然后计算该形状文本框中第二段的边界。随后将段落重新绘制到新的位图图像中,并以 PNG 格式保存。该方法特别适用于在保持文本精准尺寸和格式的前提下,将特定段落另存为单独图像的场景。
using var presentation = new Presentation("sample.pptx");
var firstShape = presentation.Slides[0].Shapes[0] as IAutoShape;
// Save the shape in memory as a bitmap.
using var shapeImage = firstShape.GetImage();
using var shapeImageStream = new MemoryStream();
shapeImage.Save(shapeImageStream, ImageFormat.Png);
// Create a shape bitmap from memory.
shapeImageStream.Seek(0, SeekOrigin.Begin);
using var shapeBitmap = Image.FromStream(shapeImageStream);
// Calculate the boundaries of the second paragraph.
var secondParagraph = firstShape.TextFrame.Paragraphs[1];
var paragraphRectangle = secondParagraph.GetRect();
// Calculate the size for the output image (minimum size - 1x1 pixel).
var imageWidth = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Width));
var imageHeight = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Height));
// Prepare a bitmap for the paragraph.
using var paragraphBitmap = new Bitmap(imageWidth, imageHeight);
// Redraw the paragraph from the shape bitmap to the paragraph bitmap.
using var imageGraphics = Graphics.FromImage(paragraphBitmap);
var drawingRectangle = new RectangleF(0, 0, paragraphRectangle.Width, paragraphRectangle.Height);
imageGraphics.DrawImage(shapeBitmap, drawingRectangle, paragraphRectangle, GraphicsUnit.Pixel);
paragraphBitmap.Save("paragraph.png", System.Drawing.Imaging.ImageFormat.Png);
结果:

示例 2
本示例在前述方法的基础上添加了缩放因子。我们将形状提取为图像并使用缩放因子 2 保存,这样在导出段落时可获得更高分辨率的输出。随后根据缩放比例计算段落边界。缩放在需要更高细节图像的情况下尤为有用,例如用于高质量印刷材料。
var imageScaleX = 2f;
var imageScaleY = imageScaleX;
using var presentation = new Presentation("sample.pptx");
var firstShape = presentation.Slides[0].Shapes[0] as IAutoShape;
// 将形状以缩放方式保存为内存位图。
using var shapeImage = firstShape.GetImage(ShapeThumbnailBounds.Shape, imageScaleX, imageScaleY);
using var shapeImageStream = new MemoryStream();
shapeImage.Save(shapeImageStream, ImageFormat.Png);
// 从内存创建形状位图。
shapeImageStream.Seek(0, SeekOrigin.Begin);
using var shapeBitmap = Image.FromStream(shapeImageStream);
// 计算第二段的边界。
var secondParagraph = firstShape.TextFrame.Paragraphs[1];
var paragraphRectangle = secondParagraph.GetRect();
paragraphRectangle.X *= imageScaleX;
paragraphRectangle.Y *= imageScaleY;
paragraphRectangle.Width *= imageScaleX;
paragraphRectangle.Height *= imageScaleY;
// 计算输出图像的尺寸(最小尺寸 - 1x1 像素)。
var imageWidth = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Width));
var imageHeight = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Height));
// 为段落准备位图。
using var paragraphBitmap = new Bitmap(imageWidth, imageHeight);
// 将段落从形状位图重新绘制到段落位图。
using var imageGraphics = Graphics.FromImage(paragraphBitmap);
var drawingRectangle = new RectangleF(0, 0, paragraphRectangle.Width, paragraphRectangle.Height);
imageGraphics.DrawImage(shapeBitmap, drawingRectangle, paragraphRectangle, GraphicsUnit.Pixel);
paragraphBitmap.Save("paragraph.png", System.Drawing.Imaging.ImageFormat.Png);
常见问题解答
我能完全关闭文本框内的自动换行吗?
可以。使用文本框的换行设置 (WrapText) 将换行关闭,即行不会在框边缘断开。
如何获取特定段落在幻灯片上的精确边界?
您可以获取段落(甚至单个 Portion)的边界矩形,以了解其在幻灯片上的确切位置和尺寸。
段落的对齐方式(左/右/居中/两端对齐)在哪里控制?
Alignment 是在 ParagraphFormat 中的段落级设置,适用于整个段落,无论各个 Portion 的格式如何。
我能为段落中的某个词单独设置拼写检查语言吗?
可以。语言在 Portion 级别通过 PortionFormat.LanguageId 设置,因此一个段落中可以共存多种语言。