ITextPdfPTable表格单元的居中显⽰
昨晚寻了⽹上很多关于IText表格居中问题,他们其中的有些代码我即使复制上去⽣成的doc表格的⽂字都是不居中的,后来我⾃⼰出了⼀种居中⽅式:
为PdfPCell对象添加paragraph对象,并将该paragraph设置为居中:
    PdfPCell cell1 = new PdfPCell();html全部居中代码
Paragraph para = new Paragraph("该单元居中");
//设置该段落为居中显⽰
para.setAlignment(1);
cell1.setPhrase(para);
table.addCell(cell1);
代码亲测⽆误,全部代码如下:
import *;
import pdf.PdfPCell;
import pdf.PdfPTable;
import tf.RtfWriter2;
import java.io.FileOutputStream;
/**
* PdfPTable的使⽤⽅法,表格居中问题
* User: HYY
* Date: 13-8-1
* Time: 下午9:54
* To change this template use File | Settings | File Templates.
*/
public class PdfpTableTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// 创建word⽂档,并设置纸张的⼤⼩
Document document = new Document(PageSize.A4);
//设置存放位置
document.open();
//⽣成三列表格
PdfPTable table = new PdfPTable(3);
//设置表格具体宽度
table.setTotalWidth(90);
//设置每⼀列所占的长度
table.setWidths(new float[]{50f, 15f, 25f});
PdfPCell cell1 = new PdfPCell();
Paragraph para = new Paragraph("该单元居中");
//设置该段落为居中显⽰
para.setAlignment(1);
cell1.setPhrase(para);
table.addCell(cell1);
table.addCell(new PdfPCell(new Phrase("無幽之路IText教程")));
table.addCell(new PdfPCell(new Phrase("無幽之路IText教程")));
document.add(table);
document.close();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。