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 01 Answer
Reset to default 1There 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
版权声明:本文标题:apache poi - How can I do to justify a paragraph in table cell - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697908a2620391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论