Grouping an SVG Image with a TextBox Using Aspose.Slides for .NET

Hello.

I need help with grouping shapes in aspose slides.

I have a .pptx template with existing textbox with a set alttext name. I’m also trying to generate shapes from a svg file. The textbox should hold the name of the svg file.

After successful generating shapes from the svg file I’d like to group both the svg file (grouped using IGroupShape AddGroupShape(ISvgImage svgImage, float x, float y, float width, float height)

How do I group svgImage with the textbox?

PS: The template already have the textbox and the shapes from svgImage are created by specifying the svg file name.

Here’s the source code.

using (Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(Application.StartupPath + @"\Templates\Template1.pptx"))
{
    try
    {
        string svgContent1 = File.ReadAllText(dataGridView1.Rows[0].Cells[1].Value.ToString());
        ISvgImage svgImage1 = new SvgImage(svgContent1);
        pres.Slides[0].Shapes.AddGroupShape(svgImage1, (float)66.79292, (float)110.5453, (float)52.56, (float)52.56);
        IShape Text0_1 = SearchTextBox.FindShape(pres.Slides[0], "TextBox1");
        if (Text0_1 != null)
        {
            ((IAutoShape)Text0_1).TextFrame.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();
        }
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        sfd.Title = "Save PowerPoint Icons";
        sfd.CheckFileExists = false;
        sfd.CheckPathExists = true;
        sfd.DefaultExt = "pptx";
        sfd.Filter = "Powerpoint Files (*.pptx) | *.pptx";
        sfd.FilterIndex = 1;
        sfd.RestoreDirectory = true;
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            pres.Save(sfd.FileName, SaveFormat.Pptx);
            Cursor = Cursors.Default;
        }
    }
    catch (FileFormatException ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

@andrey.potapov Could you please take a look.

@Charles_Kamweti Please post your question in the public Aspose support forums, that are the main place for getting support for Aspose products.

@Charles_Kamweti,
Thank you for contacting support. Please note that the AddGroupShape method you used returns an IGroupShape interface. You can use it for adding the text box like this:

groupShape.Shapes.AddClone(shape, x, y, width, height);

If this does not help, please describe your requirements in more detail.

Documents: Group

Thank you.
Let me try it out. I’ll get back to you.

@andrey.potapov kindly have a look at my code.
I’ve attached 3 PowerPoint files.

  1. The template files that I’m inserting the SVG file as PowerPoint shape.
  2. Template file after successfully inserting the
    SVG file.
  3. Output of what I’m looking for after grouping the SVG file and the textbox.

I haven’t succeeded in grouping the SVG file and textbox and have an output like that on the “Template file after grouping SVG file and textbox.pptx”. Kindly help.

Here’s my code that I’m currently using to group the SVG image file with textbox…

using (Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(System.Windows.Forms.Application.StartupPath + @"\Templates\Template1.pptx"))
{
    #region
    try
    {
        IShape title = SearchTextBox.FindShape(pres.Slides[0], "Title1");
        if (title != null)
        {
            ((IAutoShape)title).TextFrame.Text = txtTitle.Text;
        }

        IShape subTitle = SearchTextBox.FindShape(pres.Slides[0], "Subtitle2");
        if (subTitle != null)
        {
            ((IAutoShape)subTitle).TextFrame.Text = txtSubtitle.Text;
        }

        string svgContent1 = File.ReadAllText(dataGridView1.Rows[0].Cells[0].Value.ToString());
        ISvgImage svgImage1 = new SvgImage(svgContent1);
        pres.Slides[0].Shapes.AddGroupShape(svgImage1, (float)66.79292, (float)110.5453, (float)52.56, (float)52.56);
        IShape Text0_1 = SearchTextBox.FindShape(pres.Slides[0], "TextBox1");
        if (Text0_1 != null)
        {
            ((IAutoShape)Text0_1).TextFrame.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();
        }

        ISlide sld0 = pres.Slides[0];
        int a = 0;
        for (int i = 0; i < sld0.Shapes.Count; i++)
        {
            IShape groupsp = sld0.Shapes[i];
            if (groupsp is Aspose.Slides.GroupShape)
            {
                IGroupShape sp = (IGroupShape)groupsp;
                a += 1;
                sp.AlternativeText = "GroupShape" + a;
            }
        }

        IShapeCollection sc = sld0.Shapes;
        IGroupShape gp = sc.AddGroupShape();
        IShape grp1 = SearchTextBox.FindShape(pres.Slides[0], "GroupShape1");
        gp.Shapes.AddClone(grp1, grp1.X, grp1.Y, grp1.Width, grp1.Height);                      //(float)66.79292, (float)110.5453, (float)52.56, (float)52.56);
        gp.Shapes.AddClone(Text0_1, Text0_1.X, Text0_1.Y, Text0_1.Width, Text0_1.Height);
        sld0.Shapes.Remove(Text0_1);
        sld0.Shapes.Remove(grp1);
        gp.Frame = new ShapeFrame((float)32.64, (float)40, (float)30, (float)23, NullableBool.False, NullableBool.False, 0);

        if (Zip_Folder == "Bulk" && rdbBulk.Checked == true)
        {
            string fname = pptName;
            string filename = @"" + savePath + "\\" + pptName.Trim() + ".pptx";
            int i = 1;
            while (true)
            {
                if (File.Exists(filename))
                {
                    filename = fname + i.ToString();
                    i++;
                    pres.Save(@"" + savePath + "\\" + filename.Trim() + ".pptx", SaveFormat.Pptx);
                }
                else
                {
                    pres.Save(@"" + savePath + "\\" + pptName.Trim() + ".pptx", SaveFormat.Pptx);
                    break;
                }
            }
            Cursor = Cursors.Default;
        }
        else
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            sfd.Title = "Save PowerPoint Icons";
            sfd.CheckFileExists = false;
            sfd.CheckPathExists = true;
            sfd.DefaultExt = "pptx";
            sfd.Filter = "Powerpoint Files (*.pptx) | *.pptx";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                pres.Save(sfd.FileName, SaveFormat.Pptx);
                Cursor = Cursors.Default;
            }
            Cursor = Cursors.Default;
            Clear();
        }
    }
    catch (FileFormatException ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
    #endregion
}

Template.zip (200.0 KB)

@Charles_Kamweti,
I am working on the issue you are experiencing and will get back to you as soon as possible.

@andrey.potapov I’ll appreciate your timely response.

@Charles_Kamweti,
Please look at the following code snippet. You should create group shapes in PowerPoint documents using Aspose.Slides like this:

var groupShape = pres.Slides[0].Shapes.AddGroupShape(svgImage, x, y, width, height);
groupShape.Shapes.AddClone(textBox);
pres.Slides[0].Shapes.Remove(textBox);

It works.

@andrey.potapov,
Thank you for your assistance. This is what I exactly needed. I should get you a cup of coffee, let me know the mode.

Thank you for everything.

@Charles_Kamweti,
Thank you for using Aspose.Slides.

@andrey.potapov
I’m grateful for your support. :star::star::star::star::star:

@andrey.potapov

Hello.

There’s some two errors that have also been experiencing.

  1. I’m experiencing system.FormatException. ‘input string was not in a correct format’.
    This happens with some SVG files.
    string svgContent1 = File.ReadAllText(dataGridView1.Rows[0].Cells[0].Value.ToString());

Is there a way I can resolve this and/or proceed to the next SVG file? All my trials have failed.

I’ve attached some SVG files that I’m working with and having the exception.

  1. I’ve also tried vectorizing PNG file and creating PowerPoint shapes. The PNG are vectorized successfully but while creating PowerPoint shapes using aspose slides the whole shape turns out black.

Here’s the source code for vectorization.

if (sfd.ShowDialog() == DialogResult.OK)
{
    Cursor.Current = Cursors.WaitCursor;
    var vectorizer = new ImageVectorizer
    {
        Configuration = { PathBuilder = new BezierPathBuilder { TraceSmoother = new ImageTraceSmoother(1), ErrorThreshold = 30, MaxIterations = 30 }, ColorsLimit = 25, LineWidth = 1 }
    };

    using (var document = vectorizer.Vectorize(tp.FileName))
    {
        Cursor.Current = Cursors.WaitCursor;
        document.Save(sfd.FileName);
        Cursor.Current = Cursors.Default;
    }
    Cursor.Current = Cursors.Default;
    MessageBox.Show(Path.GetFileNameWithoutExtension(sfd.FileName) + " converted to svg files", Application.ProductName);
}

PS: The sample SVG files are some of which are causing system. Format exception and The sample PNG files are some of the files I’ve tried to vectorize and turned out black in PowerPoint.

Sample SVG Files.zip (120.7 KB)
Sample PNG files.zip (3.0 MB)

@Charles_Kamweti,
Unfortunately, I cannot use your information, code examples, and files to reproduce the issues you are experiencing on our side. Please check your results using the latest version of Aspose.Slides for .NET if it is possible. If the issues persist, please isolate the issues, create separate forum threads for each issue, and share the following files and information:

  • input presentation file (if you used it)
  • standalone code example that can be used to reproduce the problem on our side
  • output files
  • OS version on which the code was executed
  • .NET target platform in your app
  • Aspose.Slides version you used
1 Like