<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Documentation – Working with Tables in Java</title>
    <link>https://docs.aspose.com/words/java/working-with-tables/</link>
    <description>Recent content in Working with Tables in Java on Documentation</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    
	  <atom:link href="https://docs.aspose.com/words/java/working-with-tables/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Java: Table Overview</title>
      <link>https://docs.aspose.com/words/java/table-overview/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/table-overview/</guid>
      <description>
        
        
        &lt;p&gt;Aspose.Words is a class library designed for server-side processing of documents in various formats – PDF, HTML, different Microsoft Word formats and others – and supports tables in the following ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;tables in a document are preserved during open/save and conversions&lt;/li&gt;
&lt;li&gt;it is possible to edit table, content, and its formatting, and then export the changes to a file in a format that supports tables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this article, we will learn more about table structure, cells, rows, and columns supported by Aspose.Words, and the details of working with such tables.&lt;/p&gt;



  &lt;h2 id=&#34;table-structure&#34;&gt;
    Table Structure
  &lt;/h2&gt;
&lt;p&gt;As already mentioned, the table consists of such elements as &lt;strong&gt;Cell&lt;/strong&gt;, &lt;strong&gt;Row&lt;/strong&gt; and &lt;strong&gt;Column&lt;/strong&gt;. These are concepts that are common to all tables in general, regardless of the document format.&lt;/p&gt;
&lt;p&gt;This is a common example of a table found in a Microsoft Word document:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;tables-overview-1.png&#34; alt=&#34;tables-overview-aspose-words-java-1&#34;&gt;&lt;/p&gt;



  &lt;h3 id=&#34;table-nodes&#34;&gt;
    Table Nodes
  &lt;/h3&gt;
&lt;p&gt;A table from any document loaded into Aspose.Words is imported as a &lt;strong&gt;Table node&lt;/strong&gt;. The table can be found as a child of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;main text&lt;/li&gt;
&lt;li&gt;an inline story such as a comment or a footnote&lt;/li&gt;
&lt;li&gt;cells when a table is nested within another table&lt;/li&gt;
&lt;/ul&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that tables can be nested inside other tables to any depth.
&lt;/div&gt;




  &lt;h3 id=&#34;table-content&#34;&gt;
    Table Content
  &lt;/h3&gt;
&lt;p&gt;The table node does not contain any real content – instead, it is a container for other such nodes that make up the content:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Table&lt;/strong&gt; contains many &lt;strong&gt;Row&lt;/strong&gt; nodes. The table provides all the usual node elements, allowing you to freely move, modify, and remove the table in the document.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Row&lt;/strong&gt; represents a single table row and contains many &lt;strong&gt;Cell&lt;/strong&gt; nodes. In addition, the &lt;strong&gt;Row&lt;/strong&gt; provides elements that define how the row is displayed, such as height and alignment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cell&lt;/strong&gt; is what contains the true content visible in the table and is made up of a &lt;strong&gt;Paragraph&lt;/strong&gt; and other block level nodes. Additionally, cells can contain nested tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;tables-overview-2.png&#34; alt=&#34;tables-overview-aspose-words-java-2&#34;&gt;&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

You can check the structure of table nodes in a document using the &lt;strong&gt;DocumentExplorer&lt;/strong&gt;.
&lt;/div&gt;




  &lt;h3 id=&#34;empty-paragraph-after-table&#34;&gt;
    Empty Paragraph after Table
  &lt;/h3&gt;
&lt;p&gt;The picture above shows that the document contains a table of several rows, which in turn consists of two cells. Each of the two cells includes a paragraph, which is the container for the cell&amp;rsquo;s formatted text.&lt;/p&gt;
&lt;p&gt;It is also worth noting that separating two consecutive tables in a document requires at least one empty paragraph after the table. Without such a paragraph, consecutive tables would be joined together into one. This behavior is identical in both Microsoft Word and Aspose.Words.&lt;/p&gt;
&lt;p&gt;Aspose.Words has a number of classes related to tables – &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/&#34;&gt;Tables&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/row/&#34;&gt;Rows&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cell/&#34;&gt;Cells&lt;/a&gt;, and others.&lt;/p&gt;



  &lt;h2 id=&#34;see-also&#34;&gt;
    See Also
  &lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://docs.aspose.com/words/words/java/aspose-words-document-object-model/&#34;&gt;Aspose.Words Document Object Model (DOM)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://docs.aspose.com/words/words/java/logical-levels-of-nodes-in-a-document/&#34;&gt;Logical Levels of Nodes in a Document&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Java: How to Create a Table in Java</title>
      <link>https://docs.aspose.com/words/java/create-a-table/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/create-a-table/</guid>
      <description>
        
        
        &lt;p&gt;Aspose.Words allows users to create tables in a document from scratch and provides several different methods for doing so. This article presents details on how to add formatted tables to your document using each method, as well as a comparison of each method at the end of the article.&lt;/p&gt;



  &lt;h2 id=&#34;default-table-styles&#34;&gt;
    Default Table Styles
  &lt;/h2&gt;
&lt;p&gt;The newly created table is given default values similar to those used in Microsoft Word:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&#34;text-align:left&#34;&gt;Table Property&lt;/th&gt;
&lt;th style=&#34;text-align:left&#34;&gt;Default in Aspose.Words&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;Border Style&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;Single&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;Border Width&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;1/2 pt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Border Color&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;Black&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;Left and Right Padding&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;5.4 pts&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;AutoFit Mode&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;AutoFit to Window&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;Allow AutoFit&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;&lt;code&gt;True&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;

&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

A table can be inline if it is tightly positioned, or floating if it can be positioned anywhere on the page. By default, Aspose.Words always creates inline tables.
&lt;/div&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;



  &lt;h2 id=&#34;create-a-table-with-documentbuilder&#34;&gt;
    Create a Table with DocumentBuilder
  &lt;/h2&gt;
&lt;p&gt;In Aspose.Words, users can create a table in a document using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt;. The basic algorithm for creating a table is as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Start the table with &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#startTable&#34;&gt;StartTable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Add a cell to the table using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertCell&#34;&gt;InsertCell&lt;/a&gt; – this automatically starts a new row&lt;/li&gt;
&lt;li&gt;Optionally, use the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#getCellFormat&#34;&gt;CellFormat&lt;/a&gt; property to specify cell formatting&lt;/li&gt;
&lt;li&gt;Insert the cell content using the appropriate &lt;strong&gt;DocumentBuilder&lt;/strong&gt; methods such as &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#writeln&#34;&gt;Writeln&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertImage-byte&#34;&gt;InsertImage&lt;/a&gt;, and others&lt;/li&gt;
&lt;li&gt;Repeat steps 2-4 until the row is complete&lt;/li&gt;
&lt;li&gt;Call &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#endRow&#34;&gt;EndRow&lt;/a&gt; to end the current row&lt;/li&gt;
&lt;li&gt;Optionally, use the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#getRowFormat&#34;&gt;RowFormat&lt;/a&gt; property to specify row formatting&lt;/li&gt;
&lt;li&gt;Repeat steps 2-7 until the table is complete&lt;/li&gt;
&lt;li&gt;Call &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#endTable&#34;&gt;EndTable&lt;/a&gt; to finish building the table&lt;/li&gt;
&lt;/ol&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

&lt;p&gt;Important details:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#startTable&#34;&gt;StartTable&lt;/a&gt; can also be called inside a cell, in which case it starts the creation of a nested table within the cell.&lt;/li&gt;
&lt;li&gt;After calling &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertCell&#34;&gt;InsertCell&lt;/a&gt;, a new cell is created, and any content you add using other methods of the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt; class will be added to the current cell. To create a new cell on the same row, call &lt;strong&gt;InsertCell&lt;/strong&gt; again.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;InsertCell&lt;/strong&gt; is called immediately after &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#endRow&#34;&gt;EndRow&lt;/a&gt; and the end of a row, the table will continue on a new row.&lt;/li&gt;
&lt;li&gt;The &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#endTable&#34;&gt;EndTable&lt;/a&gt; method to end the table should only be called once after calling &lt;strong&gt;EndRow&lt;/strong&gt;. Calling &lt;strong&gt;EndTable&lt;/strong&gt; moves the cursor from the current cell to the position immediately after the table.&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;

&lt;p&gt;The process of creating a table can be clearly seen in the following picture:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;creating-table-process.jpg&#34; alt=&#34;creating-table-process&#34;&gt;&lt;/p&gt;
&lt;p&gt;The following code example shows how to create a simple table using &lt;strong&gt;DocumentBuilder&lt;/strong&gt; with default formatting:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=create-simple-table.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to create a formatted table using DocumentBuilder:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=formatted-table.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to insert a nested table using DocumentBuilder:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=nested-table.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;create-a-table-via-dom-document-object-model&#34;&gt;
    Create a Table via DOM (Document Object Model)
  &lt;/h2&gt;
&lt;p&gt;You can insert tables directly into the DOM by adding a new &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/&#34;&gt;Table&lt;/a&gt; node at a specific position.&lt;/p&gt;
&lt;p&gt;Please note that immediately after the table node creation, the table itself will be completely empty, that is it does not yet contain rows and cells. To insert rows and cells into a table, add the appropriate &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/run/&#34;&gt;Row&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cell/&#34;&gt;Cell&lt;/a&gt; child nodes to the DOM.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

This method of creating a table uses the same table defaults as when using the &lt;strong&gt;DocumentBuilder&lt;/strong&gt;.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to build a new table from scratch by adding the appropriate child nodes to the document tree:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=insert-table-directly.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;create-a-table-from-html&#34;&gt;
    Create a Table from HTML
  &lt;/h2&gt;
&lt;p&gt;Aspose.Words supports inserting content into a document from an HTML source using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertHtml-java.lang.String&#34;&gt;InsertHtml&lt;/a&gt; method. The input can be a full HTML page or just a partial snippet.&lt;/p&gt;
&lt;p&gt;Using this &lt;strong&gt;InsertHtml&lt;/strong&gt; method, users can insert tables into the document via table tags like &lt;code&gt;&amp;lt;table&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;tr&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;td&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The following code example shows how to insert a table into a document from a string containing HTML tags:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=insert-table-from-html.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;insert-a-copy-of-an-existing-table&#34;&gt;
    Insert a Copy of an Existing Table
  &lt;/h2&gt;
