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

MTB

I'm trying to use Apache poi word 3.8 to create word document in Persian/Arabic language.

My question is: how to change table direction in document? It means order of column in table(not text direction inside table cell).

For example:

rtl table vs lrt table direction

In MS word we could change table property using:

this option

Axel Richter

If the whole table shall be bidirectional, then the org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr must contain bidiVisual set to ON.

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

public class CreateWordRTLTable {

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

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("Paragraph 1 LTR");

  paragraph = doc.createParagraph();

  //create table:
  XWPFTable table = doc.createTable();
  //set the table itself to bidi visual
  if (table.getCTTbl().getTblPr() == null) {
   table.getCTTbl().addNewTblPr().addNewBidiVisual().setVal(STOnOff.ON);
  } else {
   table.getCTTbl().getTblPr().addNewBidiVisual().setVal(STOnOff.ON);
  }
  //create first row
  XWPFTableRow tableRow = table.getRow(0);
  tableRow.getCell(0).setText("first cell");
  tableRow.addNewTableCell().setText("السلام عليكم");
  tableRow.addNewTableCell().setText("third cell");
  //Each cell contains at least a paragraph. Those can be set to support bidi.
  for (int col = 0 ; col < 3; col++) {
   paragraph = tableRow.getCell(col).getParagraphs().get(0);
   CTP ctp = paragraph.getCTP();
   CTPPr ctppr = ctp.getPPr();
   if (ctppr == null) ctppr = ctp.addNewPPr();
   ctppr.addNewBidi().setVal(STOnOff.ON);
  }

  //create CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //values are in unit twentieths of a point (1/1440 of an inch)
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  for (int col = 1 ; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  }

  //create and set column widths for all columns in all rows
  //most examples don't set the type of the CTTblWidth but this
  //is necessary for working in all office versions
  //values are in unit twentieths of a point (1/1440 of an inch)
  for (int col = 0; col < 3; col++) {
   CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
   tblWidth.setW(BigInteger.valueOf(2*1440));
   tblWidth.setType(STTblWidth.DXA);
   for (int row = 0; row < 1; row++) {
    CTTcPr cttcpr = table.getRow(row).getCell(col).getCTTc().getTcPr();
    if (cttcpr != null) {
     cttcpr.setTcW(tblWidth);
    } else {
     cttcpr = CTTcPr.Factory.newInstance();
     cttcpr.setTcW(tblWidth);
     table.getRow(row).getCell(col).getCTTc().setTcPr(cttcpr);
    }
   }
  }

  paragraph = doc.createParagraph();

  doc.write(new FileOutputStream("WordDocument.docx"));

 }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Apache POI Word XWPF table direction and alignment

how change text direction(not paragraph alignment) in document in apache poi word?(XWPF)

How to set a font family to an entire Word document in Apache POI XWPF

Unable to change orientation of ms-word file(XWPF) using apache poi

Apache POI XWPF : How create a group of line?

Apache Poi - XWPF: How to change font color, size, style of the footer Page Number in docx

How to change a table direction in Microsoft Word?

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

Is it possible to set a default table style in Apache POI XWPF?

Replacing a text in Apache POI XWPF

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

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?

Apache POI XWPF - Check if a run contains a picture

Apache POI XWPF adding shapes to header

Change specific lines in a word file with Apache POI

apache poi add table in word document

How to Highlight Replaced Word Using Apache POI

Apache-poi Java: How can I change the font name and size of a list numbering in a WORD document?

How do I change color of a particular word document using apache poi?

Set image position and tilt into a word .docx document using POI XWPF

Add image into a word .docx document header using POI XWPF

java poi XWPF word - create bookmark in new document

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

How to open and paste a new table everytime in an already existing word document in Apache poi?

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

In Apache POI, Is there a way to access XWPF elements by id their id?

Insert a bulleted list from an ArrayList Apache POI XWPF

How to insert a table into a cell in poi word?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive