如何使用 Aspose 在 C# 中从幻灯片中获取文本
How to get text from slide in C# using Aspose
我正在获取 ppt 文件幻灯片中的所有形状,现在我想从这些形状中获取文本我该怎么做
这是我获取 ppt 文件中所有幻灯片形状的方法
public void Main(string[] args)
{
// The path to the documents directory.
string dataDir = Path.GetFullPath(@"C:\Users\Vipin\Desktop\");
//Load the desired the presentation
Presentation pres = new Presentation(dataDir + "Android.ppt");
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Accessing a slide using its slide index
int slideCount = prestg.Slides.Count();
for (int i = 0; i <= slideCount - 1; i++)
{
ISlide slide = pres.Slides[i];
foreach (IShape shap in slide.Shapes)
{
int slideCountNumber = i + 1;
float shapeHeight = shap.Frame.Height;
float shapeWidth = shap.Frame.Width;
Debug.Write("slide Number: " + slideCountNumber + " shape width = " + shapeWidth + " shapeHeight = " + shapeHeight);
}
}
}
}
现在我可以从中获取文本吗
您可能不想从所有形状中提取文本,而是从文本框中提取文本。为此,请使用 PresentationScanner
class
公开的 GetAllTextFrames
静态方法
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Get an Array of ITextFrame objects from all slides in the PPTX
ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true);
//Loop through the Array of TextFrames
for (int i = 0; i < textFramesPPTX.Length; i++)
//Loop through paragraphs in current ITextFrame
foreach (IParagraph para in textFramesPPTX[i].Paragraphs)
//Loop through portions in the current IParagraph
foreach (IPortion port in para.Portions)
{
//Display text in the current portion
Console.WriteLine(port.Text);
//Display font height of the text
Console.WriteLine(port.PortionFormat.FontHeight);
//Display font name of the text
if (port.PortionFormat.LatinFont != null)
Console.WriteLine(port.PortionFormat.LatinFont.FontName);
}
如果您没有许可,aspose 会给您截断的文本。所以如果你使用 Microsoft.Office.Interop.PowerPoint
对你来说会更好
使用如下
public void ReadSlide(){
string filePath= @"C:\Users\UserName\Slide.pptx";
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
string presentation_textforParent = "";
foreach (var item in presentation.Slides[1].Shapes)
{
var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
presentation_textforParent += text + " ";
}
}
}
}
我正在获取 ppt 文件幻灯片中的所有形状,现在我想从这些形状中获取文本我该怎么做
这是我获取 ppt 文件中所有幻灯片形状的方法
public void Main(string[] args)
{
// The path to the documents directory.
string dataDir = Path.GetFullPath(@"C:\Users\Vipin\Desktop\");
//Load the desired the presentation
Presentation pres = new Presentation(dataDir + "Android.ppt");
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Accessing a slide using its slide index
int slideCount = prestg.Slides.Count();
for (int i = 0; i <= slideCount - 1; i++)
{
ISlide slide = pres.Slides[i];
foreach (IShape shap in slide.Shapes)
{
int slideCountNumber = i + 1;
float shapeHeight = shap.Frame.Height;
float shapeWidth = shap.Frame.Width;
Debug.Write("slide Number: " + slideCountNumber + " shape width = " + shapeWidth + " shapeHeight = " + shapeHeight);
}
}
}
}
现在我可以从中获取文本吗
您可能不想从所有形状中提取文本,而是从文本框中提取文本。为此,请使用 PresentationScanner
class
GetAllTextFrames
静态方法
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Get an Array of ITextFrame objects from all slides in the PPTX
ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true);
//Loop through the Array of TextFrames
for (int i = 0; i < textFramesPPTX.Length; i++)
//Loop through paragraphs in current ITextFrame
foreach (IParagraph para in textFramesPPTX[i].Paragraphs)
//Loop through portions in the current IParagraph
foreach (IPortion port in para.Portions)
{
//Display text in the current portion
Console.WriteLine(port.Text);
//Display font height of the text
Console.WriteLine(port.PortionFormat.FontHeight);
//Display font name of the text
if (port.PortionFormat.LatinFont != null)
Console.WriteLine(port.PortionFormat.LatinFont.FontName);
}
如果您没有许可,aspose 会给您截断的文本。所以如果你使用 Microsoft.Office.Interop.PowerPoint
对你来说会更好使用如下
public void ReadSlide(){
string filePath= @"C:\Users\UserName\Slide.pptx";
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
string presentation_textforParent = "";
foreach (var item in presentation.Slides[1].Shapes)
{
var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
presentation_textforParent += text + " ";
}
}
}
}