&lt;p&gt;There are often times when you need to create a table based on an already existing table in a document. The easiest way to duplicate a table while retaining all formatting is to clone the Table node using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/node/#deepClone-boolean&#34;&gt;deepClone&lt;/a&gt; method.&lt;/p&gt;
&lt;p&gt;The same technique can be used to add copies of an existing row or cell to a table.&lt;/p&gt;
&lt;p&gt;The following code example shows how to duplicate a table using node constructors:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=clone-complete-table.java&#34;&gt;&lt;/script&gt;



&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

You can download the sample file of this example from &lt;a href=&#34;https://github.com/aspose-words/Aspose.Words-for-Java/blob/master/Examples/Data/Tables.docx&#34;&gt;Aspose.Words GitHub&lt;/a&gt;.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to clone the last row of a table and append it to the table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/68616fbf7f092a743b66d4491578d18c.js?file=clone-last-row.java&#34;&gt;&lt;/script&gt;



&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

You can download the sample file of this example from &lt;a href=&#34;https://github.com/aspose-words/Aspose.Words-for-Java/blob/master/Examples/Data/Tables.docx&#34;&gt;Aspose.Words GitHub&lt;/a&gt;.
&lt;/div&gt;

&lt;p&gt;If you are looking at creating tables in a document that grow dynamically with each record from your data source, then the above method is not advised. Instead, the desired output is more easily achieved by using Mail merge with regions. You can learn more about this technique in the &lt;a href=&#34;https://docs.aspose.com/words/words/java/types-of-mail-merge-operations/&#34;&gt;Mail Merge with Regions&lt;/a&gt; section.&lt;/p&gt;



  &lt;h2 id=&#34;compare-ways-to-create-a-table&#34;&gt;
    Compare Ways to Create a Table
  &lt;/h2&gt;
&lt;p&gt;Aspose.Words provides several methods to create new tables in a document. Each method has its own advantages and disadvantages, so the choice of which to use often depends on the specific situation.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take a closer look at these ways of creating tables and compare their pros and cons:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&#34;text-align:left&#34;&gt;Method&lt;/th&gt;
&lt;th style=&#34;text-align:left&#34;&gt;Advantages&lt;/th&gt;
&lt;th style=&#34;text-align:left&#34;&gt;Disadvantages&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Via &lt;code&gt;DocumentBuilder&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;The standard method for inserting tables and other document content&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Sometimes difficult to create many varieties of tables at the same time with the same builder instance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Via DOM&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Fits in better with surrounding code that creates and inserts nodes directly into the DOM without using a &lt;strong&gt;DocumentBuilder&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;The table is created &amp;ldquo;empty&amp;rdquo;: before performing most operations, you must call &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#ensureMinimum&#34;&gt;EnsureMinimum&lt;/a&gt; to create any missing child nodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;From HTML&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Can create a new table from HTML source using tags like &lt;code&gt;&amp;lt;table&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;tr&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;td&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Not all possible Microsoft Word table formats can be applied to HTML&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34;text-align:left&#34;&gt;Cloning an existing table&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;You can create a copy of an existing table while retaining all row and cell formatting&lt;/td&gt;
&lt;td style=&#34;text-align:left&#34;&gt;The appropriate child nodes must be removed before the table is ready for use&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

      </description>
    </item>
    
    <item>
      <title>Java: Work with Columns and Rows in Java</title>
      <link>https://docs.aspose.com/words/java/working-with-columns-and-rows/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/working-with-columns-and-rows/</guid>
      <description>
        
        
        &lt;p&gt;For more control over how tables work, learn how to manipulate columns and rows.&lt;/p&gt;



  &lt;h2 id=&#34;find-the-table-element-index&#34;&gt;
    Find the Table Element Index
  &lt;/h2&gt;
&lt;p&gt;Columns, rows and cells are managed by accessing the selected document node by its index. Finding the index of any node involves gathering all child nodes of the element type from the parent node, and then using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/nodecollection/#indexOf-com.aspose.words.Node&#34;&gt;IndexOf&lt;/a&gt; method to find the index of the desired node in the collection.&lt;/p&gt;



  &lt;h3 id=&#34;find-the-index-of-a-table-in-a-document&#34;&gt;
    Find the Index of a Table in a Document
  &lt;/h3&gt;
&lt;p&gt;Sometimes you may need to make changes to a particular table in a document. To do this, you can refer to a table by its index.&lt;/p&gt;
&lt;p&gt;The following code example shows how to retrieve the index of a table in a document:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=retrieve-table-index.java&#34;&gt;&lt;/script&gt;




  &lt;h3 id=&#34;finding-the-index-of-a-row-in-a-table&#34;&gt;
    Finding the Index of a Row in a Table
  &lt;/h3&gt;
&lt;p&gt;Similarly, you may need to make changes to a specific row in a selected table. To do this, you can also refer to a row by its index.&lt;/p&gt;
&lt;p&gt;The following code example shows how to retrieve the index of a row in a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=retrieve-row-index.java&#34;&gt;&lt;/script&gt;




  &lt;h3 id=&#34;finding-the-index-of-a-cell-in-a-row&#34;&gt;
    Finding the Index of a Cell in a Row
  &lt;/h3&gt;
&lt;p&gt;Finally, you may need to make changes to a specific cell, and you can do this by cell index as well.&lt;/p&gt;
&lt;p&gt;The following code example shows how to retrieve the index of a cell in a row:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=retrieve-cell-index.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;work-with-columns&#34;&gt;
    Work with Columns
  &lt;/h2&gt;
&lt;p&gt;In the Aspose.Words Document Object Model (DOM), the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/&#34;&gt;Table&lt;/a&gt; node consists of &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/row/&#34;&gt;Row&lt;/a&gt; nodes and then &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cell/&#34;&gt;Cell&lt;/a&gt; nodes. Thus, in the &lt;code&gt;Document&lt;/code&gt; Object Model of Aspose.Words, as in Word documents, there is no concept of a column.&lt;/p&gt;
&lt;p&gt;By design, the table rows in Microsoft Word and Aspose.Words are completely independent, and the basic properties and operations are contained only in the rows and cells of the table. This gives tables the ability to have some interesting attributes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each table row can have a completely different number of cells&lt;/li&gt;
&lt;li&gt;Vertically, the cells of each row can have different widths&lt;/li&gt;
&lt;li&gt;It is possible to join tables with different row formats and number of cells&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;working-with-columns-and-rows-1.png&#34; alt=&#34;work-with-columns-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;Any operations performed on columns are actually &amp;ldquo;shortcuts&amp;rdquo; that perform the operation by collectively changing row cells in such a way that it looks like they are being applied to columns. That is, you can perform operations on columns by simply iterating over the same table row cell index.&lt;/p&gt;
&lt;p&gt;The following code example simplifies such operations by proving a facade class that collects the cells that make up a &amp;ldquo;column&amp;rdquo; of a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=column-class.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to insert a blank column into a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=insert-blank-column.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to remove a column from a table in a document:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=remove-column.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;specify-rows-as-header-rows&#34;&gt;
    Specify Rows as Header Rows
  &lt;/h2&gt;
&lt;p&gt;You can choose to repeat the first row in the table as the Header Row only on the first page or on each page if the table is split into several. In Aspose.Words, you can repeat the Header Row on every page using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowformat/#getHeadingFormat&#34;&gt;HeadingFormat&lt;/a&gt; property.&lt;/p&gt;
&lt;p&gt;You can also mark multiple header rows if such rows are located one after the other at the beginning of the table. To do this, you need to apply the &lt;strong&gt;HeadingFormat&lt;/strong&gt; properties to these rows.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that Header Rows do not work in nested tables. That is, if you have a table inside another table, this setting will have no effect. It is a limitation of Microsoft Word that does not allow this, not Aspose.Words.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to build a table which includes Header Rows that repeat on subsequent pages:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=repeat-rows-on-subsequent-pages.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;keep-tables-and-rows-from-breaking-across-pages&#34;&gt;
    Keep Tables and Rows from Breaking Across Pages
  &lt;/h2&gt;
&lt;p&gt;There are times where the contents of a table should not be split across pages. For instance, if a title is above a table, the title and table should always be kept together on the same page to preserve proper appearance.&lt;/p&gt;
&lt;p&gt;There are two separate techniques that are useful to achieve this functionality:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Allow row break across pages&lt;/code&gt;, which is applied to table rows&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Keep with next&lt;/code&gt;, which is applied to paragraphs in table cells&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By default, the above properties are disabled.&lt;/p&gt;
&lt;img src=&#34;keeping-tables-and-rows-from-breaking-across-pages-1.png&#34; alt=&#34;keep-tables-and-rows-from-breaking-across-pages-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;



  &lt;h3 id=&#34;keep-a-row-from-breaking-across-pages&#34;&gt;
    Keep a Row from Breaking Across Pages
  &lt;/h3&gt;
&lt;p&gt;This involves restricting content inside the cells of a row from being split across a page. In Microsoft Word, this can found under Table Properties as the option “Allow row to break across pages”. In Aspose.Words this is found under the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowformat/&#34;&gt;RowFormat&lt;/a&gt; object of a &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/row/&#34;&gt;Row&lt;/a&gt; as the property &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowformat/#getAllowBreakAcrossPages&#34;&gt;RowFormat.AllowBreakAcrossPages&lt;/a&gt;.&lt;/p&gt;
&lt;img src=&#34;keeping-tables-and-rows-from-breaking-across-pages-2.png&#34; alt=&#34;keep-rows-from-breaking-across-pages-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;
&lt;p&gt;The following code example shows how to disable breaking rows across pages for each row in a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=row-format-disable-break-across-pages.java&#34;&gt;&lt;/script&gt;




  &lt;h3 id=&#34;keep-a-table-from-breaking-across-pages&#34;&gt;
    Keep a Table from Breaking Across Pages
  &lt;/h3&gt;
&lt;p&gt;To stop the table from splitting across pages, we need to specify that we want the content contained within the table to stay together.&lt;/p&gt;
&lt;p&gt;To do this, Aspose.Words uses a method, which allows users to select a table and enable the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words.paragraphformat/#getKeepWithNext&#34;&gt;KeepWithNext&lt;/a&gt; parameter to true for each paragraph within the table cells. The exception is the last paragraph in the table, which should be set to false.&lt;/p&gt;
&lt;img src=&#34;keeping-tables-and-rows-from-breaking-across-pages-3.png&#34; alt=&#34;keep-tables-from-breaking-across-pages-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;
&lt;p&gt;The following code example shows how to set a table to stay together on the same page:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/14f5cea1b896ffd04f143627939e0878.js?file=keep-table-together.java&#34;&gt;&lt;/script&gt;

