Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Thu Oct 17, 2024 8:10 pm

Spire.Presentation

I am trying to create a connection line between 2 shapes in PowerPoint. I am attempting to use AppendShapeConnector but I am not sure how it would know to group the 2 shapes to the attach points? If you run the code below to create a new 2_example.pptx you will notice if you move either of the 2 shapes the connection line is not attached as it is in the 1_example.pptx.

The attached example is a textbox that I want to autosize the shape to the text. I was not able to do it with the existing shape so I needed to create a new one. I thought about trying to update the existing StraightConnector1 but I could not figure out how to gain access to the properties which would be a separate issue. Thanks for any help!

Code: Select all
                Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation("1_example.pptx", FileFormat.Auto);
                List<IShape> data = presentation.Slides[0].Shapes.ToArray().ToList();
                List<IShape> connectors = data.ToList().Where(a => a.Name.Contains(" Connector")).ToList();

                foreach (var shape1 in data)
                {
                    if (shape1 is IAutoShape && !string.IsNullOrWhiteSpace(((IAutoShape)shape1).TextFrame.Text))
                    {
                        IAutoShape oldShape = (IAutoShape)shape1;
                        ITextFrameProperties oldTextFrame = oldShape.TextFrame;

                        IAutoShape newShape = presentation.Slides[0].Shapes.AppendShape(ShapeType.RoundCornerRectangle, new RectangleF(oldShape.Left, oldShape.Top, oldShape.Width, oldShape.Height));

                        newShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                        newShape.Fill.SolidColor.Color = oldShape.Fill.SolidColor.Color;
                        newShape.Line.Style = oldShape.Line.Style;//TextLineStyle.ThickThin;
                        newShape.Line.Width = oldShape.Line.Width;//5;
                        newShape.Line.FillFormat.FillType = oldShape.Line.FillFormat.FillType;
                        newShape.Line.FillFormat.SolidFillColor.Color = oldShape.Line.FillFormat.SolidFillColor.Color;//Color.Red;

                        newShape.TextFrame.TextRange.FontHeight = oldTextFrame.TextRange.FontHeight;
                        newShape.TextFrame.TextRange.LatinFont = oldTextFrame.TextRange.LatinFont;
                        newShape.TextFrame.TextRange.Paragraph.Alignment = oldTextFrame.TextRange.Paragraph.Alignment;
                        newShape.TextFrame.Text = oldTextFrame.Text;
                        newShape.TextFrame.WordWrap = false;
                        newShape.TextFrame.AutofitType = TextAutofitType.Shape;
                        newShape.Left = oldShape.Left;
                        newShape.Top = oldShape.Top;

                        presentation.Slides[0].Shapes.Remove(oldShape);

                        string locaitonName = oldTextFrame.Text.Split('\r')[0];
                        var oldConnector = connectors.FirstOrDefault(a => a.Name.Contains(locaitonName));
                        if (oldConnector is not null)
                        {
                            int newLeft = Convert.ToInt32(newShape.Left);
                            int newTop = Convert.ToInt32(newShape.Top);
                            int oldLeft = Convert.ToInt32(oldConnector.Left);
                            int oldTop = Convert.ToInt32(oldConnector.Top);
                           
                            IShape newConnector = presentation.Slides[0].Shapes.AppendShapeConnector(ShapeType.StraightConnector1, new RectangleF(newLeft, newTop, Math.Abs(oldLeft - newLeft), Math.Abs(oldTop - newTop)));
                            newConnector.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                            newConnector.Fill.SolidColor.Color = oldConnector.Fill.SolidColor.Color;
                            newConnector.Line.Style = oldConnector.Line.Style;//TextLineStyle.ThickThin;
                            newConnector.Line.Width = oldConnector.Line.Width;//5;
                            newConnector.Line.FillFormat.FillType = oldConnector.Line.FillFormat.FillType;
                            newConnector.Line.FillFormat.SolidFillColor.Color = oldConnector.Line.FillFormat.SolidFillColor.Color;//Color.Red;

                            presentation.Slides[0].Shapes.Remove(oldConnector);
                        }
                    }
                }
                presentation.SaveToFile("2_example.pptx", FileFormat.Auto);

ThisIsWolf1
 
Posts: 2
Joined: Thu Oct 17, 2024 7:49 pm

Fri Oct 18, 2024 7:18 am

Hello,

Thanks for your feedback. I have reproduced the issue you mentioned using the PPT document you provided. Currently, our product does not support specifying connection relationships between shapes, this issue has been logged in our bug tracking system under the number SPIREPPT-2627. Additionally, I found that setting the size of the text to fit the shape does not take effect(
Code: Select all
 IAutoShape shape = (IAutoShape)shape1;shape.TextFrame.AutofitType = TextAutofitType.Shape;
), so this issue has been logged in our bug tracking system under the number SPIREPPT-2626. Our Dev team will investigate them further, once there is any update, we will let you know.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Fri Oct 18, 2024 12:12 pm

I was able to resize the shape with the following. Thanks!

Code: Select all
                Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation(fileStream, FileFormat.Auto);
                List<IShape> data = presentation.Slides[0].Shapes.ToArray().ToList();

                foreach (var shape1 in data)
                {
                    if (shape1 is IAutoShape && !string.IsNullOrWhiteSpace(((IAutoShape)shape1).TextFrame.Text))
                    {
                        IAutoShape oldShape = (IAutoShape)shape1;

                        oldShape.Height = 0;
                        oldShape.Width = 0;
                        oldShape.TextFrame.AutofitType = TextAutofitType.Shape;
                    }
                }

                presentation.SaveToFile("2_" + fileName, FileFormat.Auto);

ThisIsWolf1
 
Posts: 2
Joined: Thu Oct 17, 2024 7:49 pm

Mon Oct 21, 2024 1:30 am

Hello,

Thanks for your feedback. We're very pleased to hear that your issue has been resolved. Your sharing of this experience will also be helpful to other customers, and we express our sincere gratitude for that.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.Presentation