Modestas Bujanauskas
Modestas Bujanauskas

Reputation: 11

Text direction in XWPFTable

How to rotate text using Apache POI in XWPFTable to 90 degrees?

So it will look like this

Upvotes: 1

Views: 1087

Answers (2)

Rahul khanvani
Rahul khanvani

Reputation: 401

You can use this method to set the various ENUM alighments form apache poi liberary. STJc.Enum supports various kind of alignments.

public void setTableAlignment(XWPFTable table, STJc.Enum justification) {
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc());
    jc.setVal(justification);
}

Upvotes: 0

Axel Richter
Axel Richter

Reputation: 61852

The text direction settings are not implemented into XWPFTableCell until now. But using getCTTc we can get the underlaying CTTc object. And from this we can set addNewTcPr(), addNewTextDirection().

For using org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTextDirection this example needs the full jar of all of the schemas ooxml-schemas-1.3.jar as mentioned in the 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.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextDirection;

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

  XWPFDocument document = new XWPFDocument();

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

  XWPFTable table = document.createTable(1,3);
  for (int r = 0; r < 1; r++) {
   for (int c = 0 ; c < 3; c++) {
    XWPFTableCell tableCell = table.getRow(r).getCell(c);
    tableCell.getCTTc().addNewTcPr().addNewTextDirection().setVal(STTextDirection.BT_LR);
    paragraph = tableCell.getParagraphArray(0);
    run = paragraph.createRun();  
    run.setText("text");
   }
  }

  paragraph = document.createParagraph();

  document.write(new FileOutputStream("CreateWordTableTextVertical.docx"));
  document.close();

 }
}

Upvotes: 5

Related Questions