Right now I am using aspose.slides in c#.
Could you give me some sample code about getting textbox in slide through key words and get its position and insert png image next to the textbox.
Thank you!
Right now I am using aspose.slides in c#.
Could you give me some sample code about getting textbox in slide through key words and get its position and insert png image next to the textbox.
Thank you!
@msdos41,
Thank you for contacting support.
Please try using the following code example:
var inputFilePath = "sample.pptx";
var outputFilePath = "output.pptx";
var keyWord = "test";
var imageFilePath = "image.png";
using var presentation = new Presentation(inputFilePath);
// Add the image to the presentation resources.
using var imageStream = File.OpenRead(imageFilePath);
var image = presentation.Images.AddImage(imageStream);
foreach (var slide in presentation.Slides)
{
// Find all text boxes on the slide by the key word.
var textBoxes = new List<IAutoShape>();
foreach (var shape in slide.Shapes)
{
if (shape is IAutoShape autoShape &&
autoShape.TextFrame.Text.Contains(keyWord))
{
Console.WriteLine("X: " + autoShape.X);
Console.WriteLine("Y: " + autoShape.Y);
Console.WriteLine("Width: " + autoShape.Width);
Console.WriteLine("Height: " + autoShape.Height);
Console.WriteLine();
textBoxes.Add(autoShape);
}
}
// Add the image next to each text box.
foreach (var textBox in textBoxes)
{
var x = textBox.X;
var y = textBox.Y + textBox.Height;
var width = image.Width;
var height = image.Height;
slide.Shapes.AddPictureFrame(
ShapeType.Rectangle, x, y, width, height, image);
}
}
presentation.Save(outputFilePath, SaveFormat.Pptx);