Knowledgebase (2329)
Children categories
Adjusting the column widths in a Word table is crucial for making the document look neat and easy to read. Particularly in tables with a lot of text, appropriate column widths can facilitate smoother reading. Word offers two approaches: percentage-based and fixed widths. Setting column widths by percentage can adapt to various screen sizes, keeping content neatly formatted and more pleasant to read. Using fixed widths stabilizes the table structure, precisely aligning each section, which is especially suitable for tables requiring strict alignment of numbers or complex designs. This article will introduce how to set Word table column widths based on percentage or fixed values using Spire.Doc for Java in Java projects.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.4.9</version>
</dependency>
</dependencies>
Set Column Width Based on Percentage in Java
To set column widths in a Word table using percentage values, you first need to define the table's width type as percentage. This can be achieved with Table.SetPreferredWidth(new PreferredWidth(WidthType.Percentage, (short)100)). Then, iterate through each column and set their widths to either the same or different percentage values. Here are the detailed steps:
- Create a Document object.
- Load a document using the Document.loadFromFile() method.
- Retrieve the first section of the document using Document.getSections().get(0).
- Get the first table within the section using Section.getTables().get(0).
- Use a for loop to iterate through all rows in the table.
- Set the column width for cells in different columns to percentage values using the TableRow.getCells().get(index).setCellWidth(value, CellWidthType.Percentage) method, where value is the percentage width you wish to apply.
- Save the changes to the Word document using the Document.saveToFile() method.
- Java
import com.spire.doc.*;
public class PercentageColumnWidth {
public static void main(String[] args) {
// Create a new Document object
Document doc = new Document();
// Load the document
doc.loadFromFile("Sample.docx");
// Get the first Section of the document
Section section = doc.getSections().get(0);
// Cast the first Table in the Section to a Table type
Table table = section.getTables().get(0);
// Create a PreferredWidth object, set the width type to Percentage, and set the width value to 100%
PreferredWidth percentageWidth = new PreferredWidth(WidthType.Percentage, (short) 100);
// Set the Table's preferred width to the PreferredWidth object created above
table.setPreferredWidth(percentageWidth);
// Define a variable of type TableRow
TableRow tableRow;
// Iterate over all rows in the Table
for (int i = 0; i < table.getRows().getCount(); i++) {
// Get the current row
tableRow = table.getRows().get(i);
// Set the width of the first cell to 34%, type as Percentage
tableRow.getCells().get(0).setCellWidth(34, CellWidthType.Percentage);
// Set the width of the second cell to 33%, type as Percentage
tableRow.getCells().get(1).setCellWidth(33, CellWidthType.Percentage);
// Set the width of the third cell to 33%, type as Percentage
tableRow.getCells().get(2).setCellWidth(33, CellWidthType.Percentage);
}
// Save the modified document, specifying the file format as Docx2016
doc.saveToFile("SetColumnWidthsWithPercentageValues.docx", FileFormat.Docx_2016);
// Close the document
doc.close();
}
}

