在幻灯片中,所有元素都以Shape 形式添加到PowerPoint文档中。我们前面介绍了如何使用Spire.Presentation提取幻灯片中的文本和图片,该文将详细介绍如何使用Spire.Presentation读取幻灯片中文本框的位置大小,以及文本字体大小,字体类型和是否为粗体,斜体,是否设置下划线等。除了能获取图片,Spire.Presentation 还支持获取幻灯片中图片的位置、大小等信息。
C#
//初始化一个Presentation对象对加载文档
Presentation presentation = new Presentation();
presentation.LoadFromFile("Test.ppt");
//获取第一个Shape
IAutoShape shape = presentation.Slides[0].Shapes[0] as IAutoShape;
//获取位置
float left = shape.Left;
float top = shape.Top;
//获取大小
float width = shape.Width;
float height = shape.Height;
//获取文本框里的段落
TextParagraph par = shape.TextFrame.Paragraphs[0];
//获取段落里文本的字体
TextFont font = par.TextRanges[0].EastAsianFont;
string name = font.FontName;
//加粗
TriState boldState = par.TextRanges[0].IsBold;
//字体大小
float size = par.TextRanges[0].FontHeight;
//斜体
TriState ItalicState = par.TextRanges[0].IsItalic;
//下划线
TextUnderlineType underline = par.TextRanges[0].TextUnderlineType;
//获取第二個shape(图片)
SlidePicture picture = presentation.Slides[0].Shapes[1] as SlidePicture;
//图片位置
float Pleft = picture.Left;
float Ptop = picture.Top;
//图片大小
float Pwidth = picture.Width;
float Pheight = picture.Height;
//获取图片
PictureShape pic = picture.PictureFill.Picture;
Image image = pic.EmbedImage.Image;
//保存图片
image.Save("Picture.jpg");
VB.NET
'初始化一个Presentation对象对加载文档
Dim presentation As New Presentation()
presentation.LoadFromFile("Test.ppt")
'获取第一个Shape
Dim shape As IAutoShape = TryCast(presentation.Slides(0).Shapes(0), IAutoShape)
'获取位置
Dim left As Single = shape.Left
Dim top As Single = shape.Top
'获取大小
Dim width As Single = shape.Width
Dim height As Single = shape.Height
'获取文本框里的段落
Dim par As TextParagraph = shape.TextFrame.Paragraphs(0)
'获取段落里文本的字体
Dim font As TextFont = par.TextRanges(0).EastAsianFont
Dim name As String = font.FontName
'加粗
Dim boldState As TriState = par.TextRanges(0).IsBold
'字体大小
Dim size As Single = par.TextRanges(0).FontHeight
'斜体
Dim ItalicState As TriState = par.TextRanges(0).IsItalic
'下划线
Dim underline As TextUnderlineType = par.TextRanges(0).TextUnderlineType
'获取第二個shape(图片)
Dim picture As SlidePicture = TryCast(presentation.Slides(0).Shapes(1), SlidePicture)
'图片位置
Dim Pleft As Single = picture.Left
Dim Ptop As Single = picture.Top
'图片大小
Dim Pwidth As Single = picture.Width
Dim Pheight As Single = picture.Height
'获取图片
Dim pic As PictureShape = picture.PictureFill.Picture
Dim image As Image = pic.EmbedImage.Image
'保存图片
image.Save("Picture.jpg")