&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I insert a new blank column into an existing table?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Use a helper class that gathers cells by column index, then iterate through each row and insert a new &lt;code&gt;Cell&lt;/code&gt; at the desired position. After insertion, you may need to adjust column widths to maintain layout.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; What is the recommended way to remove a column from a table?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Iterate over all rows of the table and call &lt;code&gt;Row.getCells().removeAt(columnIndex)&lt;/code&gt; for the column you want to delete. Ensure that any merged cells spanning the removed column are handled appropriately.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I make the first row of a table repeat as a header on every page?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Set &lt;code&gt;Row.getRowFormat().setHeadingFormat(true)&lt;/code&gt; on the first row (or on multiple consecutive rows) before saving the document. This marks the rows as header rows that Word repeats on each page.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I prevent a table row from breaking across pages?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Set &lt;code&gt;Row.getRowFormat().setAllowBreakAcrossPages(false)&lt;/code&gt; for the rows you want to keep together. This disables the “Allow row to break across pages” option.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I keep an entire table on the same page without splitting?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; For each paragraph inside the table cells, set &lt;code&gt;Paragraph.getParagraphFormat().setKeepWithNext(true)&lt;/code&gt;, except for the last paragraph in the table where it should be &lt;code&gt;false&lt;/code&gt;. This forces Word to keep the table together when possible.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
    <item>
      <title>Java: Merge Table Cells in in Java</title>
      <link>https://docs.aspose.com/words/java/working-with-merged-cells/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/working-with-merged-cells/</guid>
      <description>
        
        
        &lt;p&gt;Sometimes certain rows in a table require a heading or large blocks of text that take up the full width of the table. For proper design of the table, the user can merge several table cells into one. Aspose.Words supports merged cells when working with all input formats, including importing HTML content.&lt;/p&gt;



  &lt;h2 id=&#34;how-to-merge-table-cells&#34;&gt;
    How to Merge Table Cells
  &lt;/h2&gt;
&lt;p&gt;In Aspose.Words, merged cells are represented by the following properties of the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/&#34;&gt;CellFormat&lt;/a&gt; class:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getHorizontalMerge&#34;&gt;HorizontalMerge&lt;/a&gt; which describes if the cell is a part of a horizontal merge of cells&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getVerticalMerge&#34;&gt;VerticalMerge&lt;/a&gt; which describes if the cell is a part of a vertical merge of cells&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The values of these properties determine the merge behavior of cells:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The first cell in a sequence of merged cells will have &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellmerge/#FIRST&#34;&gt;CellMerge.First&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Any subsequently merged cells will have &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellmerge/#PREVIOUS&#34;&gt;CellMerge.Previous&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;A cell that is not merged will have &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellmerge/#NONE&#34;&gt;CellMerge.None&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;working-with-merged-cells-1.png&#34; alt=&#34;work-with-merged-cells-aspose-words-java&#34;&gt;&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Sometimes, when loading existing document cells in a table, they will appear merged. However, it can actually be one long cell – sometimes Microsoft Word exports merged cells this way. This can be confusing when attempting to work with individual cells, but there doesn&amp;rsquo;t seem to be any particular pattern as to when this happens.
&lt;/div&gt;




  &lt;h2 id=&#34;checking-if-a-cell-is-merged&#34;&gt;
    Checking if a Cell is Merged
  &lt;/h2&gt;
&lt;p&gt;To check if a cell is part of a sequence of merged cells, we simply check the &lt;strong&gt;HorizontalMerge&lt;/strong&gt; and &lt;strong&gt;VerticalMerge&lt;/strong&gt; properties.&lt;/p&gt;
&lt;p&gt;The following code example shows how to print the horizontal and vertical cell merge type:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=check-cells-merged.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;merge-table-cells-when-using-documentbuilder&#34;&gt;
    Merge Table Cells When Using DocumentBuilder
  &lt;/h2&gt;
&lt;p&gt;To merge cells in a table created with the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt;, you need to set the appropriate merge type for each cell where the merge is expected – first &lt;strong&gt;CellMerge.First&lt;/strong&gt; and then &lt;strong&gt;CellMerge.Previous&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Also, you must remember to clear the merge setting for those cells where no merge is required – this can be done by setting the first non-merge cell to &lt;strong&gt;CellMerge.None&lt;/strong&gt;. If this is not done, all cells in the table will be merged.&lt;/p&gt;
&lt;p&gt;The following code example shows how to create a table with two rows where the cells in the first row are merged horizontally:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=horizontal-merge.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to create a two-column table where the cells in the first column are vertically merged:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=vertical-merge.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;merge-table-cells-in-other-cases&#34;&gt;
    Merge Table Cells in Other Cases
  &lt;/h2&gt;
&lt;p&gt;In other situations where the &lt;strong&gt;DocumentBuilder&lt;/strong&gt; is not used, such as in an existing table, merging cells in the previous way may not be as easy. Instead, we can wrap the basic operations involved in applying merge properties to cells in a method that makes the task much easier. This method is similar to the Merge automation method, which is called to merge a range of cells in a table.&lt;/p&gt;
&lt;p&gt;The code below will merge the table cells in the specified range, starting at the given cell and ending at the end cell. In this case, the range can span multiple rows or columns:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=merge-cells.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to merge a range of cells between two specified cells:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=merge-cell-range.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;vertical-and-horizontal-merged-cells-in-html-table&#34;&gt;
    Vertical and Horizontal Merged Cells in HTML Table
  &lt;/h2&gt;
&lt;p&gt;As we have said in previous articles, a table in Microsoft Word is a set of independent rows. Each row has a set of cells that are independent of the cells of other rows. Thus, in the Microsoft Word table there is no such object as a “column”, and “1st column” is something like “the set of the 1st cells of each row in the table”. This allows users to have a table in which, for example, the 1st row consists of two cells – 2cm and 1cm, and the 2nd row consists of two different cells – 1cm and 2cm wide. And Aspose.Words supports this concept of tables.&lt;/p&gt;
&lt;p&gt;A table in HTML has a essentially different structure: each row has the same number of cells and (it is important for the task) each cell has the width of the corresponding column, the same for all cells in one column. So if &lt;strong&gt;HorizontalMerge&lt;/strong&gt; and &lt;strong&gt;VerticalMerge&lt;/strong&gt; return an incorrect value, use the following code example:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=print-horizontal-and-vertical-merged.java&#34;&gt;&lt;/script&gt;

&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=horizontal-and-vertical-merge-helper-classes.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;convert-to-horizontally-merged-cells&#34;&gt;
    Convert to Horizontally Merged Cells
  &lt;/h2&gt;
&lt;p&gt;Sometimes it is not possible to detect which cells are merged because some newer versions of Microsoft Word no longer use the merge flags when cells are merged horizontally. But for situations where cells are merged into a cell horizontally by their width using merge flags, Aspose.Words provides the &lt;code&gt;ConvertToHorizontallyMergedCells&lt;/code&gt; method to convert cells. This method simply transforms the table and adds new cells as needed.&lt;/p&gt;
&lt;p&gt;The following code example shows the above method in operation:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/4fe6fda3615c0c441401e2131533d93b.js?file=convert-to-horizontally-merged-cells.java&#34;&gt;&lt;/script&gt;

&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I merge cells horizontally using &lt;code&gt;DocumentBuilder&lt;/code&gt;?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Create the table with &lt;code&gt;DocumentBuilder&lt;/code&gt;, then for the first cell in the merge set &lt;code&gt;cell.getCellFormat().setHorizontalMerge(CellMerge.First)&lt;/code&gt;. For each subsequent cell that should be merged, set &lt;code&gt;CellMerge.Previous&lt;/code&gt;. After the merged range, set the next cell’s &lt;code&gt;HorizontalMerge&lt;/code&gt; to &lt;code&gt;CellMerge.None&lt;/code&gt; to stop the merge.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I merge cells vertically?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; The process is analogous to horizontal merging but uses the &lt;code&gt;VerticalMerge&lt;/code&gt; property. Set &lt;code&gt;CellMerge.First&lt;/code&gt; on the top‑most cell of the column, &lt;code&gt;CellMerge.Previous&lt;/code&gt; on the cells below, and reset to &lt;code&gt;CellMerge.None&lt;/code&gt; after the merged block.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I determine whether a particular cell is part of a merged group?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Inspect the cell’s format: &lt;code&gt;cell.getCellFormat().getHorizontalMerge()&lt;/code&gt; and &lt;code&gt;cell.getCellFormat().getVerticalMerge()&lt;/code&gt;. If either returns &lt;code&gt;CellMerge.First&lt;/code&gt; or &lt;code&gt;CellMerge.Previous&lt;/code&gt;, the cell participates in a merge; &lt;code&gt;CellMerge.None&lt;/code&gt; means it is not merged.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Is there a way to merge a rectangular range of cells programmatically?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Yes. Loop through the cells in the desired range and set the appropriate &lt;code&gt;HorizontalMerge&lt;/code&gt; and &lt;code&gt;VerticalMerge&lt;/code&gt; values. Typically you set &lt;code&gt;CellMerge.First&lt;/code&gt; on the top‑left cell, &lt;code&gt;CellMerge.Previous&lt;/code&gt; on the remaining cells in the first row for horizontal merges, and similarly use &lt;code&gt;VerticalMerge&lt;/code&gt; for rows. Aspose.Words also provides helper methods (e.g., a custom &lt;code&gt;mergeRange&lt;/code&gt; method) that encapsulate this logic.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; My table appears to have merged cells, but the merge flags are missing. How can I fix this?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Use the &lt;code&gt;Table.convertToHorizontallyMergedCells()&lt;/code&gt; method. It analyses the visual widths of cells, inserts missing cells where needed, and sets the correct &lt;code&gt;HorizontalMerge&lt;/code&gt; flags so that the merged state is represented accurately in the document model. This resolves issues where Word exported a merged cell as a single wide cell without flags.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
    <item>
      <title>Java: Table Position in Java</title>
      <link>https://docs.aspose.com/words/java/position-a-table/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/position-a-table/</guid>
      <description>
        
        
        &lt;p&gt;There are floating tables and inline tables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inline tables&lt;/strong&gt; are placed on the same layer as the text and are placed in a flow of text that only surrounds the table above and below. Inline tables will always appear between the paragraphs where you placed them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Floating tables&lt;/strong&gt; are layered over the text, and the position of the table relative to the paragraph is determined by the table anchor. Because of this, the position of the floating table in the document is affected by the vertical and horizontal positioning settings.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sometimes you need to position a table in a document in a certain way. To do this, you need to use the alignment tools and set the indents between the table and the surrounding text.&lt;/p&gt;