Set Column Width Based on Fixed Value in Java
When setting column widths in a Word table using fixed values, you first need to set the table to a fixed layout. This is done with Table.getTableFormat().setLayoutType(LayoutType.Fixed), then iterate through each column and set the width to the same or different fixed values as required. Here are the detailed steps:
- Create a Document object.
- Load a document using the Document.loadFromFile() method.
- Retrieve the first section of the document using Document.getSections().get(0).
- Get the first table within the section using Section.getTables().get(0).
- Use a for loop to iterate through all rows in the table.
- Set the column width for cells in different columns to fixed values using the TableRow.getCells().get(index).setCellWidth(value, CellWidthType.Point) method.
- Save the changes to the Word document using the Document.saveToFile() method.
- Java
import com.spire.doc.*;
public class FixedColumnWidth {
public static void main(String[] args) {
// Create a new Document object
Document doc = new Document();
// Load the document
doc.loadFromFile("Sample.docx");
// Get the first Section of the document
Section section = doc.getSections().get(0);
// Cast the first Table in the Section to a Table type
Table table = section.getTables().get(0);
// Set the table layout type to Fixed
table.getFormat().setLayoutType(LayoutType.Fixed);
// Set the table resizing method to not Auto
table.getFormat().setAllowAutoFit(false);
// Get the left margin
float leftMargin = section.getPageSetup().getMargins().getLeft();
// Get the right margin
float rightMargin = section.getPageSetup().getMargins().getRight();
// Calculate the page width minus left and right margins
double pageWidth = section.getPageSetup().getPageSize().getWidth() - leftMargin - rightMargin;
// Define a variable of type TableRow
TableRow tableRow;
// Iterate through all rows in the Table
for (int i = 0; i < table.getRows().getCount(); i++) {
// Get the current row
tableRow = table.getRows().get(i);
// Set the first column cell width to 34% of the page width
tableRow.getCells().get(0).setCellWidth((float) (pageWidth * 0.34), CellWidthType.Point);
// Set the second column cell width to 33% of the page width
tableRow.getCells().get(1).setCellWidth((float) (pageWidth * 0.33), CellWidthType.Point);
// Set the third column cell width to 33% of the page width
tableRow.getCells().get(2).setCellWidth((float) (pageWidth * 0.33), CellWidthType.Point);
}
// Save the modified document, specifying the file format as Docx2016
doc.saveToFile("SetColumnWidthsWithFixedValues.docx", FileFormat.Docx_2016);
// Close the document
doc.close();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Setting the column width in Word tables is crucial for optimizing document readability and aesthetics. Appropriate column widths prevent long lines of text from hindering readability, particularly in text-dense tables. Word offers two approaches: percentages and fixed values. Setting column widths using percentage values allows tables to adapt to different screen sizes, keeping content neatly aligned and enhancing the reading experience. Using fixed values, on the other hand, precisely controls the structure of the table, ensuring consistency and professionalism, making it suitable for designs with strict data alignment requirements or complex layouts. This article will introduce how to set Word table column width based on percentage or fixed value settings using Spire.Doc for .NET in C# projects.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Set Column Width Based on Percentage in C#
When setting column widths in a Word table using percentage values, you first need to set the table's preferred width type to percentage. This is done with Table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100). Then, iterate through each column and set the width to the same or different percentage values as required. Here are the detailed steps:
- Create a Document object.
- Load a document using the Document.LoadFromFile() method.
- Retrieve the first section of the document using Document.Sections[0].
- Get the first table within the section using Section.Tables[0].
- Use a for loop to iterate through all rows in the table.
- Set the column width for cells in different columns to percentage values using the TableRow.Cells[index].SetCellWidth(value, CellWidthType.Percentage) method.
- Save the changes to the Word document using the Document.SaveToFile() method.
- C#
using Spire.Doc;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// Create a new Document object
Document doc = new Document();
// Load the document named "example.docx"
doc.LoadFromFile("Sample.docx");
// Get the first Section of the document
Section section = doc.Sections[0];
// Cast the first Table in the Section to Table type
Table table = (Table)section.Tables[0];
// Create a PreferredWidth object, set the width type to Percentage, and set the width value to 100%
PreferredWidth percentageWidth = new PreferredWidth(WidthType.Percentage, (short)100);
// Set the preferred width of the Table to the PreferredWidth object created above
table.PreferredWidth = percentageWidth;
// Define a variable of type TableRow
TableRow tableRow;
// Iterate through all rows in the Table
for (int i = 0; i < table.Rows.Count; i++)
{
// Get the current row
tableRow = table.Rows[i];
// Set the width of the first column cell to 34%, with the type as Percentage
tableRow.Cells[0].SetCellWidth(34, CellWidthType.Percentage);
// Set the width of the second column cell to 33%, with the type as Percentage
tableRow.Cells[1].SetCellWidth(33, CellWidthType.Percentage);
// Set the width of the third column cell to 33%, with the type as Percentage
tableRow.Cells[2].SetCellWidth(33, CellWidthType.Percentage);
}
// Save the modified document, specifying the file format as Docx2016
doc.SaveToFile("set_column_width_by_percentage.docx", FileFormat.Docx2016);
// Close the document
doc.Close();
}
}
}

