How to connect two ovals with an arrow using OpenXml SDK?

Buri 0 Reputation points
2024-06-13T14:45:34.66+00:00

I am trying to use this code for creating two ovals and line. My code is based on the xml in which I connected two ovals. So at the end I woukd like to have two ovals that are connected with an arrow, and remain connected after moving one of the oval.

    public async Task WriteAsync()
    {
        var text = "Example text in the text box";
        Paragraph para = new Paragraph();
        Run run = new Run();
        run.AppendChild(new Text("There are two oval:"));
        para.AppendChild(run);
        body.AppendChild(para);

        var ovalHeight = 61;
        var ovalWidth = 112;

        var oval1leftMargin = 0;
        var oval1topMargin = 50;
        Oval oval1 = new Oval()
        {
            Id = "oval1",
            Style = $"position:absolute;width:{ovalWidth}pt;height:{ovalHeight}pt;margin-left:{oval1leftMargin}pt;margin-top:{oval1topMargin}pt;visibility:visible",
            FillColor = "white",
            StrokeColor = "black",
        };
        
        TextBox ovalTextBox = new TextBox();
        TextBoxContent textBoxContent = new TextBoxContent();
        Paragraph paragraphInOval = new Paragraph();
        Run runInOval = new Run();
        Text textInOval = new Text(text);
        runInOval.AppendChild(textInOval);
        paragraphInOval.AppendChild(runInOval);
        textBoxContent.AppendChild(paragraphInOval);
        ovalTextBox.AppendChild(textBoxContent);
        oval1.AppendChild(ovalTextBox);

        var oval2leftMargin = 240;
        var oval2topMargin = 20;
        Oval oval2 = new Oval()
        {
            Id = "oval2",
            Style = $"position:absolute;width:{ovalWidth}pt;height:{ovalHeight}pt;margin-left:{oval2leftMargin}pt;margin-top:{oval2topMargin}pt;visibility:visible",
            FillColor = "white",
            StrokeColor = "black",
        };
        
        V.Shapetype connectorType = new()
        {
            Id="connectortype1",
            Oned=true,
            Filled=false,
            CoordinateSize= "21600,21600",
            EdgePath= "m,l21600,21600e"
        };
        connectorType.Append(new V.Path()
        {
            ShowArrowhead=true,
            AllowFill=false,
            ConnectionPointType=ConnectValues.None,
        });
        connectorType.Append(new O.Lock()
        {
            Extension = ExtensionHandlingBehaviorValues.Edit,
            ShapeType=true
        });
        V.Shape connector = new V.Shape() {
            Id="connector1",
            Type= "connectortype1",
            ConnectorType=ConnectorValues.Straight,
        
        };
        connector.Append(new V.Stroke()
        {
            EndArrow=StrokeArrowValues.Block,
        });

        
        Picture picture = new Picture();
        Group group = new Group()
        {
            Id = "group1",
            HorizontalPercentage = 100,
            Style = "width:451.3pt;height:270.8pt;mso-position-horizontal-relative:char;mso-position-vertical-relative:line",
            EditAs = EditAsValues.Canvas,
            CoordinateSize= "7200,4320",
            CoordinateOrigin= "2357,4014"
        };
        group.Append(new O.Lock()
        {
            AspectRatio=true,
            Extension = ExtensionHandlingBehaviorValues.Edit,
        });

        group.AppendChild(oval1);
        group.AppendChild(oval2);
        group.Append(connectorType);
        group.Append(connector);
        picture.AppendChild(group);
        Paragraph paragraphWithPict = new Paragraph();
        Run runwithPicture = new Run();
        runwithPicture.AppendChild(picture);

        paragraphWithPict.AppendChild(runwithPicture);
        body.AppendChild(paragraphWithPict);
        mainDocumentPart.Document.Save();
    }

    <w:p w14:paraId="04F32D7E" w14:textId="15C32339" w:rsidR="0008472A" w:rsidRDefault="009E6587">
      <w:r>
        <w:rPr>
          <w:noProof/>
        </w:rPr>
      </w:r>
      <w:r>
        <w:pict w14:anchorId="078C9A7B">
          <v:group id="_x0000_s1027" editas="canvas" style="width:451.3pt;height:270.8pt;mso-position-horizontal-relative:char;mso-position-vertical-relative:line" coordorigin="2357,4014" coordsize="7200,4320">
            <o:lock v:ext="edit" aspectratio="t"/>
            <v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
              <v:stroke joinstyle="miter"/>
              <v:formulas>
                <v:f eqn="if lineDrawn pixelLineWidth 0"/>
                <v:f eqn="sum @0 1 0"/>
                <v:f eqn="sum 0 0 @1"/>
                <v:f eqn="prod @2 1 2"/>
                <v:f eqn="prod @3 21600 pixelWidth"/>
                <v:f eqn="prod @3 21600 pixelHeight"/>
                <v:f eqn="sum @0 0 1"/>
                <v:f eqn="prod @6 1 2"/>
                <v:f eqn="prod @7 21600 pixelWidth"/>
                <v:f eqn="sum @8 21600 0"/>
                <v:f eqn="prod @7 21600 pixelHeight"/>
                <v:f eqn="sum @10 21600 0"/>
              </v:formulas>
              <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
              <o:lock v:ext="edit" aspectratio="t"/>
            </v:shapetype>
            <v:shape id="_x0000_s1026" type="#_x0000_t75" style="position:absolute;left:2357;top:4014;width:7200;height:4320" o:preferrelative="f">
              <v:fill o:detectmouseclick="t"/>
              <v:path o:extrusionok="t" o:connecttype="none"/>
              <o:lock v:ext="edit" text="t"/>
            </v:shape>
            <v:oval id="_x0000_s1028" style="position:absolute;left:2671;top:5120;width:1766;height:1170"/>
            <v:oval id="_x0000_s1029" style="position:absolute;left:7093;top:5975;width:1961;height:1506"/>
            <v:shapetype id="_x0000_t32" coordsize="21600,21600" o:spt="32" o:oned="t" path="m,l21600,21600e" filled="f">
              <v:path arrowok="t" fillok="f" o:connecttype="none"/>
              <o:lock v:ext="edit" shapetype="t"/>
            </v:shapetype>
            <v:shape id="_x0000_s1031" type="#_x0000_t32" style="position:absolute;left:4437;top:5705;width:2656;height:1023;flip:x y" o:connectortype="straight">
              <v:stroke endarrow="block"/>
            </v:shape>
            <w10:anchorlock/>
          </v:group>
        </w:pict>
      </w:r>
    </w:p>

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
884 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,006 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.