How to align the shape on word document in C#

Spire.Doc offers the property ShapeObject. HorizontalAlignment and ShapeObject. Vertical Alignment to enable developers to align the shapes horizontally or vertically. This article will show you to how to align the shapes on the word document in C#.

Step 1: Create a new instance of word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Get the first section from file.

Section section = doc.Sections[0];

Step 3: Traverse the word document and set the horizontal alignment as left.

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        if (obj is ShapeObject)
        {
          (obj as ShapeObject).HorizontalAlignment = ShapeHorizontalAlignment.Left;

        }
    }
}

Step 4: Save the document to file.

doc.SaveToFile("Result.docx", FileFormat.Docx);

Effective screenshot after setting the alignment of the shapes.

How to align the shape on word document in C#

How to align the shape on word document in C#

Full codes:

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Section section = doc.Sections[0];

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        if (obj is ShapeObject)
        {
            (obj as ShapeObject).HorizontalAlignment = ShapeHorizontalAlignment.Left;

            ////Set the vertical alignment as top
            //(obj as ShapeObject).VerticalAlignment = ShapeVerticalAlignment.Top;
        }
    }
}

doc.SaveToFile("Result.docx", FileFormat.Docx2013);