title: Apache POI - How to copy tables from one docx to another docx
body: <p>Hi I am trying to copy a table from a docx file to another but what happens is that the value of the table are copied down bellow the table in the new document and outside of it (see pictures bellow)</p>
  
  <p>Table in original docx
  <a href="https://i.stack.imgur.com/qceiV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qceiV.png" alt="enter image description here"></a></p>
  
  <p>Talbe in the new docx</p>
  
  <p><a href="https://i.stack.imgur.com/upADQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/upADQ.png" alt="enter image description here"></a></p>
  
  <p>As you can see the values of the table are copied outside the table.
  I am using Libre Office, apache poi version 3.17 and my computer runs Ubuntu 16.04</p>
  
  <p>The code I am using to perform the copy is the following</p>
  
  <pre><code>public static void copyTable(XWPFDocument input_doc,XWPFDocument output_doc,
                               int table_index_input, int table_index_output) {
  
      XWPFTable template_table = input_doc.getTables().get(table_index_input);
  
      CTTbl ctTbl = CTTbl.Factory.newInstance(); // Create a new CTTbl for the new table
      ctTbl.set(template_table.getCTTbl()); // Copy the template table's CTTbl
      XWPFTable new_table = new XWPFTable(ctTbl, output_doc); // Create a new table using the CTTbl upon
  
      output_doc.createParagraph();
      output_doc.createTable();// Create a empty table in the document
      output_doc.setTable(table_index_output, new_table);  // Replace the empty table to table2
  
  }
  </code></pre>