admin管理员组

文章数量:1389783

I am using Apache POI 4.1.2 and want to apply justification (align both sides) in a table cell. However, it is not working properly.

Adding run.addBreak(); enables justification, but it creates an extra blank line. Removing run.addBreak(); prevents the blank line, but justification does not work.

XWPFRun run = paragraph.createRun();
run.setText(text); 
run.addBreak(); // ??
run.setFontSize(10);

Is there a good solution for this?

Adding run.addBreak(); enables justification, but it creates an extra blank line.

I am using Apache POI 4.1.2 and want to apply justification (align both sides) in a table cell. However, it is not working properly.

Adding run.addBreak(); enables justification, but it creates an extra blank line. Removing run.addBreak(); prevents the blank line, but justification does not work.

XWPFRun run = paragraph.createRun();
run.setText(text); 
run.addBreak(); // ??
run.setFontSize(10);

Is there a good solution for this?

Adding run.addBreak(); enables justification, but it creates an extra blank line.

Share Improve this question asked Mar 13 at 13:29 jyajyajyajya 11 silver badge 0
Add a comment  | 

1 Answer 1

Reset to default 1

There must be a ParagraphAlignment setting. If that ist set to BOTH, then this sets a kind of Justify text. But, as told in linked Microsoft page:

When you justify text, space is added between words so that both edges of each line are aligned with both margins. The last line in the paragraph is aligned left.

That's how it works.

There also is a setting to "Distribute text". This can be set using ParagraphAlignment.DISTRIBUTE. The Word GUI only provides this setting when a language which needs this is added to Microsoft Office. Adding language Chinese (traditional) will lead to this for example.

Following complete example shows the difference between ParagraphAlignment.BOTH and ParagraphAlignment.DISTRIBUTE.

import java.io.*;
import .apache.poi.xwpf.usermodel.*;

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

  XWPFDocument document = new XWPFDocument();
  
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The table ParagraphAlignment.BOTH vs. DISTRIBUTE:");

  XWPFTable table = document.createTable();
  table.setWidth("100%");
  
  XWPFTableRow tableRow = table.getRow(0);
  tableRow.getCell(0).setText("ParagraphAlignment.BOTH: When you justify text, space is added between words so that both edges of each line are aligned with both margins. The last line in the paragraph is aligned left.");
  tableRow.getCell(0).getParagraphs().get(0).setAlignment(ParagraphAlignment.BOTH);
  tableRow.getCell(0).setWidth("50%");
  tableRow.addNewTableCell().setText("ParagraphAlignment.DISTRIBUTE: Using this also in last line of paragraph space is added between words and letters so that both edges of each line are aligned with both margins.");
  tableRow.getCell(1).getParagraphs().get(0).setAlignment(ParagraphAlignment.DISTRIBUTE);
  tableRow.getCell(1).setWidth("50%");

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

 }
}

Result:

本文标签: apache poiHow can I do to justify a paragraph in table cellStack Overflow