Convert an SVG File into a Group of Shapes, Add a Background Shape and Text Box and Group Them All Together in C#

I need some assistance in adding a background shape to an svg file converted to group of shapes. I’m using the following code to add svg file as background shape, and another svg file converted to a group of shape and fill the textbox with the svg(svg converted to group of shapes) file name. I need to group the three into one group and repeat this for all background shapes, svg files and textboxes. I’ve attached the current output I have and the expected output.

Here’s my current code. Attached is the pptx template I’m using, the current and expected output files.

FolderBrowserDialog fold = new FolderBrowserDialog();
if (fold.ShowDialog() == DialogResult.OK)
{
    string svgFilePath = fold.SelectedPath;
    DirectoryInfo dInfo = new DirectoryInfo(svgFilePath);
    FileInfo[] sfiles = dInfo.GetFiles("*.svg");
    if (sfiles.Any())
    {
        int svgFilesPerPage = 20;
        string[] svgFiles = Directory.GetFiles(svgFilePath, "*.svg");
        string bgContent = File.ReadAllText(bgFile);
        ISvgImage bgSVG = new SvgImage(bgContent);

        using (Presentation presentation = new Presentation(Application.StartupPath + @"\Templates\DefaultTemplate.pptx"))
        {
            int slidesNeeded = (int)Math.Ceiling((double)svgFiles.Length / svgFilesPerPage);
            for (int i = 0; i < slidesNeeded && i < presentation.Slides.Count; i++)
            {
                float[] svgXPosition = { (float)66.79292, (float)254.5669, (float)442.3408, (float)630.1146, (float)817.8885, (float)66.79292, (float)254.5669, (float)442.3408, (float)630.1146, (float)817.8885, (float)66.79292, (float)254.5669, (float)442.3408, (float)630.1146, (float)817.8885, (float)66.79292, (float)254.5669, (float)442.08, (float)630.1146, (float)817.8885 };
                float[] svgYPosition = { (float)110.5453, (float)110.751, (float)110.1687, (float)110.751, (float)110.1687, (float)219.1795, (float)219.3853, (float)218.8678, (float)219.3853, (float)218.803, (float)329.184, (float)329.3898, (float)328.8723, (float)329.3898, (float)328.8075, (float)441.1568, (float)441.3625, (float)440.64, (float)441.3625, (float)440.7802 };
                float[] svgXWidth = { (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56 };
                float[] svgYHeight = { (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56, (float)52.56 };

                //Fll Title and Subtitle
                IShape title = SearchTextBox.FindShape(presentation.Slides[i], "Title1");
                if (title != null)
                {
                    title.FillFormat.FillType = FillType.NoFill;
                    ITextFrame titleFrame = ((IAutoShape)title).TextFrame;
                    titleFrame.Text = txtInfo.ToTitleCase(slideTitle);
                    IPortion titlePort = titleFrame.Paragraphs[0].Portions[0];
                    titlePort.PortionFormat.LatinFont = new FontData(titleString[0]);
                    titlePort.PortionFormat.FontHeight = float.Parse(titleString[1]);
                }

                IShape subTitle = SearchTextBox.FindShape(presentation.Slides[i], "Subtitle2");
                if (subTitle != null)
                {
                    subTitle.FillFormat.FillType = FillType.NoFill;
                    ITextFrame subtitleFrame = ((IAutoShape)subTitle).TextFrame;
                    subtitleFrame.Text = txtInfo.ToTitleCase(slideSubtitle);
                    IPortion subtitlePort = subtitleFrame.Paragraphs[0].Portions[0];
                    subtitlePort.PortionFormat.LatinFont = new FontData(subtitleString[0]);
                    subtitlePort.PortionFormat.FontHeight = float.Parse(subtitleString[1]);
                }

                for (int j = 0; j < svgFilesPerPage && (i * svgFilesPerPage + j) < svgFiles.Length; j++)
                {
                    //Load SVG file
                    string svgContent = File.ReadAllText(svgFiles[i * svgFilesPerPage + j]);
                    ISvgImage svgImage = new Aspose.Slides.SvgImage(svgContent);

                    //Size and Postio for Backgroundshape
                    float pixelIncrease = 24f;
                    float posPixel = 12f;

                    // Use known positions for each SVG image
                    float svgX = svgXPosition[j];
                    float svgY = svgYPosition[j];
                    float svgWidth = svgXWidth[j];
                    float svgHeight = svgYHeight[j];

                    // Add background shape for each SVG image
                    IPPImage ppImageBg = presentation.Images.AddImage(bgSVG);
                    presentation.Slides[i].Shapes.AddPictureFrame(ShapeType.Rectangle, svgX - posPixel, svgY - posPixel, svgWidth + pixelIncrease, svgHeight + pixelIncrease, ppImageBg);

                    //Add SVG image as a shape to the slide
                    var groupshape = presentation.Slides[i].Shapes.AddGroupShape(svgImage, svgX, svgY, svgWidth, svgHeight);
                    foreach (IShape svgShape in groupshape.Shapes)
                    {
                        Color fillColor = shapeColors[0];
                        svgShape.FillFormat.FillType = FillType.Solid;
                        svgShape.FillFormat.SolidFillColor.Color = fillColor;
                    }

                    for (int k = 1; k <= svgFilesPerPage; k++)
                    {
                        string textBoxName = "TextBox" + k;

                        // Find the textbox by alternative text
                        IShape textBox = SearchTextBox.FindShape(presentation.Slides[i], textBoxName);

                        // Check if the textbox exists and update its text
                        if (textBox != null && (i * svgFilesPerPage + k) <= svgFiles.Length)
                        {
                            ((IAutoShape)textBox).FillFormat.FillType = FillType.NoFill;
                            ITextFrame textFrame = ((IAutoShape)textBox).TextFrame;
                            var iconName = Regex.Replace(Path.GetFileNameWithoutExtension(svgFiles[i * svgFilesPerPage + k - 1]), @"[\d-]", string.Empty);
                            textFrame.Text = txtInfo.ToTitleCase(iconName);// Path.GetFileNameWithoutExtension(txtInfo.ToTitleCase(svgFiles[i * svgFilesPerPage+k-1]));
                            IPortion portion = textFrame.Paragraphs[0].Portions[0];
                            portion.PortionFormat.LatinFont = new FontData(textString[0]);
                            portion.PortionFormat.FontHeight = float.Parse(textString[1]);
                        }
                        else if (textBox != null)
                        {
                            presentation.Slides[i].Shapes.Remove(textBox);
                            k--;
                        }
                    }
                }
            }

            //Remove extra slides
            for (int i = slidesNeeded; i < presentation.Slides.Count; i++)
            {
                presentation.Slides.RemoveAt(i);
                //adjust index after removal
                i--;
            }
            // Save the modified PowerPoint presentation
            Clear(presentation);
        }
    }
    else { AlertNotification.ShowAlertMessage("No svg files found.", AlertNotification.AlertType.ERROR); }
}

Files.zip (779.6 KB)

@Charles_Kamweti,
Thank you for contacting support.

Could you please also share the SVG file you used for the background and the SVG files for the shapes?

Here’s the zip file with SVG file for the background and the SVG files for the shapes for your reference.
SVG Files.zip (43.5 KB)

@Charles_Kamweti,
To add the images to the presentation and group them with the text boxes, you can perform the following for each item:

  1. Add the images to presentation resources.
  2. Create a new group shape.
  3. Add the background shape and item shape to the group shape.
  4. Clone the text box and add it to the group shape.
  5. Set new text for the new text box.
  6. Remove the original text box.

The following code snippet shows you how to do this (formatting code was skipped):

// Add the images to presentation resources.
IPPImage ppBackgroundImage = presentation.Images.AddImage(backgroundImage);
IPPImage ppItemImage = presentation.Images.AddImage(itemImage);

// Create a new group shape.
IGroupShape groupShape = slide.Shapes.AddGroupShape();

// Add the background shape and item shape to the group shape.
IPictureFrame backgroundPicture = groupShape.Shapes.AddPictureFrame(ShapeType.Rectangle, backgroundX, backgroundY, backgroundWidth, backgroundHeight, ppBackgroundImage);
IPictureFrame itemPicture = groupShape.Shapes.AddPictureFrame(ShapeType.Rectangle, itemX, itemY, itemWidth, itemHeight, ppItemImage);

// Clone the text box and add it to the group shape.
IAutoShape newTextBox = groupShape.Shapes.AddClone(textBox) as IAutoShape;

// Set new text for the new text box.
newTextBox.TextFrame.Text = svgFileName;

// Remove the original text box.
slide.Shapes.Remove(textBox);

I hope this will help you.