Search code examples
javaapache-poidocx

Using XWPFParagraph and XWPFRun, how to make the lines printing on the same. See the description please


I'm generating a .docx file from Java using Apache POI. I'm having a table that has columns like.. table without any entry I'm writing the content inside second row using following code.

String itemNames[] = {“Item 1”, “Item Name 2”, "Item 3"};
for(int i=0; i<3; i++) {
 //getRow(1) represents second row of the table
 //printing sr. no
 paragraph = table.getRow(1).getCell(0).addParagraph();
 paragraph.setAlignment(ParagraphAlignment.CENTER);
 paragraph.setVerticalAlignment(TextAlignment.CENTER);
 run = paragraph.createRun();
 run.setText(i+1);

//printing item names
paragraph = table.getRow(1).getCell(1).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
run = paragraph.createRun();
run.setText(itemNames[i]);

//printing qty
paragraph = table.getRow(1).getCell(2).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
run = paragraph.createRun();
run.setText(1 + (i*10));
}

That is generating the output like below. Error picture In this, as you can see, the item name for row 2 can't be written in 1 line as cell width is smaller than the content. So, there's word wrap set so that line completes writing in 2 lines. However, rest of the content take only 1 line.

So when the code is printing third entry, it is creating a new paragraph in sr no, item name and qty and writing the values. But, as you can see in the image, the output is not as it should be.

I tried to get number of lines in paragraph and in run also but I could not get the actual number and as the item name is bigger, I want to add breaks to other columns to save it in proper form. But how to find the line numbers for every run for the cell 2 in row 2?


Solution

  • What I would do is writing the content of row 2 of the table into the already present row and then for each further content row, creating a new XWPFTableRow using XWPFTable.createRow.

    Example:

    First table in WordTableExample.docx:

    enter image description here

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    public class WordInsertTableRows {
    
     public static void main(String[] args) throws Exception {
    
      XWPFDocument doc = new XWPFDocument(new FileInputStream("WordTableExample.docx"));
      XWPFTable table = doc.getTableArray(0);
      XWPFTableRow row;
      XWPFParagraph paragraph;
      XWPFRun run;
    
      String itemNames[] = {"Item 1", "Item Name 2", "Item 3"};
    
      for(int i=0; i<itemNames.length; i++) {
       row = table.getRow(1+i); //getRow(1) represents second row of the table
       if (row == null) row = table.createRow(); //if there is not a row already, then create one
       //printing sr. no
       paragraph = row.getCell(0).getParagraphArray(0);
       if (paragraph == null) paragraph = row.getCell(0).addParagraph();
       paragraph.setAlignment(ParagraphAlignment.CENTER);
       //paragraph.setVerticalAlignment(TextAlignment.CENTER); //this sets valign for the paragraph
       row.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //I suspect you wants set valign for the cell
       run = paragraph.createRun();
       run.setText(""+(i+1));
    
       //printing item names
       paragraph = row.getCell(1).getParagraphArray(0);
       if (paragraph == null) paragraph = row.getCell(1).addParagraph();
       paragraph.setAlignment(ParagraphAlignment.CENTER);
       //paragraph.setVerticalAlignment(TextAlignment.CENTER);
       row.getCell(1).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
       run = paragraph.createRun();
       run.setText(itemNames[i]);
    
       //printing qty
       paragraph = row.getCell(2).getParagraphArray(0);
       if (paragraph == null) paragraph = row.getCell(2).addParagraph();
       //paragraph.setVerticalAlignment(TextAlignment.CENTER);
       row.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
       run = paragraph.createRun();
       run.setText(""+(1 + (i*10)));
      }
    
      doc.write(new FileOutputStream("WordTableExampleNew.docx"));
    
      doc.close();
     }
    }