
< /p>
Я не буду сохранять границы таблицы/ячейки, но оставил их, чтобы объяснить проблему. Между крайним левым краем таблицы и текстом «Заголовок раздела» есть некоторое поле/отступ/пространство, а затем аналогичная проблема с правой стороны. Я не могу понять, что создает этот интервал и как его удалить. Я пытался исследовать результат слова «документ» в LibreOffice, но не понимаю, какой элемент вызывает интервал, то есть таблица, ячейки или оболочка абзаца для текста. Сами текстовые метки определенно не содержат вокруг себя лишних символов-пробелов. Фантомные отступы не исчезают при удалении границ таблицы/ячейки.
int TWIPS_PER_INCH = 1440;
XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
XWPFTable table = header.createTable(1, 2);
//table.removeBorders();
table.setWidth("100%");
table.setCellMargins(0, 0, 0, 0);
/*
* Create CTTblGrid for this table with widths of the 2 columns.
* Necessary for Libreoffice/Openoffice to accept the column widths.
*/
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(3 * TWIPS_PER_INCH));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3 * TWIPS_PER_INCH));
/*
* Left-Hand Cell
*/
XWPFTableRow tableRow = table.getRow(0);
XWPFTableCell cell = tableRow.getCell(0);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTTOM);
cell.setWidth("50%");
XWPFParagraph paragraph = doc.createParagraph();
paragraph = cell.getParagraphArray(0);
if (paragraph == null) paragraph = cell.addParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setSpacingBefore(0);
paragraph.setSpacingAfter(0);
paragraph.setFirstLineIndent(0);
paragraph.setIndentationLeft(0);
XWPFRun run = paragraph.createRun();
run = paragraph.createRun();
run.setFontFamily("Helvetica");
run.setFontSize(18);
run.setText("Section Title");
/*
* Right-Hand Cell
*/
cell = tableRow.getCell(1);
cell.setWidth("50%");
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTTOM);
paragraph = cell.getParagraphArray(0);
if (paragraph == null) paragraph = cell.addParagraph();
paragraph.setAlignment(ParagraphAlignment.RIGHT);
paragraph.setSpacingBefore(0);
paragraph.setSpacingAfter(0);
paragraph.setFirstLineIndent(0);
paragraph.setIndentationRight(0);
run = paragraph.createRun();
run.setFontFamily("Helvetica");
run.setFontSize(15);
run.setText("Sub-Title");
logger.log( String.format("BOTTOM BORDER SPACE: [%s]\n", table.getBottomBorderSpace()) );
logger.log( String.format("TOP BORDER SPACE: [%s]\n", table.getTopBorderSpace()) );
logger.log( String.format("LEFT BORDER SPACE: [%s]\n", table.getLeftBorderSpace()) );
logger.log( String.format("RIGHT BORDER SPACE: [%s]\n", table.getRightBorderSpace()) );
logger.log( String.format("INSIDE HOR BORDER SPACE: [%s]\n", table.getInsideHBorderSpace()) );
logger.log( String.format("INSIDE VERT BORDER SPACE: [%s]\n", table.getInsideVBorderSpace()) );
logger.log( String.format("BOTTOM CELL MARGIN: [%s]\n", table.getCellMarginBottom()) );
logger.log( String.format("TOP CELL MARGIN: [%s]\n", table.getCellMarginTop()) );
logger.log( String.format("LEFT CELL MARGIN: [%s]\n", table.getCellMarginLeft()) );
logger.log( String.format("RIGHT CELL MARGIN: [%s]\n", table.getCellMarginRight()) );
Вот результаты этого журнала выше:
BOTTOM BORDER SPACE: [-1]
TOP BORDER SPACE: [-1]
LEFT BORDER SPACE: [-1]
RIGHT BORDER SPACE: [-1]
INSIDE HOR BORDER SPACE: [-1]
INSIDE VERT BORDER SPACE: [-1]
BOTTOM CELL MARGIN: [0]
TOP CELL MARGIN: [0]
LEFT CELL MARGIN: [0]
RIGHT CELL MARGIN: [0]
Подробнее здесь: https://stackoverflow.com/questions/752 ... apache-poi
Мобильная версия