PdfPTable 生的表格默认是---居中 80%
(ˇˍˇ) 想~修改一下他的宽度
查查API
setWidthPercentage(float widthPercentage) 满足需要
package com.dcb1.barge.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* 改变复杂表格的宽度和对齐方式.
*/
public class TableWidthAlignment {
/**
* Changing the width and alignment of the complete table.
*
* param args no arguments needed throws IOException no arguments needed
* throws IOException
*
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException,
IOException {
// 定义中文字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font fontCN = new com.lowagie.text.Font(bfChinese, 12,
com.lowagie.text.Font.NORMAL);
System.out.println("table width and alignment");
// 步骤1:创建一个大小为A4的文档
Document document = new Document(PageSize.A4);
try {
// 步骤 2:
// 我们为document创建一个监听,并把PDF流写到文件中
PdfWriter.getInstance(document, new FileOutputStream(
"src/TableWidthAlignment.pdf"));
// 步骤 3:打开文档
document.open();
// 创建一个有3列的表格
PdfPTable table = new PdfPTable(3);
// 定义一个表格单元
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
// 定义一个表格单元的跨度
cell.setColspan(3);
// 把单元加到表格中
table.addCell(cell);
// 把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
table.addCell("1.3");
table.addCell("2.3");
table.addCell("3.3");
// 重新定义单元格
cell = new PdfPCell(new Paragraph("cell test1"));
// 定义单元格的框颜色
cell.setBorderColor(new Color(255, 0, 0));
// 把单元格加到表格上,默认为一个单元
table.addCell(cell);
// 重新定义单元格
cell = new PdfPCell(new Paragraph("cell test2"));
// 定义单元格的跨度
cell.setColspan(2);
// 定义单元格的背景颜色
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
// 增加到表格上
table.addCell(cell);
document.add(new Paragraph("默认情况下的大小---居中 80%", fontCN));
// 增加到文档中
document.add(table);
document.add(new Paragraph("居中 100%", fontCN));
// 设置表格大小为可用空白区域的100%
table.setWidthPercentage(100);
// 增加到文档中2
document.add(table);
document.add(new Paragraph("居右 50%", fontCN));
// 设置表格大小为可用空白区域的50%
table.setWidthPercentage(50);
// 设置水平对齐方式为 居右
table.setHorizontalAlignment(Element.ALIGN_RIGHT);
document.add(new Paragraph("居左 50%", fontCN));
// 增加到文档中3
document.add(table);
// 设置水平对齐方式为 居左
table.setHorizontalAlignment(Element.ALIGN_LEFT);
document.add(table);
} catch (Exception de) {
de.printStackTrace();
}
// 步骤 5:关闭文档
document.close();
}
}
引用
http://hi.baidu.com/lion98/blog/item/aca07bec343720d12e2e211a.html
分享到:
相关推荐
1.先创建PdfPTable对象,生成PDF表格cell之后,添加隔行换色的事件,将此事件在PdfPTable加入Document对象之前,插入进去。 2.隔行换色的事件需要自己写一个java类,里面去定义背景颜色和长宽高,实质就是在pdf表格...
多个版本的itext-jar包, itext-2.0.4/itext-2.1.5/itext-2.1.7
IText 和 ITextAsian 的开发包
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,...
赠送jar包:itext-2.1.7.jar; 赠送原API文档:itext-2.1.7-javadoc.jar; 赠送源代码:itext-2.1.7-sources.jar; 赠送Maven依赖信息文件:itext-2.1.7.pom; 包含翻译后的API文档:itext-2.1.7-javadoc-API文档-...
Itext5.5.13 完整的全套包,包含bcpkix-jdk15on-1.47.jar bcprov-jdk15on-1.59.jar itext-asian-5.2.0.jar itext-hyph-xml-5.1.1.jar itext-pdfa-5.5.13-javadoc.jar itext-pdfa-5.5.13-sources.jar itext-pdfa-...
itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录
iText7jar和英文文档,以及itext5的一些文档。建议大家去官网看itext7,改动有点大。我也在摸索中。
最近在做项目用到jasperreports6.5.0,该库依赖itext2.7.1版本比较旧,又不能升级itext为5.0以上。照成中文无法显示,因此打了两个字体库兼容itext2.7.1。项目完美显示中文字体。 使用方法: 1、将字体jar文件拷贝...
itext 生成pdf的中文帮助文档,具体有:图片、块(下划线/删除线、上标/下标)、短句、段落等。
itext-rtf-2.1.7.jar,iTextAsian.jar,iText-5.0.6.jar,itext-2.1.7.jar
IText7jar集合
iText-5.0.6.jar iTextAsian.jar
共享一下个人用java编写的生成word文件程序,通过itext生成word文件代码附itext-2.0.7.jar,iTextAsian.jar包
java的PDF生成jar包,包含 iText-5.5.8.jar, iTextAsian.jar, itextpdf-5.5.8, itextpdf-5.5.8-javadoc, itext-xtra-5.5.8, xmlworker-5.5.8。 已打包中文字体
iText1.2.7、iText-rtf2.1.7、iTextAsian
导出报表用itext字段的PdfPtable类,没什么大问题。但是导出一篇文章到pdf里面,就需要借助core-render.jar包,但是一行中文过长,不会自动换行,在pdf文件里面把过长的字符给截取,这样很不友好,修改Flying Saucer...
这是曾经在项目中做pdf导出时用过的itext的包,有包含asian包,可解决中文输出问题。内容包含:itext-4.2.1&&itext;-asian-5.2.0&&itext;-pdfa-5.5.13
赠送jar包:itext-2.1.7.jar; 赠送原API文档:itext-2.1.7-javadoc.jar; 赠送源代码:itext-2.1.7-sources.jar; 赠送Maven依赖信息文件:itext-2.1.7.pom; 包含翻译后的API文档:itext-2.1.7-javadoc-API文档-...
itext 包,itext.jar,itext 下载,汉化itext 包,itext.jar,itext 下载,汉化