H Athukorala
H Athukorala

Reputation: 853

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

I am trying to generate a word document with a table. There is only one page page and it has 5 rows and 2 columns. I am using the letter page and the size is 8.5" x 11". And I gave the margins to the program.

Here is my code,

    XWPFDocument xWPFDocument = new XWPFDocument();

    CTSectPr cTSectPr = xWPFDocument.getDocument().getBody().addNewSectPr();
    CTPageMar cTPageMar = cTSectPr.addNewPgMar();
    cTPageMar.setLeft(BigInteger.valueOf(475));
    cTPageMar.setTop(BigInteger.valueOf(720));
    cTPageMar.setRight(BigInteger.valueOf(446));
    cTPageMar.setBottom(BigInteger.valueOf(605));

    XWPFTable xWPFTable = xWPFDocument.createTable(5, 2);
    xWPFTable.getCTTbl().getTblPr().unsetTblBorders();

    xWPFTable.setTableAlignment(TableRowAlign.CENTER);
    xWPFTable.setWidth("100%");

And I am setting cell width and row height using the following code. But I am not noticing any change.

    XWPFTableRow xWPFTableRow;

    for (int i = 0; i < 5; i++) {
        xWPFTableRow = xWPFTable.getRow(i);

        xWPFTableRow.setHeight(2880);
        xWPFTableRow.getCell(i).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6033));
    }

What I am looking for is how to set Horizontal and Vertical Spacing also is there a way to set Horizontal and Vertical Pitch using Apache POI?

Upvotes: 0

Views: 3141

Answers (1)

Axel Richter
Axel Richter

Reputation: 61852

The current apache poi 4.1.2 provides a method setWidth(java.lang.String widthValue) where widthValue can be a String giving a percentage width in XWPFTable as well as in XWPFTableCell.

Setting the cell spacing is not supported directly until now. So underlying ooxml-schemas classes must be used for this.

Complete example:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

public class CreateWordTableCellSpacing {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The table");

  int cols = 3;
  int rows = 3;
  XWPFTable table = document.createTable(rows, cols);

  table.setWidth("100%");
  table.getRow(0).getCell(0).setWidth("20%");
  table.getRow(0).getCell(1).setWidth("30%");
  table.getRow(0).getCell(2).setWidth("50%");

  //set spacing between cells
  table.getCTTbl()
   .getTblPr()
   .addNewTblCellSpacing()
   .setType(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth.DXA
   );
  table.getCTTbl()
   .getTblPr()
   .getTblCellSpacing()
   .setW(java.math.BigInteger.valueOf(
     180 // 180 TWentieths of an Inch Point (Twips) = 180/20 = 9 pt = 9/72 = 0.125"
   ));

  paragraph = document.createParagraph();

  FileOutputStream out = new FileOutputStream("CreateWordTableCellSpacing.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

enter image description here

Upvotes: 2

Related Questions