Search code examples
apache-poiapache-poi-4

Need to shift the table to right in apache poi word


I have created the table in apache POI, need to create another similar table but shifted right: Created table with Apache POI

But I want to create similar another table to the right but at bottom. enter image description here

Do we have a setting in XWPFTable to set the alignment for the table to shift right or Do i need to re-build the entire table again

Code snippet

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.math.BigInteger;

public class WordBuilder {

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

        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("The table:");
        createNonGroupedSubTacTable(document);
        CTSectPr sectPr = document.getDocument().getBody().getSectPr();
        if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageSz pageSz = sectPr.addNewPgSz();
        pageSz.setOrient(STPageOrientation.PORTRAIT);
        pageSz.setW(BigInteger.valueOf(11900)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
        pageSz.setH(BigInteger.valueOf(16840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"

        FileOutputStream out = new FileOutputStream("example_docx");
        document.write(out);
        out.close();
    }

    private static void createNonGroupedSubTacTable(XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        XWPFTable table = document.createTable(10, 8);
        setTableAlign(table);
        int defaultColWidth = 1 * 1600 * 5 / 8; // 8 columns fits to 8 inches
        int[] colunmWidths = new int[]{
                defaultColWidth * 3 / 4, defaultColWidth * 8 / 4, defaultColWidth * 2 / 4, defaultColWidth * 3 / 4,
                defaultColWidth * 3 / 4, defaultColWidth * 2 / 4, defaultColWidth * 8 / 4, defaultColWidth * 3 / 4
        };
        table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
        setColumnWidth(table, 0, 0, colunmWidths[0]);
        //other columns
        for (int col = 1; col < colunmWidths.length; col++) {
            table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
            setColumnWidth(table, 0, col, colunmWidths[col]);
        }
        //set cell borders
        for (int col = 0; col < 3; col++) {
            setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        }
        setCellBorders(table.getRow(0).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.THICK, STBorder.NIL});
        setCellBorders(table.getRow(0).getCell(4), new STBorder.Enum[]{STBorder.THICK, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        for (int col = 5; col < 8; col++) {
            setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        }
        for (int col = 0; col < 3; col++) {
            setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        }
        setCellBorders(table.getRow(1).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.THICK, STBorder.THICK});
        setCellBorders(table.getRow(1).getCell(4), new STBorder.Enum[]{STBorder.THICK, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        for (int col = 5; col < 8; col++) {
            setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        }
        table.getRow(0).setHeight(28 * 20); // 28pt row height
        table.getRow(0).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

        XWPFTableCell cell = table.getRow(0).getCell(7);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText("Species:");
        cell = table.getRow(0).getCell(6);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setAlignment(ParagraphAlignment.RIGHT);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText("Greater silver smelt");

        cell = table.getRow(0).getCell(3);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Zone:");

        cell = table.getRow(0).getCell(1);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Union and international waters of 1 and 2 (2)");

        table.getRow(1).setHeight(28 * 20); // 28pt row height
        table.getRow(1).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

        cell = table.getRow(1).getCell(6);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        paragraph.setAlignment(ParagraphAlignment.RIGHT);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.addBreak();
        run = paragraph.createRun();
        run.setText("Argentinia silos");

        cell = table.getRow(1).getCell(1);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.addTab();
        run = paragraph.createRun();
        run.addTab();
        run = paragraph.createRun();
        run.addBreak();
        run = paragraph.createRun();
        run.setText("(ARU/1/2)");
        for (int row = 2; row < 6; row++) {
            for (int col = 5; col < 8; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            setCellBorders(table.getRow(row).getCell(6), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            for (int col = 0; col < 5; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(7);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setFontSize(10);
            run.setText("Belgium");

            cell = table.getRow(row).getCell(6);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            paragraph.setAlignment(ParagraphAlignment.RIGHT);
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("6  ");
            run = paragraph.createRun();
            run.setText("(2)");
            run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
        }

        mergeCellHorizontally(table, 2, 0, 3);
        cell = table.getRow(2).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Analytical TAC");

        mergeCellHorizontally(table, 3, 0, 3);
        cell = table.getRow(3).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Article 3 of Regulation (EC) No 847/96 shall not apply");

        mergeCellHorizontally(table, 4, 0, 3);
        cell = table.getRow(4).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(10);
        run.setText("Article 4 of Regulation (EC) No 847/96 shall not apply.");

        for (int col = 0; col < 8; col++) {
            setCellBorders(table.getRow(6).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
        }

        for (int row = 7; row < 8; row++) {
            for (int col = 5; col < 8; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            setCellBorders(table.getRow(row).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            setCellBorders(table.getRow(row).getCell(4), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            for (int col = 0; col < 3; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(7);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("TAC");

            cell = table.getRow(row).getCell(6);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            paragraph.setAlignment(ParagraphAlignment.RIGHT);
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run = paragraph.createRun();
            run.setText("10");
            run = paragraph.createRun();
            run.setText("(2)");
            run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
        }
        for (int row = 8; row < 10; row++) {
            if (row == 9) {
                for (int col = 0; col < 3; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                }
                setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                setCellBorders(table.getRow(row).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                for (int col = 4; col < 8; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                }
            } else {
                for (int col = 0; col < 3; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                }
                setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                setCellBorders(table.getRow(row).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                for (int col = 4; col < 8; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                }

            }
            XWPFTableCell cell1 = table.getRow(row).getCell(7);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("1");


            mergeCellHorizontally(table, row, 0, 5);
            cell = table.getRow(row).getCell(0);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run = paragraph.createRun();
            run.setFontSize(9);
            run.setText("Up to 2 % of the quota may consist of by-catches of whiting and mackerel (OT1/*2A3A4). " +
                    "By-catches of whiting and mackerel counted against the quota pursuant to this provision and by-catches of species counted " +
                    "against the quota pursuant to Article 15(8) of Regulation (EU) No 1380/2013 shall, together, not exceed 9% of the quota.");

        }

    }

    private static void setColumnWidth(XWPFTable table, int row, int col, int width) {
        CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
        tblWidth.setW(BigInteger.valueOf(width));
        tblWidth.setType(STTblWidth.DXA);
        CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setTcW(tblWidth);
        } else {
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setTcW(tblWidth);
            table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
        }
    }

    private static void setCellBorders(XWPFTableCell cell, STBorder.Enum[] borderTypesLTRB) {
        CTTcBorders borders = CTTcBorders.Factory.newInstance();
        borders.addNewLeft().setVal(borderTypesLTRB[0]);
        borders.addNewTop().setVal(borderTypesLTRB[1]);
        borders.addNewRight().setVal(borderTypesLTRB[2]);
        borders.addNewBottom().setVal(borderTypesLTRB[3]);
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setTcBorders(borders);
        } else {
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setTcBorders(borders);
            cell.getCTTc().setTcPr(tcPr);
        }
    }

    private static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) {
        XWPFTableCell cell = table.getRow(row).getCell(fromCol);
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr == null) tcPr = cell.getCTTc().addNewTcPr();
        if (tcPr.isSetGridSpan()) {
            tcPr.getGridSpan().setVal(BigInteger.valueOf(toCol - fromCol + 1));
        } else {
            tcPr.addNewGridSpan().setVal(BigInteger.valueOf(toCol - fromCol + 1));
        }
        for (int colIndex = toCol; colIndex > fromCol; colIndex--) {
            table.getRow(row).getCtRow().removeTc(colIndex);
            table.getRow(row).removeCell(colIndex);
        }
    }

    private static void setTableAlign(XWPFTable table) {
        CTTbl cttblp = table.getCTTbl();
        CTTblPr cttblpr;
        cttblpr = (cttblp.getTblPr() == null ? cttblp.addNewTblPr() : cttblp.getTblPr());
        cttblpr.addNewBidiVisual().setVal(STOnOff.ON);
        CTJc ctjc = (cttblpr.isSetJc() ? cttblpr.getJc() : cttblpr.addNewJc());
        ctjc.setVal(STJc.LEFT);
    }
}

Solution

  • You can indent the table using the low level org.openxmlformats.schemas.wordprocessingml.x2006.main.* classes.

    XWPFTable table = document.createTable(3,3);
    // set indentation of the table
    CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
    tableIndentation.setW(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
    tableIndentation.setType(STTblWidth.DXA);
    

    But there also is XWPFTable.setTableAlignment. Using that you can align the whole table.

    XWPFTable table = document.createTable(3,3);
    table.setTableAlignment(TableRowAlign.RIGHT); //table right aligned
    

    For apache poi versions prior 4.1.1 the following low level approach can be used to right align the table:

    XWPFTable  table = document.createTable(3,3);
    table.getCTTbl().getTblPr().addNewJc().setVal(STJc.RIGHT);  //table right aligned
    

    Complete example:

    import java.io.FileOutputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
    
    import java.math.BigInteger;
    
    public class CreateWordTableIndent {
    
     public static void main(String[] args) throws Exception {
    
      XWPFDocument document = new XWPFDocument();
    
      XWPFParagraph paragraph = document.createParagraph();
      XWPFRun run = paragraph.createRun();  
      run.setText("The table indented:");
      XWPFTable table = document.createTable(3,3);
      // set indentation of the table
      CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
      tableIndentation.setW(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
      tableIndentation.setType(STTblWidth.DXA);
      for (int row = 0; row < 3; row++) {
       for (int col = 0; col < 3; col++) {
        table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
       }
      }
      //create CTTblGrid for this table with widths of the 3 columns. 
      //necessary for Libreoffice/Openoffice to accept the column widths.
      //first column
      table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
      //other columns
      for (int col = 1; col < 3; col++) {
       table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
      }
      paragraph = document.createParagraph();
    
      paragraph = document.createParagraph();
      run = paragraph.createRun();  
      run.setText("The table right aligned:");
      table = document.createTable(3,3);
      table.setTableAlignment(TableRowAlign.RIGHT); //table right aligned
      for (int row = 0; row < 3; row++) {
       for (int col = 0; col < 3; col++) {
        table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
       }
      }
      //create CTTblGrid for this table with widths of the 3 columns. 
      //necessary for Libreoffice/Openoffice to accept the column widths.
      //first column
      table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
      //other columns
      for (int col = 1; col < 3; col++) {
       table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
      }
      paragraph = document.createParagraph();
    
    
      FileOutputStream out = new FileOutputStream("CreateWordTableIndent.docx");
      document.write(out);
      out.close();
      document.close();
    
     }
    }
    

    Result:

    enter image description here