&lt;p&gt;In this article, we will discuss what options Aspose.Words provides for positioning.&lt;/p&gt;



  &lt;h2 id=&#34;specify-inline-table-position&#34;&gt;
    Specify Inline Table Position
  &lt;/h2&gt;
&lt;p&gt;You can set the position of an inline table using the Aspose.Words API and the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAlignment&#34;&gt;Alignment&lt;/a&gt; property. Thus, you can adjust the alignment of the table relative to the document page.&lt;/p&gt;
&lt;p&gt;The following code example shows how to set the position of an inline table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/0f235c484e5edae70a542ebdaae40fd8.js?file=inline-table-position.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;get-floating-table-alignment&#34;&gt;
    Get Floating Table Alignment
  &lt;/h2&gt;
&lt;p&gt;If the table text wrapping is set to &lt;strong&gt;Around&lt;/strong&gt;, you can get the table&amp;rsquo;s horizontal and vertical alignment using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getRelativeHorizontalAlignment&#34;&gt;RelativeHorizontalAlignment&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getRelativeVerticalAlignment&#34;&gt;RelativeVerticalAlignment&lt;/a&gt; properties.&lt;/p&gt;
&lt;p&gt;With &lt;strong&gt;other types of text wrapping&lt;/strong&gt;, you can get inline table alignment using the &lt;strong&gt;Alignment&lt;/strong&gt; property.&lt;/p&gt;
&lt;p&gt;The following code example shows how to get the table&amp;rsquo;s alignment:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/0f235c484e5edae70a542ebdaae40fd8.js?file=get-table-position.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;get-floating-table-position&#34;&gt;
    Get Floating Table Position
  &lt;/h2&gt;
&lt;p&gt;The position of a floating table is determined using the following properties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getHorizontalAnchor&#34;&gt;HorizontalAnchor&lt;/a&gt; – an object for calculating the horizontal positioning of a floating table&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getVerticalAnchor&#34;&gt;VerticalAnchor&lt;/a&gt; – an object for calculating the vertical positioning of a floating table&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAbsoluteHorizontalDistance&#34;&gt;AbsoluteHorizontalDistance&lt;/a&gt; – absolute horizontal floating table position&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAbsoluteVerticalDistance&#34;&gt;AbsoluteVerticalDistance&lt;/a&gt; – absolute vertical floating table position&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAllowOverlap&#34;&gt;AllowOverlap&lt;/a&gt; – option to enable/disable overlap with other floating objects&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getRelativeHorizontalAlignment&#34;&gt;RelativeHorizontalAlignment&lt;/a&gt; –  floating table relative horizontal alignment.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getRelativeVerticalAlignment&#34;&gt;RelativeVerticalAlignment&lt;/a&gt; –  floating table relative vertical alignment.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following code example shows how to get the position of a floating table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/0f235c484e5edae70a542ebdaae40fd8.js?file=get-floating-table-position.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;set-floating-table-position&#34;&gt;
    Set Floating Table Position
  &lt;/h2&gt;
&lt;p&gt;Just like getting, you can set the position of a floating table using the same Aspose.Words API.&lt;/p&gt;
&lt;p&gt;It is important to know that alignment and horizontal and vertical distance are combined properties and one can reset the other. For example, setting the &lt;strong&gt;RelativeHorizontalAlignment&lt;/strong&gt; will reset the &lt;strong&gt;AbsoluteHorizontalDistance&lt;/strong&gt; to its default value and vice versa. The same is true for the vertical arrangement.&lt;/p&gt;
&lt;p&gt;The following code example shows how to set the position of a floating table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/0f235c484e5edae70a542ebdaae40fd8.js?file=floating-table-position.java&#34;&gt;&lt;/script&gt;

&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/0f235c484e5edae70a542ebdaae40fd8.js?file=relative-horizontal-or-vertical-position.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;get-distance-between-table-and-surrounding-text&#34;&gt;
    Get Distance between Table and Surrounding Text
  &lt;/h2&gt;
&lt;p&gt;Aspose.Words also provides an opportunity to find out the distances between tables and surrounding texts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getDistanceTop&#34;&gt;DistanceTop&lt;/a&gt; – the value of the distance from above&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getDistanceBottom&#34;&gt;DistanceBottom&lt;/a&gt; – the value of the distance of perception&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getDistanceRight&#34;&gt;DistanceRight&lt;/a&gt; – distance value on the right&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getDistanceLeft&#34;&gt;DistanceLeft&lt;/a&gt; – distance value on the left&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following code example shows how to get the distance between a table and its surrounding text:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/0f235c484e5edae70a542ebdaae40fd8.js?file=distance-between-table-surrounding-text.java&#34;&gt;&lt;/script&gt;


      </description>
    </item>
    
    <item>
      <title>Java: Work with Text in a Table</title>
      <link>https://docs.aspose.com/words/java/extract-text-from-and-replace-text-in-a-table/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/extract-text-from-and-replace-text-in-a-table/</guid>
      <description>
        
        
        &lt;p&gt;As mentioned in previous articles, a table usually contains plain text, although other content such as images or even other tables can be placed in table cells.&lt;/p&gt;
&lt;p&gt;Adding text or other content to the table is carried out using the appropriate methods of the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt; class and is described in the &lt;strong&gt;&amp;ldquo;Create a Table&amp;rdquo;&lt;/strong&gt; article. In this article, we will talk about how to work with text in an already existing table.&lt;/p&gt;



  &lt;h2 id=&#34;replace-text-in-a-table&#34;&gt;
    Replace Text in a Table
  &lt;/h2&gt;
&lt;p&gt;The table, like any other node in Aspose.Words, has access to the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/range/&#34;&gt;Range&lt;/a&gt; object. Using the table range object, you can replace text in a table.&lt;/p&gt;
&lt;p&gt;The ability to use special characters when replacing is currently supported, so it is possible to replace existing text with multi-paragraph text. To do this, you need to use the special metacharacters described in the corresponding &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/range/#replace-java.lang.String-java.lang.String&#34;&gt;Replace&lt;/a&gt; method.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Typically, text replacement should be done at the cell level (per cell) or at the paragraph level.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to replace all instances of a string of text in the cells of an entire table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/7855fd2588b90f4640bf0540285b5277.js?file=replace-text.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;extract-plain-text-from-a-table&#34;&gt;
    Extract Plain Text from a Table
  &lt;/h2&gt;
&lt;p&gt;Using the &lt;strong&gt;Range&lt;/strong&gt; object, you can also call methods on the entire table range and extract the table as plain text. To do this, use the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/range/#Text&#34;&gt;Text&lt;/a&gt; property.&lt;/p&gt;
&lt;p&gt;The following code example shows how to print the text range of a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/7855fd2588b90f4640bf0540285b5277.js?file=extract-text.java&#34;&gt;&lt;/script&gt;



&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

&lt;p&gt;The same technique is used to extract content from individual table cells only.&lt;/p&gt;
&lt;p&gt;The following code example shows how to print a text range of row and table elements:&lt;/p&gt;

&lt;/div&gt;

&lt;p&gt;The following code example shows how to print the text range of row and table elements.&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/7855fd2588b90f4640bf0540285b5277.js?file=print-text-range-row-and-table.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;working-with-alternative-table-text&#34;&gt;
    Working with Alternative Table Text
  &lt;/h2&gt;
&lt;p&gt;Microsoft Word tables have a &lt;code&gt;table title&lt;/code&gt; and &lt;code&gt;table description&lt;/code&gt; that provide an alternative textual representation of the information contained in the table.&lt;/p&gt;
&lt;p&gt;In Aspose.Words, you can also add a table title and description using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getTitle&#34;&gt;Title&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getDescription&#34;&gt;Description&lt;/a&gt; properties. These properties are meaningful for DOCX documents conforming to the ISO/IEC 29500. When saving in formats earlier than ISO/IEC 29500, these properties are ignored.&lt;/p&gt;
&lt;p&gt;The following code example shows how to set the title and description properties of a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/7855fd2588b90f4640bf0540285b5277.js?file=table-title-and-description.java&#34;&gt;&lt;/script&gt;


      </description>
    </item>
    
    <item>
      <title>Java: Table Formatting in Java</title>
      <link>https://docs.aspose.com/words/java/applying-formatting/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/applying-formatting/</guid>
      <description>
        
        
        &lt;p&gt;Each element of a table can be applied with different formatting. For example, table formatting will be applied to the entire table, row formatting to only particular rows, cell formatting to only certain cells.&lt;/p&gt;
&lt;p&gt;Aspose.Words provides a rich API to retrieve and apply formatting to a table. You can use the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/&#34;&gt;Table&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowformat/&#34;&gt;RowFormat&lt;/a&gt;, and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/&#34;&gt;CellFormat&lt;/a&gt; nodes to set formatting.&lt;/p&gt;
&lt;p&gt;In this article, we will talk about how to apply formatting to different table nodes and what table formatting settings Aspose.Words supports.&lt;/p&gt;



  &lt;h2 id=&#34;apply-formatting-to-different-nodes&#34;&gt;
    Apply Formatting to Different Nodes
  &lt;/h2&gt;
&lt;p&gt;In this section, we will look at applying formatting to various table nodes.&lt;/p&gt;



  &lt;h3 id=&#34;table-level-formatting&#34;&gt;
    Table Level Formatting
  &lt;/h3&gt;
&lt;p&gt;To apply formatting to a table you can use the properties available on the corresponding &lt;strong&gt;Table&lt;/strong&gt; node using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/&#34;&gt;Table&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/&#34;&gt;PreferredWidth&lt;/a&gt;, and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/tablecollection/&#34;&gt;TableCollection&lt;/a&gt; classes.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that the table must have at least one row before the table properties can be applied. This means that when building a table with the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt;, this formatting must be done after the first call to &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertCell()&#34;&gt;InsertCell&lt;/a&gt;, or after the first row is added to the table, or when nodes are inserted directly into the DOM.
&lt;/div&gt;

