Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease default height of a table row in word using apache poi in java

I am using Apache Poi for creating word, I cant able to decrease row height. I found two methods for setting height but both are not working. i used the following snippets.

int nRows2 = 6;
int nCols2 = 3;
XWPFTable table2 = doc.createTable(nRows2, nCols2);

CTTblWidth width2 = table2.getCTTbl().addNewTblPr().addNewTblW();
width2.setType(STTblWidth.DXA);
width2.setW(BigInteger.valueOf(13000));

XWPFTableRow testingrow = table2.getRow(0);

CTTblPr testingTblPr = table2.getCTTbl().getTblPr();
CTString sstyleStr = testingTblPr.addNewTblStyle();
sstyleStr.setVal("StyledTable");


CTTrPr trPr2 = testingrow.getCtRow().addNewTrPr();
CTHeight ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf(2));
  System.out.println("height is "+testingrow.getHeight());
//tableRowOne.setHeight(0);
  testingrow.getCell(0).setText("vijay ");
  testingrow.getCell(0).setColor("123456");

//  Second method is just setting height from row object 
testingrow.setHeight(2);
like image 583
vijaya kumar Avatar asked Apr 11 '16 10:04

vijaya kumar


2 Answers

The XWPFTableRow.setHeight(int height) https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFTableRow.html#setHeight%28int%29 works for me.

The height must be set in Twips (Twentieth of an Inch Point).

But if you want to decrease the row height below the default line height, which depends on the font size, then you must set w:hRule="exact". This is only possible using the underlying objects and having the ooxml-schemas-1.3.jar in class path as mentioned in https://poi.apache.org/faq.html#faq-N10025.

Example:

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
/*
To
org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
the fully ooxml-schemas-1.3.jar is needed as mentioned in https://poi.apache.org/faq.html#faq-N10025
*/

public class CreateTable 
{
   public static void main(String[] args)throws Exception 
   {
   //Blank Document
   XWPFDocument document= new XWPFDocument();

   //Write the Document in file system
   FileOutputStream out = new FileOutputStream(
   new File("create_table.docx"));

   //create table
   XWPFTable table = document.createTable();
   //create first row
   XWPFTableRow tableRowOne = table.getRow(0);
   tableRowOne.getCell(0).setText("col one, row one");
   tableRowOne.addNewTableCell().setText("col two, row one");
   tableRowOne.addNewTableCell().setText("col three, row one");
   //create second row
   XWPFTableRow tableRowTwo = table.createRow();
   tableRowTwo.getCell(0).setText("col one, row two");
   tableRowTwo.getCell(1).setText("col two, row two");
   tableRowTwo.getCell(2).setText("col three, row two");

int twipsPerInch =  1440;
tableRowTwo.setHeight((int)(twipsPerInch*1/10)); //set height 1/10 inch.
tableRowTwo.getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT); //set w:hRule="exact"

   //create third row
   XWPFTableRow tableRowThree = table.createRow();
   tableRowThree.getCell(0).setText("col one, row three");
   tableRowThree.getCell(1).setText("col two, row three");
   tableRowThree.getCell(2).setText("col three, row three");

twipsPerInch =  1440;
tableRowThree.setHeight(twipsPerInch*1); //set height 1 inch.

   document.write(out);
   out.close();
   System.out.println("create_table.docx written successully");
   }
}
like image 177
Axel Richter Avatar answered Oct 30 '22 15:10

Axel Richter


To supplement the answer by Axel Richter, if you want to get rid of the paragraph white space in the cells so that the text doesn't run out of room within the cell, simply remove the paragraph within the cell and create your own like this:

XWPFTable table= document.createTable();
XWPFTableRow tableRow = table.getRow(0);
tableRow.addNewTableCell();
tableRow.getCell(0).removeParagraph(0);
XWPFParagraph paragraph = tableRow.getCell(0).addParagraph();
like image 40
Phil H. Avatar answered Oct 30 '22 15:10

Phil H.