Install FileFormat.Words library to automate the creation & manipulation of Docx/Docs files. Use this open-source API to learn how to align table in Word files.

How to Move a Table in Word Documents Programmatically

Overview

Welcome to another interesting blog post on FileFormat.Words for .NET. There are many articles published on this open-source API that you may find here. This library has the potential to create and manipulate MS Word documents in a C# application. However, we already have covered how to create Tables in Word documents using FileFormat.Words. Therefore, in this guide, we will learn how to move a Table in Word documents using methods and properties exposed by this open-source API. By the end of this blog post, you should have learned how to center table or move to right/left programmatically.

We will cover the following points in this blog post:

FileFormat.Words – Open-source API Installation

Well, the installation process of this open-source API is no more running a single command or downloading a package. So, you may set up FileFormat.Words by downloading the NuGet Package or running the following command in the NuGet Package manager:

Install-Package FileFormat.Words

How to Move a Table in Word Documents – Code Segment

Now, we can start writing code snippets to move a table in Word programmatically. Before going ahead, please visit this guide in which you can find the steps and the code snippet to create a table using this open-source API.

This section will demonstrate the methods to justify table position to center, left/right in a Word document.

  • Create an instance of the TableProperties class.
  • Instantiate an instance of the TableJustification class.
  • Invoke the AlignCneter method to position the table in the center of the document.
  • Call the Append method to attach the tableJustification object to the tblProp object.
  • The AppendChild method will attach the table properties to the table.
// Create an instance of the TableProperties class.
TableProperties tblProp = new TableProperties();
// Instantiate an instance of the TableJustification class.
TableJustification tableJustification = new TableJustification();
// Invoke the AlignCneter method to position the table in center of the document.
tableJustification.AlignCneter();
// Call the Append method to attach the tableJustification object to the tblProp object.
tblProp.Append(tableJustification);
// The AppendChild method will attach the table properties to the table.
table.AppendChild(tblProp);

Insert the above lines of source code into your main file and run the project. Finally, you will see the output in the image below:

how to center table

Likewise, you may invoke AlignLeft and AlignRight methods to change the positions to left or right.

Conclusion

This is the end of this blog post. We hope you have learned how to move a table in Word documents using the C# library. In addition, you have also gone through how to center a table and how to align the table to left/right programmatically. Further, this article can help you if you are looking to build a module that will automate Word file creation & manipulation. So, you may go through the documentation of this open-source API to know other powerful features.

Finally, fileformat.com continues to write blog posts on other topics. Moreover, you can follow us on our social media platforms, including FacebookLinkedIn, and Twitter.

Contribute

Since FileFormat.Words for .NET is an open-source project and is available on GitHub. So, the contribution from the community is much appreciated.

Ask a Question

You can let us know about your questions or queries on our forum.

Frequently Asked Questions – FAQs

How can I move a table in a Word document?

You can do that using methods exposed by FileFormat.Words. So, AlignCneter, AlignLeft and AlignRight methods are used to position the table center/left/right.

How do I automatically adjust tables in Word?

Please visit this link to know the answer in detail.

See Also