How to align the shape on word document in C#

2018-05-17 08:06:27 Written by  support iceblue
Rate this item
(0 votes)

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:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AlignShape
{
    class Program
    {
        static void Main(string[] args)
        {
            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);
        }
    }
}

Additional Info

  • tutorial_title: Align the shape on word document in C#
Last modified on Friday, 03 September 2021 03:47