Java Word Appache POI - 垂直对齐单元格内容/删除文本后的空格 [英] Java Word Appache POI - Vertically align cell content / remove space after text

查看:57
本文介绍了Java Word Appache POI - 垂直对齐单元格内容/删除文本后的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表是这样的:

My table look like this: Word POI Table

As you can see the text is aligned to the top of a cell. I want to have it centered vertically or aligned to bottom of the cell.. I tried to achieve it with cell.setVerticalAlignment(XWPFVertAlign.CENTER); but it doesn't change anything. I was able to add margin to the cells by table.setCellMargin(). It added margin to top of the cell, but empty space below the text makes the cell too big. Maybe is there a way to make the cell width fit text height? My intended result would look like this: desired result

And here is part of my code:`

XWPFTable table = document.createTable();
CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
tableIndentation.setW(BigInteger.valueOf(720));
tableIndentation.setType(STTblWidth.DXA);

XWPFTableRow tableRowOne = table.getRow(0);
XWPFParagraph paragraphTable = tableRowOne.getCell(0).addParagraph();
table.setCellMargins(0, 50, 0, 0);
XWPFRun runT = paragraphTable.createRun();

runT.setBold(true);
runT.setColor("ffffff");
runT.setText("REFERENCE ACTIVITIES PROVIDED: " + String.valueOf(def.format(c.getEarnedSum())) + " POINTS EARNED");
runT = tableRowOne.addNewTableCell().addParagraph().createRun();
runT = tableRowOne.addNewTableCell().addParagraph().createRun();

tableRowOne.getCell(0).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(1).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(2).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(0).removeParagraph(0);
tableRowOne.getCell(1).removeParagraph(0);
tableRowOne.getCell(2).removeParagraph(0);`

解决方案

The XWPFTableCell.setVerticalAlignment works. But one has to know that each paragraph has a default spacing of 10pt after it. So the paragraph height is text height plus 10pt. And that height is vertical aligned then.

So to get XWPFTableCell.setVerticalAlignment working as you might expect, you needs to set spacing after of the paragraphs to 0.

Complete example:

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

public class CreateWordTableVerticalAlign {

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

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table:");

  //create the table
  XWPFTable table = document.createTable(3,3);
  table.setWidth("100%");
  for (int r = 0; r < 3; r++) {
   XWPFTableRow row = table.getRow(r);

   row.setHeight(1440/2); // 1/2inch; 1440Twip = 1440/20 == 72pt = 1inch 

   for (int c = 0; c < 3; c++) {
    XWPFTableCell cell = row.getCell(c);
    cell.setText("row " + r + ", col " + c);

    // get first paragraph in cell - this contains the content set above by cell.setText
    XWPFParagraph firstParagraphInCell = cell.getParagraphArray(0);
    // set spacing after to 0 (defaults to 10pt)
    firstParagraphInCell.setSpacingAfter(0);

    if (r == 0) {
     // default vertical align
    } else if (r == 1) {
     cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
    } else if (r == 2) {
     cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTTOM);
    }
   }
  }

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

Result:

这篇关于Java Word Appache POI - 垂直对齐单元格内容/删除文本后的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