&lt;p&gt;The pictures below show a representation of the &lt;strong&gt;Table&lt;/strong&gt; formatting features in Microsoft Word and their corresponding properties in Aspose.Words.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-1.png&#34; alt=&#34;apply-formatting-to-table-level-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-2.png&#34; alt=&#34;apply-formatting-to-table-level-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;The following code example shows how to apply an outline border to a table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=apply-outline-border.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to build a table with all borders enabled (grid):&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=build-table-with-borders.java&#34;&gt;&lt;/script&gt;




  &lt;h3 id=&#34;row-level-formatting&#34;&gt;
    Row Level Formatting
  &lt;/h3&gt;
&lt;p&gt;Row-level** formatting can be controlled using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/row/&#34;&gt;Row&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowformat/&#34;&gt;RowFormat&lt;/a&gt;, and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowcollection/&#34;&gt;RowCollection&lt;/a&gt; classes.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that a &lt;strong&gt;Row&lt;/strong&gt; can only be a child node of a &lt;strong&gt;Table&lt;/strong&gt;. At the same time, there must be at least one &lt;strong&gt;Cell&lt;/strong&gt; in the &lt;strong&gt;Row&lt;/strong&gt; so that formatting can be applied to it.
&lt;/div&gt;

&lt;p&gt;The pictures below show a representation of the &lt;strong&gt;Row&lt;/strong&gt; formatting features in Microsoft Word and their corresponding properties in Aspose.Words.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-3.png&#34; alt=&#34;apply-formatting-to-row-level-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;The following code example shows how to modify the table row formatting:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=modify-row-formatting.java&#34;&gt;&lt;/script&gt;




  &lt;h3 id=&#34;cell-level-formatting&#34;&gt;
    Cell Level Formatting
  &lt;/h3&gt;
&lt;p&gt;Cell-level formatting is controlled by the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cell/&#34;&gt;Cell&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/&#34;&gt;CellFormat&lt;/a&gt;, and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellcollection/&#34;&gt;CellCollection&lt;/a&gt; classes.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

&lt;p&gt;Note that a &lt;strong&gt;Cell&lt;/strong&gt; can only be a child node of a &lt;strong&gt;Row&lt;/strong&gt;. At the same time, there must be at least one &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/paragraph/&#34;&gt;Paragraph&lt;/a&gt; in the &lt;strong&gt;Cell&lt;/strong&gt; so that formatting can be applied to it.&lt;/p&gt;
&lt;p&gt;In addition to the &lt;strong&gt;Paragraph&lt;/strong&gt;, you can also insert a &lt;strong&gt;Table&lt;/strong&gt; into a &lt;strong&gt;Cell&lt;/strong&gt;.&lt;/p&gt;

&lt;/div&gt;

&lt;p&gt;The pictures below show a representation of the &lt;strong&gt;Cell&lt;/strong&gt; formatting features in Microsoft Word and their corresponding properties in Aspose.Words.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-4.png&#34; alt=&#34;apply-formatting-to-cell-level-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-5.png&#34; alt=&#34;apply-auto-formatting-to-row-level-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;The following code example shows how to modify the formatting of a table cell:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=modify-cell-formatting.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The following code example shows how to set the amount of space (in points) to add to the left/top/right/bottom of the contents of the cell:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=cell-padding.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;specifying-row-heights&#34;&gt;
    Specifying Row Heights
  &lt;/h2&gt;
&lt;p&gt;The simplest way to set the row height is to use the &lt;strong&gt;DocumentBuilder&lt;/strong&gt;. Using the appropriate &lt;strong&gt;RowFormat&lt;/strong&gt; properties, you can set the default height setting or apply a different height for each row in the table.&lt;/p&gt;
&lt;p&gt;In Aspose.Words, table row height is controlled by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the row height property – &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/row/format#Height&#34;&gt;Height&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;the height rule property for the given row – &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/row/format#HeightRule&#34;&gt;HeightRule&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the same time, a different height can be set for each row – this allows you to widely control the table settings.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Rule options for specifying the height of an object can be set using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/heightrule/&#34;&gt;HeightRule&lt;/a&gt; fields.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to create a table that contains a single cell and apply row formatting:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=apply-row-formatting.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;specify-table-and-cell-widths&#34;&gt;
    Specify Table and Cell Widths
  &lt;/h2&gt;
&lt;p&gt;A table in a Microsoft Word document provides several different ways to resize the table and individual cells. These properties allow considerable control over the appearance and behavior of the table, so that Aspose.Words supports the behavior of tables, as in Microsoft Word.&lt;/p&gt;
&lt;p&gt;It is important to know that table elements present several different properties that can affect how the widths of the overall table, as well as individual cells, is calculated:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preferred width on the table&lt;/li&gt;
&lt;li&gt;Preferred width on individual cells&lt;/li&gt;
&lt;li&gt;Allowing autofit on the table&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article details how the various table width calculation properties work and how to get full control over table width calculation. This is
especially useful to know in such cases where the table layout does not appear as expected.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

&lt;p&gt;In most cases, the preferred cell is recommended than the table width. The preferred cell width is more in line with the DOCX format specification as well as the Aspose.Words model.&lt;/p&gt;
&lt;p&gt;The cell width is actually a calculated value for the DOCX format. The actual cell width can depend on many things. For example, changing the page margins or the preferred table width can affect the actual cell width.&lt;/p&gt;
&lt;p&gt;The preferred cell width is a cell property that is stored in the document. It does not depend on anything and does not change when you change the table or other properties of the cell.&lt;/p&gt;

&lt;/div&gt;



&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

All properties and methods described in this article are related to how tables work in Microsoft Word. So in most cases, if you are building your table programmatically but are finding it hard to create the desired table, you can instead try visually creating it in Microsoft Word first and then simply copy the formatting values into your application.
&lt;/div&gt;




  &lt;h3 id=&#34;how-to-use-preferred-width&#34;&gt;
    How to Use Preferred Width
  &lt;/h3&gt;
&lt;p&gt;The desired width of a table or individual cells is defined through the preferred width property, which is the size that an element strives to fit. That is, the preferred width can be specified  for the entire table or for individual cells. In some situations it may not be possible to fit this width exactly, but the actual width will be close to this value in most cases.&lt;/p&gt;
&lt;p&gt;The appropriate preferred width type and value are set using the methods of the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/&#34;&gt;PreferredWidth&lt;/a&gt; class:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/#AUTO&#34;&gt;Auto&lt;/a&gt; field to specify auto or “no preferred width”&lt;/li&gt;
&lt;li&gt;the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/#fromPercent-double&#34;&gt;FromPercent&lt;/a&gt; method to specify a percentage width&lt;/li&gt;
&lt;li&gt;the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/#fromPoints-double&#34;&gt;FromPoints&lt;/a&gt; method to specify the width in points&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The pictures below show a representation of the &lt;em&gt;preferred width setting features&lt;/em&gt; in Microsoft Word and their corresponding properties in Aspose.Words.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-8.png&#34; alt=&#34;formatting-table-properties-aspose-words-java&#34;&gt;&lt;/p&gt;
&lt;p&gt;An example of how these options are applied to a real table in a document can be seen in the picture below.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-9.png&#34; alt=&#34;table-applied-options-java&#34;&gt;&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Before you can use the preferred width in a table, you must ensure that the table contains at least one row. This is because such table formatting in a Microsoft Word document or in a document created in Aspose.Words is stored in the rows of the table.
&lt;/div&gt;




  &lt;h4 id=&#34;specify-the-preferred-table-or-cell-width&#34;&gt;
    Specify the Preferred Table or Cell Width
  &lt;/h4&gt;
&lt;p&gt;In Aspose.Words, table and cell widths are set using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getPreferredWidth&#34;&gt;Table.PreferredWidth&lt;/a&gt; property and the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getPreferredWidth&#34;&gt;CellFormat.PreferredWidth&lt;/a&gt; property, with options available in the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidthtype/&#34;&gt;PreferredWidthType&lt;/a&gt; enumeration:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Auto&lt;/strong&gt;, which is equivalent to no preferred width set&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Percent&lt;/strong&gt;, which fits the element relative to the available space in the window or container size, and recalculates the value when the available width changes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Points&lt;/strong&gt;, which corresponds to an element of the specified width in points&lt;/li&gt;
&lt;/ul&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

By default, a table can be described as being fitted to 100% of the available space on the page. In this case, this means the table will try to take up the space between the left and right page margins.
&lt;/div&gt;

&lt;p&gt;Using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getPreferredWidth&#34;&gt;Table.PreferredWidth&lt;/a&gt; property will adjust its preferred width relative to its container: page, text column, or outer table cell if it is a nested table.&lt;/p&gt;
&lt;p&gt;The following code example shows how to set the table to auto-fit to 50% of the page width:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=auto-fit-page-width.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;Using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getPreferredWidth&#34;&gt;CellFormat.PreferredWidth&lt;/a&gt; property on a given cell will adjust its preferred width.&lt;/p&gt;
&lt;p&gt;The following code example shows how to set the different preferred width settings:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=preferred-width-settings.java&#34;&gt;&lt;/script&gt;




  &lt;h4 id=&#34;find-preferred-width-type-and-value&#34;&gt;
    Find Preferred Width Type and Value
  &lt;/h4&gt;
&lt;p&gt;You can use the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/#getType&#34;&gt;Type&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidth/#getValue&#34;&gt;Value&lt;/a&gt; properties to find the preferred width details of the desired table or cell.&lt;/p&gt;
&lt;p&gt;The following code example shows how to retrieve the preferred width type of a table cell:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=retrieve-preferred-width-type.java&#34;&gt;&lt;/script&gt;




  &lt;h3 id=&#34;how-to-set-autofit&#34;&gt;
    How to Set Autofit
  &lt;/h3&gt;
&lt;p&gt;The &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAllowAutoFit&#34;&gt;AllowAutoFit&lt;/a&gt; property allows cells in a table to grow and shrink according to a selected criterion. For example, you can use the &lt;strong&gt;AutoFit to Window&lt;/strong&gt; option to fit the table to the width of the page, and the &lt;strong&gt;AutoFit to Content&lt;/strong&gt; option to allow each cell to grow or shrink according to its content.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Additionally, the &lt;strong&gt;AllowAutoFit&lt;/strong&gt; property can be used in conjunction with a preferred cell width to format a cell that automatically fits its content, but also has an initial width. If necessary, the cell width can then grow past this width.
&lt;/div&gt;

&lt;p&gt;By default, Aspose.Words inserts a new table using &lt;strong&gt;AutoFit to Window&lt;/strong&gt;. The table will be sized according to the available page width. To resize a table, you can call the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#autoFit-int&#34;&gt;AutoFit&lt;/a&gt; method. This method accepts an &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/autofitbehavior/&#34;&gt;AutoFitBehavior&lt;/a&gt; enumeration that specifies what type of autofit is applied to the table.&lt;/p&gt;
&lt;p&gt;It is important to know that the autofit method is actually a shortcut that applies different properties to the table at the same time. These are properties that actually give the table the observed behavior. We will discuss these properties for each autofit option.&lt;/p&gt;
&lt;p&gt;The following code example shows how to set a table to shrink or grow each cell according to its contents:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=allow-auto-fit.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;We will use the following table to apply the various auto fit settings as a demonstration.&lt;/p&gt;
&lt;img src=&#34;how-to-apply-different-autofit-settings-to-a-table-1.png&#34; alt=&#34;apply-different-autofit-settings-to-a-table-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;



  &lt;h4 id=&#34;autofit-table-to-window&#34;&gt;
    AutoFit Table to Window
  &lt;/h4&gt;
&lt;p&gt;When autofitting to a window is applied to a table, the following operations are actually performed behind the scenes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;Table.AllowAutoFit&lt;/strong&gt; property is enabled to automatically resize columns to fit the available content, using a &lt;strong&gt;Table.PreferredWidth&lt;/strong&gt; value of 100%&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CellFormat.PreferredWidth&lt;/strong&gt; is removed from all table cells


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that this is slightly different from the Microsoft Word behavior, where each cell&amp;rsquo;s preferred width is set to appropriate values based on their current size and content. Aspose.Words does not update the preferred width so they just get cleared instead.
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Column widths are recalculated for the current table content – the end result is a table that occupies the entire available width&lt;/li&gt;
&lt;li&gt;The width of the columns in the table changes automatically as the user edits the text&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The following code example shows how to autofit a table to the page width:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=auto-fit-table-to-page-width.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;An example of how these options are applied to the above table can be seen in the picture below.&lt;/p&gt;
&lt;img src=&#34;how-to-apply-different-autofit-settings-to-a-table-2.png&#34; alt=&#34;autofit-table-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;



  &lt;h4 id=&#34;autofit-table-to-content&#34;&gt;
    AutoFit Table to Content
  &lt;/h4&gt;
&lt;p&gt;When the table is autofitted the content, the following steps are actually performed behind the scenes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The &lt;strong&gt;Table.AllowAutoFit&lt;/strong&gt; property is enabled to automatically resize each cell according to its content&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The preferred table width is removed from &lt;strong&gt;Table.PreferredWidth&lt;/strong&gt;, &lt;strong&gt;CellFormat.PreferredWidth&lt;/strong&gt; is removed for each table cell


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that this autofit option removes the preferred width from cells, just like in Microsoft Word. If you want to keep the column sizes and increase or decrease the columns to fit the content, you should set the &lt;strong&gt;Table.AllowAutoFit&lt;/strong&gt; property to &lt;strong&gt;True&lt;/strong&gt; on its own rather than using the autofit shortcut.
&lt;/div&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Column widths are recalculated for the current table content – the end result is a table where the column widths and the width of the entire table are automatically resized to best fit the content as the user edits the text&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The following code example shows how to autofit a table to its content:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=auto-fit-table-to-contents.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;An example of how these options are applied to the above table can be seen in the picture below.&lt;/p&gt;
&lt;img src=&#34;how-to-apply-different-autofit-settings-to-a-table-3.png&#34; alt=&#34;resize-column-autofit-settings-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;



  &lt;h4 id=&#34;disable-autofit-in-table-and-use-fixed-column-widths&#34;&gt;
    Disable AutoFit in Table and Use Fixed Column Widths
  &lt;/h4&gt;
&lt;p&gt;If a table has autofit disabled and fixed column widths are used instead, the following steps are performed:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Table.AllowAutoFit&lt;/strong&gt; property is disabled so columns do not grow or shrink to their content&lt;/li&gt;
&lt;li&gt;The preferred width of the entire table is removed from &lt;strong&gt;Table.PreferredWidth&lt;/strong&gt;, &lt;strong&gt;CellFormat.PreferredWidth&lt;/strong&gt; is removed from all table cells&lt;/li&gt;
&lt;li&gt;The end result is a table whose column widths are determined by the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getWidth&#34;&gt;CellFormat.Width&lt;/a&gt; property, and whose columns are not automatically resized when the user enters text or when the page is resized&lt;/li&gt;
&lt;/ol&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that if no width is specified for &lt;strong&gt;CellFormat.Width&lt;/strong&gt;, the default value of one inch (72 points) is used.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to disable autofit and enable fixed width for the specified table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=auto-fit-table-to-fixed-column-widths.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;An example of how these options are applied to the above table can be seen in the picture below.&lt;/p&gt;
&lt;img src=&#34;how-to-apply-different-autofit-settings-to-a-table-4.png&#34; alt=&#34;disable-autofit-settings-to-a-table-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;



  &lt;h3 id=&#34;order-of-precedence-when-calculating-cell-width&#34;&gt;
    Order of Precedence when Calculating Cell Width
  &lt;/h3&gt;
&lt;p&gt;Aspose.Words allows users to define the width of a table or cell through multiple objects, including &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/&#34;&gt;CellFormat&lt;/a&gt; – its &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getWidth&#34;&gt;Width&lt;/a&gt; property is mostly left over from previous versions, however, it is still useful for simplifying setting the cell width.&lt;/p&gt;
&lt;p&gt;It is important to know that the &lt;strong&gt;CellFormat.Width&lt;/strong&gt; property works differently depending on which of the other width properties already exist in the table.&lt;/p&gt;
&lt;p&gt;Aspose.Words uses the following order for calculating cell widths:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order&lt;/th&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAllowAutoFit&#34;&gt;AllowAutoFit&lt;/a&gt; is determined&lt;/td&gt;
&lt;td&gt;If &lt;strong&gt;AutoFit&lt;/strong&gt; is enabled:&lt;br&gt;- the table may grow past the preferred width to accommodate the content – it usually does not shrink below the preferred width&lt;br&gt;- any change to the &lt;strong&gt;CellFormat.Width&lt;/strong&gt; value is ignored and the cell will fit to its contents instead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidthtype/&#34;&gt;PreferredWidthType&lt;/a&gt; with a value of &lt;strong&gt;Points&lt;/strong&gt; or &lt;strong&gt;Percent&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;CellFormat.Width&lt;/strong&gt; is ignored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/preferredwidthtype/&#34;&gt;PreferredWidthType&lt;/a&gt; with a value of &lt;strong&gt;Auto&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The value from &lt;strong&gt;CellFormat.Width&lt;/strong&gt; is copied and becomes the cell&amp;rsquo;s preferred width (in points)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Any changes to the width property are not updated in the preferred width and must instead be applied to the preferred width instead.
&lt;/div&gt;



&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

While creating a fixed table layout, specify the cell width. A cell without width cannot be saved to DOC format. Document formats other than DOC, such as DOCX, allow, in principle, saving cells without width in a fixed table layout.
&lt;/div&gt;




  &lt;h2 id=&#34;allow-spacing-between-cells&#34;&gt;
    Allow Spacing Between Cells
  &lt;/h2&gt;
&lt;p&gt;You can get or set any additional space between table cells similar to the &amp;ldquo;Сell spacing&amp;rdquo; option in Microsoft Word. This can be done using the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getAllowCellSpacing&#34;&gt;AllowCellSpacing&lt;/a&gt; property.&lt;/p&gt;
&lt;p&gt;An example of how these options are applied to a real table in a document can be seen in the picture below.&lt;/p&gt;
&lt;img src=&#34;applying-formatting-10.png&#34; alt=&#34;formatting-spacing-between-cells-aspose-words-java&#34; style=&#34;width:500px&#34;/&gt;
&lt;p&gt;The following code example shows how to set the spacing between cells:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=allow-cell-spacing.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;apply-borders-and-shading&#34;&gt;
    Apply Borders and Shading
  &lt;/h2&gt;
&lt;p&gt;Borders and shading can be applied either to the entire table using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#setBorder-int-int-double-java.awt.Color-boolean&#34;&gt;Table.SetBorder&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#setBorders-int-double-java.awt.Color&#34;&gt;Table.SetBorders&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#setShading-int-java.awt.Color-java.awt.Color&#34;&gt;Table.SetShading&lt;/a&gt;, or only to specific cells using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getBorders&#34;&gt;CellFormat.Borders&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/cellformat/#getShading&#34;&gt;CellFormat.Shading&lt;/a&gt;. Additionally, row borders can be set using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/rowformat/#getBorders&#34;&gt;RowFormat.Borders&lt;/a&gt;, however shading cannot be applied in this way.&lt;/p&gt;
&lt;p&gt;The pictures below show the border and shadow settings in Microsoft Word and their corresponding properties in Aspose.Words.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-6.png&#34; alt=&#34;apply-borders-shading-aspose-words-java-1&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;applying-formatting-to-table-row-and-cell-7.png&#34; alt=&#34;apply-borders-shading-aspose-words-java-2&#34;&gt;&lt;/p&gt;
&lt;p&gt;The following code example shows how to format a table and cell with different borders and shadings:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/1c794bd06eb1e3b67f0368daa6f23b1f.js?file=format-table-and-cell-with-different-borders.java&#34;&gt;&lt;/script&gt;

