Нужно сдвинуть таблицу вправо в слове apache poi - PullRequest
0 голосов
/ 04 ноября 2019

Я создал таблицу в apache POI, необходимо создать еще одну похожую таблицу, но смещение вправо: Created table with Apache POI

Но я хочу создать аналогичную другую таблицу справа, но вниз. enter image description here

У нас есть параметр в XWPFTable, чтобы установить выравнивание для таблицы, чтобы сместить вправо, или мне нужно перестроить всю таблицу снова

Фрагмент кода

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

Ответы [ 2 ]

1 голос
/ 06 ноября 2019

Этот ответ о ваших проблемах с предоставленным вами полным примером:

Первое: выравнивание с использованием параметров двунаправленного текста, чтобы таблица отображалась в направлении справа налево, не рекомендуется. Это следует использовать только для языков, которым действительно нужен двунаправленный текст, например, иврит или арабский. Таким образом, выравнивание вашей таблицы должно выглядеть примерно так:

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

Полный пример создания выровненной по правому краю таблицы из предоставленных снимков экрана:

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);
    }
}
1 голос
/ 04 ноября 2019

Вы можете сделать отступ в таблице, используя классы 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 . Используя это, вы можете выровнять всю таблицу.

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

Для apache poi версий, предшествующих 4.1.1, для выравнивания таблицы вправо можно использовать следующий низкоуровневый подход:

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

 }
}

Результат:

enter image description here

...