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);