Set Column Width Based on Fixed Value in C#
When setting column widths in a Word table using fixed values, you first need to set the table's layout to fixed. This is done with Table.TableFormat.LayoutType = LayoutType.Fixed. Then, iterate through each column and set the width to the same or different fixed values as required. Here are the detailed steps:
- Create a Document object.
- Load a document using the Document.LoadFromFile() method.
- Retrieve the first section of the document using Document.Sections[0].
- Get the first table within the section using Section.Tables[0].
- Use a for loop to iterate through all rows in the table.
- Set the column width for cells in different columns to fixed values using the TableRow.Cells[index].SetCellWidth(value, CellWidthType.Point) method. Note that value should be replaced with the desired width in points.
- Save the changes to the Word document using the Document.SaveToFile() method.
- C#
using Spire.Doc;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// Create a new Document object
Document doc = new Document();
// Load the document
doc.LoadFromFile("Sample.docx");
// Get the first Section of the document
Section section = doc.Sections[0];
// Cast the first Table in the Section to Table type
Table table = (Table)section.Tables[0];
// Set the table layout type to Fixed
table.Format.LayoutType = LayoutType.Fixed;
// Get the left margin
float leftMargin = section.PageSetup.Margins.Left;
// Get the right margin
float rightMargin = section.PageSetup.Margins.Right;
// Calculate the page width minus the left and right margins
double pageWidth = section.PageSetup.PageSize.Width - leftMargin - rightMargin;
// Define a variable of type TableRow
TableRow tableRow;
// Loop through all rows in the Table
for (int i = 0; i < table.Rows.Count; i++)
{
// Get the current row
tableRow = table.Rows[i];
// Set the width of the first column cell to 34% of the page width
tableRow.Cells[0].SetCellWidth((float)(pageWidth * 0.34), CellWidthType.Point);
// Set the width of the second column cell to 33% of the page width
tableRow.Cells[1].SetCellWidth((float)(pageWidth * 0.33), CellWidthType.Point);
// Set the width of the third column cell to 33% of the page width
tableRow.Cells[2].SetCellWidth((float)(pageWidth * 0.33), CellWidthType.Point);
}
// Save the modified document, specifying the file format as Docx2016
doc.SaveToFile("set_column_width_to_fixed_value.docx", FileFormat.Docx2016);
// Close the document
doc.Close();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
The use of online editors is showing a growing trend nowadays, driven by a multitude of factors, including the rise of remote working, the increasing popularity of cloud services, and the convenience that online editors offer in terms of accessibility and collaboration.
Among the rich features offered by online document editors, the security feature is highly valued by some users. Having the ability to password-protect documents online helps prevent unauthorized access, reduces the risk of data leakage, and ensures that only designated members can view or edit documents. This feature is especially important in collaborative environments where you need to control who can make changes.
In this article, we will explore two ways to protect a Word document online in the Spire.Cloud.Office document editor.
Spire.Cloud.Office Document Editor
Spire.Cloud.Office is a powerful online document editing tool designed to provide a seamless experience for editing Microsoft Office documents such as Word, Excel, and PowerPoint in web browsers. The online editing tool supports real-time collaboration, allowing multiple users to work on the same document simultaneously, which is especially useful for teams working on projects together, enabling immediate feedback and updates.
More than just a document editor, Spire.Cloud.Office can also serve as a document viewer and converter, making it a versatile tool for various document management needs. To utilize the services offered by Spire.Cloud.Office, you will need to first install it on your system.
- Install Spire.Cloud.Office for .NET on Windows
- Install Spire.Cloud.Office for Linux on Ubuntu
- Install Spire.Cloud.Office for Linux on CentOS
After the installation is complete, you can integrate Spire.Cloud.Office editor in your own web application or visit the example application hosted on port 3000 to explore the editor's functionalities.

Password Protect a Word Document Online
To encrypt a Word document with password in the Spire.Cloud.Office editor, simply follow the steps below:
1. Upload a Word document by clicking the "Upload File" button on the example page.

2. Select a file, wait a second for the editor to get the file ready, and then click "Edit".

3. After opening the file in the editor, go to "File > Document Protection > Add password".

4. Enter password in the dialogue box that pops up and click "OK" to confirm.

5. The editor will automatically save the encrypted file. Later on, anyone who wants to access the file will need to enter a password.

To change or remove password, open the encrypted document and then go to "File > Document Protection > Change password / Delete password".

Set Editing Restrictions on a Word Document Online
If you want to secure your document against accidental or malicious modifications, you can set editing restrictions on the document. In this way, users are only allowed to edit specified parts of the document, while other parts can only be viewed.
The following are the steps to restrict editing a Word document online using Spire.Cloud.Office editor:
1. When a file is opened in the editor, click the "Restricted Edit" under the "Review" tab.

2. After clicking, a "Restrict Editing" pane will appear on the right side of the document. You can select one from the drop-down menu to allow only this type of editing in the document.

3. If you want to allow certain people to edit a specific part of your document even if it’s been restricted.
First select the part that you want to keep editable, and click the "Add User" under the "Exception (optional)" section. Then enter the usernames (or email addresses) you want to allow to edit your document.

4. Tick-mark the users you just added and click the "Start Protection" button. Then enter password on the pop-up dialogue box and click "OK" to start enforcing protection on your document.

5. The editable parts will be highlighted. To remove editing restrictions on a Word document, click the "Stop Protection" button and enter the password.
