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

vijaya kumar

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);
Axel Richter

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");
   }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set table dimensions and spacing in word using apache poi in java

How to put a table inside header of word using java apache poi?

How To Set Excel Default Row Height in Apache POI

Java:using apache POI how to convert ms word file to pdf?

how to add comment to a table of word by apache poi

java Apache POI Word existing table insert row with cell style and formatting

How to insert a table in ms excel using apache java poi

How to Highlight Replaced Word Using Apache POI

Insert a Row in Excel Using Java Apache POI

Using Apache Poi to parse table within a table in a word document

How to remove a row using apache poi

Apache Poi - Java-: How to add text containing blank lines as separate paragraphs to a Word document using Apache POI?

Unexplained spacing created in Word Doc table using Apache POI

Line Spacing Inside the Table in Word Using Apache POI

Insert table into a word document at a specific position using apache poi

How to change the pivot table style from default blue to other colors using apache-poi

How to decrease datatable row height in primefaces

How to add image with border into table cell in Word with Apache POI?

How to change table direction in Apache word poi (XWPF)?

How to increase the height of the excel row using Apache-POI having merged cell value greater than the cell width?

How do I ADD bullet points to a word document using Apache POI in Java

Writing äöü to Word using Apache POI

Write a docx file using Apache POI Word JAVA

how to set specific cell width in different row in apache poi table?

how to get last column value of respective row of excel sheet in java using Apache POI

how to add images side by side word document using apache poi

How to add a hyperlink to image in a Word document using Apache POI?

Hi, how can I restart numbering in java word apache poi?

How to autofit table's row height in MS Word?