How to adjust widths of the second row in Words table

I have a Word table with custom first row, but the following rows aren't adjusted. Please tell me why, and how to do the same widths of cells of the second row?

  XWPFDocument document = new XWPFDocument();


    CTDocument1 doc = document.getDocument();
    CTBody body = doc.getBody();

    if (!body.isSetSectPr()) {
        body.addNewSectPr();
    }
    CTSectPr section = body.getSectPr();

    if(!section.isSetPgSz()) {
        section.addNewPgSz();
    }
    CTPageSz pageSize = section.getPgSz();
    pageSize.setW(BigInteger.valueOf(15840));
    pageSize.setH(BigInteger.valueOf(12240));

    XWPFParagraph paragraph = document.createParagraph();
    CTSectPr ctSectPr = section ;//paragraph.getCTP().addNewPPr().addNewSectPr();
    CTColumns ctColumns = ctSectPr.addNewCols();
    ctColumns.setNum(BigInteger.valueOf(1));
    paragraph.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun test = paragraph.createRun();
    test.setFontSize(15);
    test.setBold(true);
    test.setFontFamily("Times New Roman");
    test.setText("Перечни документов, необходимые для применения и исполнения технических регламентов (форма № 21)");
    test.addBreak();

    XWPFTable supergroupTable = document.createTable();
    supergroupTable.setWidth(15200);
    //supergroupTable.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*720));
    //supergroupTable.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(12*720));

    CTTblWidth tblWidth = supergroupTable.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(400));
    supergroupTable.getRow(0).getCell(0).setText("№");
    //STTblWidth.DXA is used to specify width in twentieths of a point.
    tblWidth.setType(STTblWidth.DXA);

    XWPFTableRow supegroupRow = supergroupTable.getRow(0);
    XWPFTableCell supergroupCell = supegroupRow.createCell();
    tblWidth = supergroupTable.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(1700));
    tblWidth.setType(STTblWidth.DXA);
    supergroupCell.setText("Обозначение технического регламента");

    supergroupCell = supegroupRow.createCell();
    tblWidth = supergroupTable.getRow(0).getCell(2).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(1700));
    tblWidth.setType(STTblWidth.DXA);
    supergroupCell.setText("Наименование на русском языке ");

    supergroupCell = supegroupRow.createCell();
    tblWidth = supergroupTable.getRow(0).getCell(3).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(880));
    tblWidth.setType(STTblWidth.DXA);
    supergroupCell.setText("Статус");

    supergroupCell = supegroupRow.createCell();
    tblWidth = supergroupTable.getRow(0).getCell(4).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(1700));
    tblWidth.setType(STTblWidth.DXA);
    supergroupCell.setText("Обозначение документа");

    supergroupCell = supegroupRow.createCell();
    tblWidth = supergroupTable.getRow(0).getCell(5).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(1700));
    tblWidth.setType(STTblWidth.DXA);
    supergroupCell.setText("Наименование на русском языке");

    supergroupCell = supegroupRow.createCell();
    tblWidth = supergroupTable.getRow(0).getCell(6).getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(880));
    tblWidth.setType(STTblWidth.DXA);
    supergroupCell.setText("Статус");

    supegroupRow = supergroupTable.createRow();
    supergroupCell = supegroupRow.getCell(0);
    supergroupCell.setText("cell 1");
    supergroupCell = supegroupRow.getCell(1);
    supergroupCell.setText("cell 2");
    supergroupCell = supegroupRow.getCell(2);
    supergroupCell.setText("cell 3");
    supergroupCell = supegroupRow.getCell(3);
    supergroupCell.setText("cell 4");
    supergroupCell = supegroupRow.getCell(4);
    supergroupCell.setText("cell 5");
    supergroupCell = supegroupRow.getCell(5);
    supergroupCell.setText("cell 6");
    supergroupCell = supegroupRow.getCell(6);
    supergroupCell.setText("cell 7");

When I trying to set width of the second row in the same manner that it is set up for the first row the document come to mess and I can't do it the same manner.

The resulting document

Upvotes: 1

Views: 639

Answers (1)

Axel Richter
Axel Richter

Reputation: 61945

Current versions of apache poi provide setWidth methods on high level, so the low level CT... classes are not needed. Using the low level CT... classes one needs special knowledge of the exact XML which needs to be created, else one creates wrong XML very fast. This seems to be the case in your case.

Using current apache poi 5.0.0 creating a table as yours is as simple as this:

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation;
import java.math.BigInteger;

public class CreateWordTableCellWidths {

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

  XWPFDocument document = new XWPFDocument();

  //page settings are not provided high level on apache poi until now.
  CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
  CTPageSz pageSz = sectPr.addNewPgSz();
  pageSz.setW(BigInteger.valueOf(15840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"
  pageSz.setH(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
  pageSz.setOrient(STPageOrientation.LANDSCAPE);

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("Header line...");

  //create table having first row having 7 columns
  XWPFTable table = document.createTable(1, 7);

  table.setWidth("100%");

  //set cell width and content for first row
  table.getRow(0).getCell(0).setText("Nr.");
  table.getRow(0).getCell(0).setWidth("400");
  table.getRow(0).getCell(1).setText("Loremipsumsemitdolor dolorsemitloremipsum semitdolor");
  table.getRow(0).getCell(1).setWidth("1700");
  table.getRow(0).getCell(2).setText("Lorem ipsum semit dolor dolor semit lorem");
  table.getRow(0).getCell(2).setWidth("1700");
  table.getRow(0).getCell(3).setText("Lorem ipsum");
  table.getRow(0).getCell(3).setWidth("880");
  table.getRow(0).getCell(4).setText("Lorem ipsum semit dolor dolor semit lorem");
  table.getRow(0).getCell(4).setWidth("1700");
  table.getRow(0).getCell(5).setText("Lorem ipsum semit dolor dolor semit lorem");
  table.getRow(0).getCell(5).setWidth("1700");
  table.getRow(0).getCell(6).setText("Lorem ipsum");
  table.getRow(0).getCell(6).setWidth("880");

  //create further row
  XWPFTableRow row = table.createRow();
  row.getCell(0).setText("cell 1");
  row.getCell(0).setWidth("400");
  row.getCell(1).setText("cell 2");
  row.getCell(1).setWidth("1700");
  row.getCell(2).setText("cell 3");
  row.getCell(2).setWidth("1700");
  row.getCell(3).setText("cell 4");
  row.getCell(3).setWidth("880");
  row.getCell(4).setText("cell 5");
  row.getCell(4).setWidth("1700");
  row.getCell(5).setText("cell 6");
  row.getCell(5).setWidth("1700");
  row.getCell(6).setText("cell 7");
  row.getCell(6).setWidth("880");

  paragraph = document.createParagraph();

  FileOutputStream out = new FileOutputStream("CreateWordTableCellWidths.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

Only page settings are not provided high level on apache poi until now. So to set page settings using the low level CT... classes is necessary.

For usage in Microsoft Word in general you set cell widths for cells in first row only. Further rows are using those cell widths too except if there are merged cells used.

For usage in Libreoffice or OpenOffice Writer width of all cells in all rows needs to be set.

Upvotes: 1

Related Questions