&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I set a fixed width for a specific table column?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Use the &lt;code&gt;CellFormat.PreferredWidth&lt;/code&gt; property on the target cell and set it with &lt;code&gt;PreferredWidth.fromPoints(double)&lt;/code&gt; or &lt;code&gt;PreferredWidth.fromPercent(double)&lt;/code&gt;. The column will adopt this width unless AutoFit overrides it.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; What is the difference between &lt;code&gt;AllowAutoFit&lt;/code&gt; and &lt;code&gt;PreferredWidth&lt;/code&gt;?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; &lt;code&gt;AllowAutoFit&lt;/code&gt; lets the table automatically resize columns based on content. &lt;code&gt;PreferredWidth&lt;/code&gt; defines an explicit width that the table or cell tries to achieve. If AutoFit is enabled, explicit widths may be ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I add spacing between cells (cell spacing) programmatically?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Set &lt;code&gt;Table.setAllowCellSpacing(true)&lt;/code&gt; and then assign a spacing value with &lt;code&gt;Table.setCellSpacing(double)&lt;/code&gt; (the spacing is measured in points).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I apply different border styles to the first row only?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Access the first &lt;code&gt;Row&lt;/code&gt; object, retrieve its &lt;code&gt;RowFormat&lt;/code&gt;, and use &lt;code&gt;RowFormat.getBorders().setLineStyle(LineStyle.SINGLE)&lt;/code&gt; (or other styles) to customize the borders for that row alone.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I control row height and ensure it is respected?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Use &lt;code&gt;Row.getRowFormat().setHeight(double)&lt;/code&gt; to set the height in points and &lt;code&gt;Row.getRowFormat().setHeightRule(HeightRule.EXACT)&lt;/code&gt; to enforce the exact height, preventing Word from adjusting it automatically.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
    <item>
      <title>Java: Working with Table Styles in Java</title>
      <link>https://docs.aspose.com/words/java/working-with-tablestyle/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/working-with-tablestyle/</guid>
      <description>
        
        
        &lt;p&gt;A table style defines a set of formatting that can be easily applied to a table. Formatting such as borders, shading, alignment, and font can be set in a table style and applied to many tables for a consistent appearance.&lt;/p&gt;
&lt;p&gt;Aspose.Words supports applying a table style to a table and also reading properties of any table style. Table styles are preserved during loading and saving in the following ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Table styles in DOCX and WordML formats are preserved when loading and saving to these formats&lt;/li&gt;
&lt;li&gt;Table styles are preserved when loading and saving in  DOC format (but not to any other format)&lt;/li&gt;
&lt;li&gt;When exporting to other formats, rendering or printing, table styles are expanded to direct formatting in the table, so all formatting is preserved&lt;/li&gt;
&lt;/ul&gt;



  &lt;h2 id=&#34;create-a-table-style&#34;&gt;
    Create a Table Style
  &lt;/h2&gt;
&lt;p&gt;The user can create a new style and add it to style collection. The &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/stylecollection/&#34;&gt;Add&lt;/a&gt; method is used to create a new table style.&lt;/p&gt;
&lt;p&gt;The following code example shows how to create a new user defined table style:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/f1d06175603c48e6dabf5a2eea01207c.js?file=create-table-style.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;copy-an-existing-table-style&#34;&gt;
    Copy an Existing Table Style
  &lt;/h2&gt;
&lt;p&gt;If necessary, you can copy a table style that already exists in a certain document into your style collection using the &lt;code&gt;AddCopy&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;It is important to know that with this copying, the linked styles are also copied.&lt;/p&gt;
&lt;p&gt;The following code example shows how to import a style from one document to another document:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/f1d06175603c48e6dabf5a2eea01207c.js?file=copy-style-different-document.java&#34;&gt;&lt;/script&gt;




  &lt;h2 id=&#34;apply-an-existing-table-style&#34;&gt;
    Apply an Existing Table Style
  &lt;/h2&gt;
&lt;p&gt;Aspose.Words provides a &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/tablestyle/&#34;&gt;TableStyle&lt;/a&gt; inherited from the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/style/&#34;&gt;Style&lt;/a&gt; class. &lt;strong&gt;TableStyle&lt;/strong&gt; facilitates the user to apply different style options like as shading, padding, indentation, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/tablestyle/#getCellSpacing&#34;&gt;CellSpacing&lt;/a&gt; and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/tablestyle/#getFont&#34;&gt;Font&lt;/a&gt;, etc.&lt;/p&gt;
&lt;p&gt;In addition, Aspose.Words provides the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/stylecollection/&#34;&gt;StyleCollection&lt;/a&gt; class and a few properties of the &lt;code&gt;Table&lt;/code&gt; class to specify which table style we will work with: &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getStyle&#34;&gt;Style&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#setStyleIdentifier-int&#34;&gt;StyleIdentifier&lt;/a&gt;, &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getStyleName&#34;&gt;StyleName&lt;/a&gt;, and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#setStyleOptions-int&#34;&gt;StyleOptions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Aspose.Words also provides &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/conditionalstyle/&#34;&gt;ConditionalStyle&lt;/a&gt; class that represents special formatting applied to some area of a table with an assigned table style, and the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/conditionalstylecollection/&#34;&gt;ConditionalStyleCollection&lt;/a&gt; that represents a collection of &lt;strong&gt;ConditionalStyle&lt;/strong&gt; objects. This collection contains a permanent set of items representing one item for each value of the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/conditionalstyletype/&#34;&gt;ConditionalStyleType&lt;/a&gt; enumeration type. The &lt;strong&gt;ConditionalStyleType&lt;/strong&gt; enumeration defines all possible table areas to which conditional formatting may be defined in a table style.&lt;/p&gt;
&lt;p&gt;In this case, conditional formatting can be defined for all possible table area defined under the ConditionalStyleType enumeration type.&lt;/p&gt;
&lt;p&gt;The following code example shows how to define conditional formatting for header row of the table:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/f1d06175603c48e6dabf5a2eea01207c.js?file=define-conditional-formatting.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;You can also choose which table parts to apply styles to, such as first column, last column, banded rows. They are listed in the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/tablestyleoptions/&#34;&gt;TableStyleOptions&lt;/a&gt; enumeration and are applied through the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/table/#getStyleOptions&#34;&gt;StyleOptions&lt;/a&gt; property. The &lt;strong&gt;TableStyleOptions&lt;/strong&gt; enumeration allows a bitwise combination of these features.&lt;/p&gt;
&lt;p&gt;The following code example shows how to create a new table with a table style applied:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/f1d06175603c48e6dabf5a2eea01207c.js?file=build-table-with-style.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The pictures below show a representation of the &lt;strong&gt;Table Styles&lt;/strong&gt; in Microsoft Word and their corresponding properties in Aspose.Words.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;working-with-table-styles-1.png&#34; alt=&#34;table-style-aspose-words-java&#34;&gt;&lt;/p&gt;



  &lt;h2 id=&#34;take-formatting-from-table-style-and-apply-it-as-direct-formatting&#34;&gt;
    Take Formatting from Table Style and Apply it as Direct Formatting
  &lt;/h2&gt;
&lt;p&gt;Aspose.Words also provides the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/document/#expandTableStylesToDirectFormatting&#34;&gt;ExpandTableStylesToDirectFormatting&lt;/a&gt; method to take formatting found on a table style and expands it onto the rows and cells of the table as direct formatting. Try combining formatting with table style and cell style.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

This method will not override any other formatting already applied to the table through a row or cell format.
&lt;/div&gt;

&lt;p&gt;The following code example shows how to expand the formatting from styles onto table rows and cells as direct formatting:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/f1d06175603c48e6dabf5a2eea01207c.js?file=expand-formatting-on-cells-and-row-from-style.java&#34;&gt;&lt;/script&gt;

&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I create a custom table style in Java?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Use the &lt;code&gt;StyleCollection.add&lt;/code&gt; method on the document’s style collection, specifying a name and &lt;code&gt;StyleType.TABLE&lt;/code&gt;. After creating the style, set its properties such as &lt;code&gt;Font&lt;/code&gt;, &lt;code&gt;Shading&lt;/code&gt;, &lt;code&gt;Borders&lt;/code&gt;, and &lt;code&gt;ParagraphFormat&lt;/code&gt;. Finally, add the style to the collection so it can be applied to tables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Can I copy a table style from another document?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Yes. Load the source document, retrieve the desired &lt;code&gt;Style&lt;/code&gt; from its &lt;code&gt;StyleCollection&lt;/code&gt;, and call &lt;code&gt;StyleCollection.addCopy(sourceStyle)&lt;/code&gt; on the target document’s style collection. This copies the style together with any linked styles.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; What is the simplest way to apply an existing table style to a table?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Set the table’s &lt;code&gt;StyleIdentifier&lt;/code&gt;, &lt;code&gt;StyleName&lt;/code&gt;, or &lt;code&gt;Style&lt;/code&gt; property. For example, &lt;code&gt;table.setStyleIdentifier(StyleIdentifier.TABLE_GRID_LIGHT)&lt;/code&gt; or &lt;code&gt;table.setStyleName(&amp;quot;MyCustomStyle&amp;quot;)&lt;/code&gt;. The table will immediately reflect the style’s formatting.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I add conditional formatting (e.g., header row shading) to a table style?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Access the style’s &lt;code&gt;ConditionalStyleCollection&lt;/code&gt; via &lt;code&gt;TableStyle.getConditionalStyles()&lt;/code&gt;. Retrieve the &lt;code&gt;ConditionalStyle&lt;/code&gt; for &lt;code&gt;ConditionalStyleType.HEADER_ROW&lt;/code&gt; and modify its &lt;code&gt;Shading&lt;/code&gt;, &lt;code&gt;Font&lt;/code&gt;, or other properties. The conditional formatting is applied automatically to the matching table area.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I convert a table style into direct formatting on a table’s rows and cells?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Call &lt;code&gt;Document.expandTableStylesToDirectFormatting()&lt;/code&gt; after the table has the style applied. This method expands all style‑based formatting into explicit formatting on each row and cell, preserving the appearance while removing the style reference.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
    <item>
      <title>Java: Join Tables in Java</title>
      <link>https://docs.aspose.com/words/java/join-tables/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/join-tables/</guid>
      <description>
        
        
        &lt;p&gt;A table, represented in the Aspose.Words Document Object Model, is made up of independent rows and cells, making it easy to join tables.&lt;/p&gt;
&lt;p&gt;To manipulate a table to join with another table, we just need to move the rows from the second table to the end of the first one and remove the second table&amp;rsquo;s container.&lt;/p&gt;
&lt;p&gt;The following code example shows how to merge rows from two tables into one:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/89807559333317718ba2e6084fdcb3e2.js?file=combine-rows.java&#34;&gt;&lt;/script&gt;

&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I join two tables in a Word document using Aspose.Words for Java?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Locate the two &lt;code&gt;Table&lt;/code&gt; objects in the document, then move each &lt;code&gt;Row&lt;/code&gt; from the second table into the first using &lt;code&gt;firstTable.appendChild(row.deepClone(true))&lt;/code&gt;. After all rows are transferred, remove the now‑empty second table with &lt;code&gt;secondTable.remove()&lt;/code&gt;. Finally, call &lt;code&gt;doc.updatePageLayout()&lt;/code&gt; to refresh the layout.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How do I split a joined table back into separate tables?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Create a new &lt;code&gt;Table&lt;/code&gt; instance, move the rows you want to separate from the original table into the new one, and insert the new table at the desired position using &lt;code&gt;firstTable.getParentNode().insertAfter(newTable, firstTable)&lt;/code&gt;. Adjust any required formatting on the new table’s rows and cells.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; The joined table loses its original formatting after moving rows. What should I do?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Preserve formatting by cloning the row with &lt;code&gt;row.deepClone(true)&lt;/code&gt;, which copies both content and formatting. After the join, call &lt;code&gt;firstTable.ensureMinimum()&lt;/code&gt; and &lt;code&gt;doc.updatePageLayout()&lt;/code&gt; to make sure the table layout is recalculated and the formatting is applied correctly.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
    <item>
      <title>Java: Split Table in Java</title>
      <link>https://docs.aspose.com/words/java/split-table/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/split-table/</guid>
      <description>
        
        
        &lt;p&gt;A table, represented in the Aspose.Words Document Object Model, is made up of independent rows and cells, making it easy to split a table.&lt;/p&gt;
&lt;p&gt;To manipulate a table to split it into two tables, we just need to move some of the rows from the original table to the new one. To do this, we need to pick the row by which we want to split the table.&lt;/p&gt;
&lt;p&gt;We can create two tables from the original table by following these simple steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a clone of the table without cloning the children to keep the moved rows and insert them after the original table&lt;/li&gt;
&lt;li&gt;Starting at the specified row, move all subsequent rows to this second table&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The following code example shows how to split a table into two tables on a specific row:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/ff5affdcea04dcd20d1b872f9503dbfe.js?file=split-table.java&#34;&gt;&lt;/script&gt;

&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I split a table at a specific row using Aspose.Words for Java?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Load the document, locate the target &lt;code&gt;Table&lt;/code&gt; node, clone the table without its child nodes (&lt;code&gt;Table clonedTable = (Table) originalTable.deepClone(false);&lt;/code&gt;), insert the cloned table after the original, then move each &lt;code&gt;Row&lt;/code&gt; starting from the split row to the cloned table using &lt;code&gt;clonedTable.appendChild(row);&lt;/code&gt;. This creates two independent tables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Does cloning a table also clone its rows and cells?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; By default &lt;code&gt;deepClone(true)&lt;/code&gt; copies the table together with all its rows and cells. To split a table you should use &lt;code&gt;deepClone(false)&lt;/code&gt; so only the table structure is cloned; the rows are then moved manually to the new table.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; After splitting, how do I preserve the original table’s formatting in the new table?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; The cloned table inherits the original table’s formatting (borders, shading, width, etc.) because the clone copies the table’s properties. Since you move existing &lt;code&gt;Row&lt;/code&gt; objects, their individual formatting is retained as well. If you need to adjust spacing, modify &lt;code&gt;Table.setAllowAutoFit(false)&lt;/code&gt; or set &lt;code&gt;Table.getRowFormat().setHeight(...)&lt;/code&gt; on the new table.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Can I split a table without affecting the surrounding document layout (e.g., page breaks or spacing)?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Yes. Insert the cloned table immediately after the original with &lt;code&gt;originalTable.getParentNode().insertAfter(clonedTable, originalTable);&lt;/code&gt;. This keeps the flow of paragraphs unchanged. If a page break is required, insert a &lt;code&gt;Paragraph&lt;/code&gt; with a page break (&lt;code&gt;Paragraph pageBreak = new Paragraph(doc); pageBreak.appendChild(new Break(BreakType.PAGE_BREAK));&lt;/code&gt;) between the two tables.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
    <item>
      <title>Java: How to Build a Table from a DataTable in Java</title>
      <link>https://docs.aspose.com/words/java/how-to-build-a-table-from-a-datatable/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://docs.aspose.com/words/java/how-to-build-a-table-from-a-datatable/</guid>
      <description>
        
        
        &lt;p&gt;Often your application will pull data from a database and store it in the form of a &lt;strong&gt;DataTable&lt;/strong&gt;. You may wish to easily insert this data into your document as a new table and quickly apply formatting to the whole table.&lt;/p&gt;


&lt;div class=&#34;alert alert-primary&#34; role=&#34;alert&#34;&gt;

Note that the preferred way of inserting data from a &lt;strong&gt;DataTable&lt;/strong&gt; into a document table is by using &lt;a href=&#34;https://docs.aspose.com/words/words/java/mail-merge-and-reporting/#aboutmailmerge-mailmergewithregionsexplained&#34;&gt;Mail Merge with Regions&lt;/a&gt;. The technique presented in this article is only suggested if you are unable to create a suitable template beforehand to merge data with, in other words, if you require everything to happen programmatically.
&lt;/div&gt;

&lt;p&gt;Using Aspose.Words, you can easily retrieve data from a database and store it as a table:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt; object on your &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/document/&#34;&gt;Document&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Start a new table using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;If we want to insert the names of each of the columns from our &lt;strong&gt;DataTable&lt;/strong&gt; as a header row then iterate through each data column and write the column names into a row in the table.&lt;/li&gt;
&lt;li&gt;Iterate through each &lt;strong&gt;DataRow&lt;/strong&gt; in the &lt;strong&gt;DataTable&lt;/strong&gt;:
&lt;ol&gt;
&lt;li&gt;Iterate through each object in the &lt;strong&gt;DataRow&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Insert the object into the document using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/&#34;&gt;DocumentBuilder&lt;/a&gt;. The method used depends on the type of the object being inserted e.g &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#writeln()&#34;&gt;DocumentBuilder.writeln()&lt;/a&gt; for text and &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertImage(byte%5B%5D)&#34;&gt;DocumentBuilder.insertImage()&lt;/a&gt; for a byte array which represents an image.&lt;/li&gt;
&lt;li&gt;At the end of processing of the data row also end the row being created by the &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/DocumentBuilder&#34;&gt;DocumentBuilder&lt;/a&gt; by using &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#endRow&#34;&gt;DocumentBuilder.endRow()&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Once all rows from the &lt;strong&gt;DataTable&lt;/strong&gt; have been processed finish the table by calling &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#endTable&#34;&gt;DocumentBuilder.endTable()&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Finally, we can set the desired table style using one of the appropriate table properties such as &lt;a href=&#34;https://reference.aspose.com/words/java/com.aspose.words/tablestyle/#getStyleIdentifier&#34;&gt;Table.getStyleIdentifier()&lt;/a&gt; to automatically apply formatting to the entire table.
The following data in our &lt;strong&gt;DataTable&lt;/strong&gt; is used in this example:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&#34;how-to-build-a-table-from-a-datatable-1.png&#34; alt=&#34;build-a-table-from-a-datatable-aspose-words-java-1&#34;&gt;&lt;/p&gt;
&lt;p&gt;The following code example shows how to execute the above algorithm in Aspose.Words:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/b5330afb035e842063be7ce93cefe219.js?file=import-table-from-data-table.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The method can then be easily called using your &lt;strong&gt;DocumentBuilder&lt;/strong&gt; and data.&lt;/p&gt;
&lt;p&gt;The following code example shows how to import the data from a &lt;code&gt;DataTable&lt;/code&gt; and insert it into a new table in the document:&lt;/p&gt;
&lt;script type=&#34;application/javascript&#34; src=&#34;https://gist.github.com/aspose-words-gists/b5330afb035e842063be7ce93cefe219.js?file=build-table-from-data-table.java&#34;&gt;&lt;/script&gt;

&lt;p&gt;The table shown in the picture below is produced by running the above code.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;how-to-build-a-table-from-a-datatable-2.png&#34; alt=&#34;build-a-table-from-a-datatable-aspose-words-java-2&#34;&gt;&lt;/p&gt;
&lt;hr&gt;



  &lt;h2 id=&#34;faq&#34;&gt;
    FAQ
  &lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How can I insert an image stored as a byte array into a table cell?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Use &lt;code&gt;DocumentBuilder.insertImage(byte[] imageBytes)&lt;/code&gt; while the builder’s cursor is positioned inside the desired cell. After inserting the image you may call &lt;code&gt;DocumentBuilder.endRow()&lt;/code&gt; to continue with the next cell.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; What is the easiest way to apply a predefined table style after the table is built?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Retrieve the &lt;code&gt;Table&lt;/code&gt; node (the first child of the builder’s current paragraph) and call &lt;code&gt;table.setStyleIdentifier(StyleIdentifier.LIGHT_LIST)&lt;/code&gt; (or any other &lt;code&gt;StyleIdentifier&lt;/code&gt;). The style is applied to the whole table instantly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Is there a more automatic approach than writing code for each column when populating a table from a &lt;code&gt;DataTable&lt;/code&gt;?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Yes. Aspose.Words’ &lt;strong&gt;Mail Merge with Regions&lt;/strong&gt; can map a &lt;code&gt;DataTable&lt;/code&gt; directly to a table in a template. Create a region in the template (&lt;code&gt;&amp;lt;&amp;lt;TableStart:Data&amp;gt;&amp;gt; … &amp;lt;&amp;lt;TableEnd:Data&amp;gt;&amp;gt;&lt;/code&gt;) and call &lt;code&gt;document.getMailMerge().executeWithRegions(dataTable)&lt;/code&gt;. This eliminates manual iteration.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; How should I handle &lt;code&gt;null&lt;/code&gt; values in the &lt;code&gt;DataTable&lt;/code&gt; while building the table?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Check each cell value before writing. If the value is &lt;code&gt;null&lt;/code&gt;, write an empty string or a placeholder, e.g. &lt;code&gt;builder.writeln(&amp;quot;&amp;quot;)&lt;/code&gt;. This prevents &lt;code&gt;NullPointerException&lt;/code&gt; and keeps the table layout consistent.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Can I merge cells or set column widths after the table has been created?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Yes. After &lt;code&gt;endTable()&lt;/code&gt;, locate the &lt;code&gt;Table&lt;/code&gt; node, then use &lt;code&gt;Cell.merge(Cell other)&lt;/code&gt; to merge cells horizontally or vertically. Column widths can be set via &lt;code&gt;Table.getFirstRow().getCells().get(i).getCellFormat().setWidth(width)&lt;/code&gt;. Adjust widths before saving the document.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
  </channel>
</rss>
