需要在 apache poi word 中将 table 右移

Need to shift the table to right in apache poi word

我在 apache POI 中创建了 table,需要创建另一个类似的 table 但向右移动:

但我想在右侧但在底部创建另一个类似的 table。

我们在 XWPFTable 中是否有一个设置来将 table 的对齐方式设置为右移,或者我是否需要重新构建整个 table

代码片段

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

您可以使用低级别缩进 table org.openxmlformats.schemas.wordprocessingml.x2006.main.* 类。

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

但是还有XWPFTable.setTableAlignment。使用它你可以对齐整个 table.

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

对于 4.1.1 之前的 apache poi 版本,可以使用以下低级方法右对齐 table:

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

完整示例:

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();

 }
}

结果:

此答案是关于您提供的完整示例的问题:

第一:不推荐使用双向文本设置对齐 table 以从右到左的方向显示。这只应用于真正需要双向文本的语言,例如希伯来语或阿拉伯语。所以你的 table 对齐应该是这样的:

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

其次:合并单元格时,需要设置新合并单元格的列宽。否则 Word 可能会使用各种不需要的自动操作来确定列宽。例如:

...
mergeCellHorizontally(table, 2, 4, 7);
setColumnWidth(table, 2, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
...

...
mergeCellHorizontally(table, row, 1, 7);
setColumnWidth(table, row, 1, colunmWidths[1]+colunmWidths[2]+colunmWidths[3]+colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
...

根据您提供的屏幕截图创建右对齐 table 的完整示例:

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

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();
        document.close();
    }

    private static void createNonGroupedSubTacTable(XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        XWPFTable table = document.createTable(10, 8);

        setTableAlign(table, STJc.RIGHT);

        int defaultColWidth = 1 * 1600 * 5 / 8; // 8 columns fits to 8 inches
        int[] colunmWidths = new int[]{
                defaultColWidth * 3 / 4, defaultColWidth * 2 / 4, defaultColWidth * 8 / 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(0);
        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(2);
        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(4);
        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(6);
        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(2);
        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(6);
        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(0);
            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(3);
            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, 4, 7);
        setColumnWidth(table, 2, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
        cell = table.getRow(2).getCell(4);
        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, 4, 7);
        setColumnWidth(table, 3, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
        cell = table.getRow(3).getCell(4);
        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, 4, 7);
        setColumnWidth(table, 4, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
        cell = table.getRow(4).getCell(4);
        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 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 = 0; 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(0);
            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(3);
            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 < 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 < 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(0);
            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, 1, 7);
            setColumnWidth(table, row, 1, colunmWidths[1]+colunmWidths[2]+colunmWidths[3]+colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);

            cell = table.getRow(row).getCell(1);
            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, STJc.Enum align) {
        CTTbl cttblp = table.getCTTbl();
        CTTblPr cttblpr = (cttblp.getTblPr() == null ? cttblp.addNewTblPr() : cttblp.getTblPr());
        CTJc ctjc = (cttblpr.isSetJc() ? cttblpr.getJc() : cttblpr.addNewJc());
        ctjc.setVal(align);
    }
}