Hi Team,
I have a sample PPTX with a slide containing animated text frame. Text frame has four animated paragraphs. I use Aspose to read this PPTX and want to get images of individual animated text paragraphs. Ideally, I should get four separate images corresponding to four animated paragraphs in the PPTX slide, but I get more than 4.
The code:
using Aspose.Slides;
using Aspose.Slides.Export;
using System.Drawing.Imaging;
using Aspose.Slides.Animation;
using Aspose.Slides.Charts;
using Aspose.Slides.SmartArt;
using Aspose.Slides.Ink;
using System.Drawing;
using System.Drawing.Imaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Aspose.Slides;
using Aspose.Slides.Animation;
using Aspose.Slides.Charts;
using Aspose.Slides.SmartArt;
using Aspose.Slides.Ink;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using Aspose.Slides.Export;
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Starting Aspose.Slides processing...");
// Apply Aspose.Slides license
License license = new License();
license.SetLicense("Aspose.Total.lic"); // Make sure this file is in your application's directory
Console.WriteLine("License applied successfully");
// Check if file exists
string presentationPath = "sample.pptx";
if (!File.Exists(presentationPath))
{
Console.WriteLine($"ERROR: Presentation file not found at: {Path.GetFullPath(presentationPath)}");
return;
}
Console.WriteLine($"Loading presentation from: {Path.GetFullPath(presentationPath)}");
// Load the presentation
using (Presentation presentation = new Presentation(presentationPath))
{
Console.WriteLine($"Presentation loaded successfully. Total slides: {presentation.Slides.Count}");
// Loop through all slides
for (int slideIndex = 0; slideIndex < presentation.Slides.Count; slideIndex++)
{
// Get current slide
ISlide slide = presentation.Slides[slideIndex];
Console.WriteLine($"Processing slide {slideIndex + 1}. Total shapes: {slide.Shapes.Count}");
// Process each shape in the current slide
foreach (var curShape in slide.Shapes)
{
try
{
Console.WriteLine($" Shape type: {curShape.GetType().Name}");
IAutoShape curAutoShape = curShape as AutoShape;
if (curAutoShape == null)
{
Console.WriteLine(" Not an AutoShape - skipping");
continue;
}
if (curAutoShape.TextFrame == null)
{
Console.WriteLine(" No TextFrame found - skipping");
continue;
}
Console.WriteLine($" AutoShape has TextFrame with {curAutoShape.TextFrame.Paragraphs.Count} paragraphs");
using (var cloneShapeImage = curAutoShape.GetThumbnail())
{
Console.WriteLine($" Got thumbnail of size: {cloneShapeImage.Width}x{cloneShapeImage.Height}");
Rectangle finalRect = new Rectangle();
List<Tuple<Rectangle, Bitmap>> paragraphs = new List<Tuple<Rectangle, Bitmap>>();
int paraCounter = 1;
foreach (var curPara in curAutoShape.TextFrame.Paragraphs)
{
var paraRect = curPara.GetRect();
Console.WriteLine($" Paragraph {paraCounter} rect: X={paraRect.X}, Y={paraRect.Y}, W={paraRect.Width}, H={paraRect.Height}");
Double lineWidth = curAutoShape.LineFormat.Width;
if (Double.IsNaN(lineWidth) || Double.IsInfinity(lineWidth))
{
lineWidth = 0.0;
}
finalRect.X = (int)(curAutoShape.X + lineWidth);
finalRect.Y = (int)(curAutoShape.Y + paraRect.Y);
double paraHeight = Math.Ceiling(paraRect.Height);
finalRect.Width = Math.Max(1, (int)(curAutoShape.Width - (2 * lineWidth)));
finalRect.Height = Math.Max(1, (int)paraHeight);
Console.WriteLine($" Final rect: X={finalRect.X}, Y={finalRect.Y}, W={finalRect.Width}, H={finalRect.Height}");
string fileName = $"slide_{slideIndex + 1}_para_{paraCounter}.png";
Console.WriteLine($" Saving to file: {fileName}");
var dstImage = new Bitmap((int)(finalRect.Width), (int)(finalRect.Height));
using (Graphics g = Graphics.FromImage(dstImage))
{
g.DrawImage(cloneShapeImage, new Rectangle(0, 0, dstImage.Width, dstImage.Height), finalRect, GraphicsUnit.Pixel);
}
dstImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine($" Successfully saved paragraph {paraCounter}");
paraCounter++;
}
}
}
catch (Exception ex)
{
Console.WriteLine($" ERROR processing shape: {ex.Message}");
Console.WriteLine($" Stack trace: {ex.StackTrace}");
}
}
}
}
Console.WriteLine("Processing completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
}
}
}
sample.zip (49.7 KB)
tryAspose.zip (19.5 KB)
I used Aspose 25.5 version. The PPTX and the output images are attached.
Pls help resolve this issue. Thanks,