package com.jll.demo;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
* 汇聚了很多对表格的操作
* @author jiang.lili
*
*/
public class PDFTableDemo {
/**
* 找了好久才发现这样写的话不显示页数模式
*/
/* Rectangle rect = new Rectangle(PageSize.A4);
Document document = new Document(rect, 10, 10, 10, 10);*/
Document document = new Document(PageSize.A4);
public Font getFont(){
// 添加 中文信息
BaseFont bf;
Font f=null;
try {
bf = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", false);
f = new Font(bf, 12, Font.NORMAL, BaseColor.BLUE);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
/**
* 插入一个Table
*/
public void createPdfTable1(String path) {
try {
PdfWriter.getInstance(document, new FileOutputStream(path));
document.open();
// 生成一个一行4列的表格
PdfPTable table = new PdfPTable(1);
PdfPCell cell;
// 生成一个单元格
cell = new PdfPCell(new Phrase("Cell with colspan 3可以显示中文", getFont()));
// 跨4列
cell.setColspan(4);
// 把刚刚生成的单元格加载到表格中
table.addCell(cell);
table.addCell(cell);
// 添加一个单元格
for(int i=0;i<1000;i++){
table.addCell("row 1; cell1");
table.addCell("row 2; cell2");
table.addCell("row 3; cell3");
table.addCell("row 4; cell4");
}
document.add(table);
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 表格嵌套
*/
public void createPdfTable2() {
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:/pdftable2.pdf"));
document.open();
// 1行4列
PdfPTable table = new PdfPTable(1);
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.2");
document.add(table);
// 1行2列
PdfPTable newTable =null;
for (int i = 0; i <1000; i++) {
// 1行2列
newTable = new PdfPTable(2);
// 2行1列
PdfPTable newTable2 = new PdfPTable(3);
newTable2.addCell("2.1");
newTable2.addCell("2.2");
newTable2.addCell("2.2");
newTable.addCell(newTable2);
newTable.addCell("122314232");
int _MAX_ROWS = 1000;//最大行数,之后清理
int row_count = 0;//初始值
if (++row_count % _MAX_ROWS == 0) {
//datatable是我的一个PdfPTable的new出来的一个实例
document.add(newTable);
newTable.deleteBodyRows();
newTable.setSkipFirstHeader(true);//防止释放后一页出现两次表头。
}
document.add(newTable);
}
// 将表格插入到指定的位置
/* for (int i = 1; i <= 24; i++) {
if (i == 1) {
table.addCell(newTable);
} else if (i == 20) {
table.addCell(newTable2);
} else {
table.addCell("cell" + i);
}
}*/
//table.addCell(newTable);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 设置表格宽度
*/
public void createPdfTable3() {
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:/pdftable3.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell = new PdfPCell(new Phrase("跨3列", getFont()));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("跨2列", getFont()));
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Phrase("2行3列", getFont()));
table.addCell(new Phrase("3行1列", getFont()));
table.addCell(new Phrase("3行2列", getFont()));
table.addCell(new Phrase("3行3列", getFont()));
// 100%
table.setWidthPercentage(100);
document.add(table);
// 下滑两行
document.add(new Paragraph("\n\n"));
// 宽度50%,居左|| Element.ALIGN_CENTER=>居中
table.setWidthPercentage(50);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
document.add(table);
document.add(new Paragraph("\n\n"));
// 固定的宽度
table.setTotalWidth(500);
table.setLockedWidth(true);
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 设置表格前后间隔
*/
public void createPdfTable4() {
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:/pdftable4.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("合并三个单元格", getFont()));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new Phrase("第二行第一列", getFont()));
table.addCell(new Phrase("第二行第二列", getFont()));
table.addCell(new Phrase("第二行第三列", getFont()));
table.addCell(new Phrase("第三行第一列", getFont()));
table.addCell(new Phrase("第三行第二列", getFont()));
table.addCell(new Phrase("第三行第三列", getFont()));
cell = new PdfPCell(new Paragraph("红色边框", getFont()));
cell.setBorderColor(new BaseColor(255, 0, 0));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("合并两个单元格", getFont()));
cell.setColspan(2);
cell.setBackgroundColor(new BaseColor(0xC0, 0xC0, 0xC0));
table.addCell(cell);
// 设置表格宽度为50%
table.setWidthPercentage(50);
document.add(new Paragraph("追加三个表格", getFont()));
document.add(table);
document.add(table);
document.add(new Paragraph("\n"));
document.add(table);
document.newPage();
document.add(new Paragraph("使用'SpacingBefore'和'setSpacingAfter", getFont()));
// 每段文字上面和下面的空间大小
// (小结:感觉setSpacingBefore()这个方法和document.add(new
// Paragraph("\n"));没啥区别)
table.setSpacingBefore(15f);
document.add(table);
document.add(table);
document.add(new Paragraph("这里没有间隔", getFont()));
// table.setSpacingBefore(15f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 设置单元格宽度
*/
public void createPdfTable5() {
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:/pdftable5.pdf"));
document.open();
// 按比例设置单元格宽度
float[] widths = { 0.1f, 0.1f, 0.05f, 0.75f };
PdfPTable table = new PdfPTable(widths);
table.addCell("10%");
table.addCell("10%");
table.addCell("5%");
table.addCell("75%");
table.addCell("aqqqqqqqqqqqqqqqqqqqqqqqq");
table.addCell("a");
table.addCell("aaaaa");
table.addCell("aaaaaaaaaaa");
table.addCell("bb");
table.addCell("bb");
table.addCell("bbbbb");
table.addCell("bbbbbbbbbbbb");
table.setSpacingBefore(15f);
document.add(table);
// 调整比例
widths[0] = 20f;
widths[1] = 20f;
widths[2] = 10f;
widths[3] = 50f;
table.setWidths(widths);
document.add(table);
// 按绝对值设置单元格宽度
widths[0] = 40f;
widths[1] = 40f;
widths[2] = 20f;
widths[3] = 300f;
// 不明白这句话的意思(起什么作用?)
Rectangle r = new Rectangle(PageSize.A4.getRight(72),
PageSize.A4.getTop(72));
table.setWidthPercentage(widths, r);
table.setSpacingAfter(15f);
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 设置单元格高度
*/
public void createPdfTable6() {
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d://pdftable6.pdf"));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
// 拆行
table.addCell(new PdfPCell(new Paragraph("拆行", getFont())));
cell = new PdfPCell(new Paragraph(
"dert err ter tertret ter tetggrt rtyete utyututyuuuuuuuuuuuuuuuuuu" +
"uuuuuterererrtrerwerwerwerwrwrwefsdfwsfrtretetetertertertetertppppppppp"));
cell.setNoWrap(false);
table.addCell(cell);
// 不拆行
table.addCell(new PdfPCell(new Paragraph("不拆行", getFont())));
cell.setNoWrap(true);
table.addCell(cell);
// 设置高度
table.addCell(new PdfPCell(new Paragraph("设置任意高度", getFont())));
cell = new PdfPCell(
new Paragraph(
"erewr天文台ewerwrqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" +
"\n1. blah blah\n2. blah blah blah\n3. blah blah\n4. blah blah blah\n" +
"5. blah blah\n6. blah blah \n7. blah blah\n8. blah blah blah",
getFont()));
table.addCell(cell);
// 设置固定高度
table.addCell(new PdfPCell(new Paragraph("设置固定高度", getFont())));
cell.setFixedHeight(50f);
table.addCell(cell);
// 设置最小高度
table.addCell(new PdfPCell(new Paragraph("设置最小高度", getFont())));
cell = new PdfPCell(new Paragraph("最小高度:50f", getFont()));
cell.setMinimumHeight(50f);
table.addCell(cell);
// 最后一行拉长到page底部
table.setExtendLastRow(true);
table.addCell(new PdfPCell(new Paragraph("拉长最后一行", getFont())));
cell = new PdfPCell(new Paragraph("最后一行拉长到page底部", getFont()));
table.addCell(cell);
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 设置单元格颜色
*/
public void createPdfTable7(){
try {
PdfWriter.getInstance(document, new FileOutputStream("d://pdftable7.pdf"));
document.open();
PdfPTable table = new PdfPTable(4);
PdfPCell cell;
cell = new PdfPCell(new Paragraph("颜色测试", getFont()));
table.addCell(cell);
//红色背景,无边框
cell = new PdfPCell(new Paragraph("红色背景,无边框", getFont()));
cell.setBorder(Rectangle.NO_BORDER);
cell.setBackgroundColor(BaseColor.RED);
table.addCell(cell);
//绿色背景,下边框
cell = new PdfPCell(new Paragraph("绿色背景,下边框", getFont()));
cell.setBorder(Rectangle.BOTTOM);
//下边框的颜色
cell.setBorderColorBottom(BaseColor.MAGENTA);
//下边框的宽度
cell.setBorderWidthBottom(5f);
cell.setBackgroundColor(BaseColor.GREEN);
table.addCell(cell);
//蓝色背景,上边框
cell = new PdfPCell(new Paragraph("蓝色背景,上边框", getFont()));
cell.setBorder(Rectangle.TOP);
cell.setUseBorderPadding(true);
//上边框的宽度
cell.setBorderWidthTop(15f);
//上边框的颜色
cell.setBorderColorTop(BaseColor.YELLOW);
cell.setBackgroundColor(BaseColor.CYAN);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("背景灰色度", getFont()));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("0.25"));
//设置单元格无边框
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.25f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("0.5"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("0.75"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.75f);
table.addCell(cell);
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
document.close();
}
}
/**
* 插入图像
*/
public void createPdfTable8(){
try {
PdfWriter.getInstance(document, new FileOutputStream("d://pdftable8.pdf"));
document.open();
Image image =Image.getInstance("WebContent/image/doudou.jpg");
float[] widths = {1f,4f};
PdfPTable table = new PdfPTable(widths);
table.addCell(new PdfPCell(new Paragraph("图片测试", getFont())));
table.addCell(new PdfPCell(new Paragraph("", getFont())));
table.addCell(new PdfPCell(new Paragraph("图片测试", getFont())));
table.addCell(new PdfPCell(new Paragraph("", getFont())));
//插入图片
table.addCell(image);
//调整图片大小
table.addCell("This two");
table.addCell(new PdfPCell(image,true));
//不调整图片大小
//(小结:这样就不显示图片了,最后发现是因为图片太大,超过了页面的显示范围了,换一个小点的像素就显示了)
table.addCell("This there");
table.addCell(new PdfPCell(image,false));
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
document.close();
}
}
/**
* 设置表头
*/
public void createPdfTable9(){
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("d:/pdftable9.pdf"));
/**
* 调用PDFBuilder类的setFooter方法显示页眉页脚
*/
// PDFBuilder.setFooter(writer);
//PdfReportM1HeaderFooter headerFooter = new PdfReportM1HeaderFooter();
//writer.setBoxSize("art",PageSize.A4);
//writer.setPageEvent(headerFooter);
document.open();
String bogusData[] = {"M0065920", "SL", "FR86000P", "PCGOLD","119000", "96 06",
"2001-08-13", "4350", "6011648299","FLFLMTGP", "153", "119000.00"};
int numColumns = 12;
float[] headerWidths = {1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f};
PdfPTable table = new PdfPTable(headerWidths);
//int headerWidths[] = {9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 };
//table.setWidths(headerWidths);
table.setWidthPercentage(100);
table.getDefaultCell().setPadding(3);
table.getDefaultCell().setBorderWidth(2);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("a");
table.addCell("b");
table.addCell("c");
table.addCell("d");
table.addCell("e");
table.addCell("f");
table.addCell("g");
table.addCell("h");
table.addCell("i");
table.addCell("g");
table.addCell("k");
table.addCell("l");
table.setHeaderRows(1);
//边框的宽度
table.getDefaultCell().setBorderWidth(1);
for (int i = 0; i < 100; i++) {
for (int j = 0; j < numColumns; j++) {
table.addCell(bogusData[j]);
}
}
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
document.close();
}
}
/**
* 分割表格
*/
public void createPdfTable10(){
try {
PdfWriter writer =PdfWriter.getInstance(document, new FileOutputStream("d:/pdftable10.pdf"));
document.open();
//横向切割
/**
* 当你通过getDirectContent()方法直接使用PdfContentByte对象时,
* 你所添加的所有对象都将浮于文本和图象。
* 如果你想避免这种情况和希望添加内容在图象或文本的背后,
* 你需要使用用getDirectContentUnder()方
*/
PdfContentByte cb = writer.getDirectContent();
PdfPTable table = new PdfPTable(10);
for (int i = 1; i <=100; ++i) {
PdfPCell cell = new PdfPCell(new Paragraph("number"+i));
cell.setGrayFill(0.75f);
table.addCell(cell);
}
table.setTotalWidth(400);
table.writeSelectedRows(0, 5, 0, -1, 5, 700, cb);
table.writeSelectedRows(5, -1, 0, -1, 210, 600, cb);
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block cell.setBorderColor();
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
document.close();
}
}
/**
* 设置单元格留白
*/
public void createPdfTable11(){
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("d:/pdftable11.pdf"));
PDFBuilder.setFooter(writer);
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
Paragraph p = new Paragraph("Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog.");
table.addCell(new PdfPCell(new Paragraph("默认",getFont())));
table.addCell(p);
table.addCell(new PdfPCell(new Paragraph("Padding:10",getFont())));
cell = new PdfPCell(p);
cell.setPadding(10f);
table.addCell(cell);
table.addCell(new PdfPCell(new Paragraph("padding:0",getFont())));
cell = new PdfPCell(p);
cell.setPadding(0f);
table.addCell(cell);
table.addCell(new PdfPCell(new Paragraph("上padding 0,左padding 20",getFont())));
cell = new PdfPCell(p);
cell.setPaddingTop(0f);
cell.setPaddingLeft(20f);
table.addCell(cell);
document.add(table);
document.newPage();
table = new PdfPTable(2);
table.addCell(new PdfPCell(new Paragraph("没有Leading",getFont())));
table.getDefaultCell().setLeading(0f, 0f);
table.addCell("blah blah\nblah blah blah\nblah blah\nblah blah blah\nblah " +
"blah\nblah blah blah\nblah blah\nblah blah blah\n");
table.addCell(new PdfPCell(new Paragraph("固定Leading 14pt",getFont())));
table.getDefaultCell().setLeading(14f, 0f);
table.addCell("blah blah\nblah blah blah\nblah blah\nblah blah blah\nblah " +
"blah\nblah blah blah\nblah blah\nblah blah blah\n");
table.addCell(new PdfPCell(new Paragraph("相对于字体",getFont())));
table.getDefaultCell().setLeading(0f, 1.0f);
table.addCell("blah blah\nblah blah blah\nblah blah\nblah blah blah\nblah " +
"blah\nblah blah blah\nblah blah\nblah blah blah\n");
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
document.close();
}
}
/**
* 设置单元格边框
*/
public void createPdfTable12(){
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("d://pdftable12.pdf"));
PDFBuilder.setFooter(writer);
document.open();
//没有边框
PdfPTable table = new PdfPTable(3);
table.getDefaultCell().setBorder(0);
table.addCell(new PdfPCell(new Paragraph("cell 1")));
table.addCell(new PdfPCell(new Paragraph("cell 2")));
table.addCell(new PdfPCell(new Paragraph("cell 3")));
document.add(table);
//边框粗细颜色
document.newPage();
Rectangle r = new Rectangle(0f,0f);
r.setBorderWidthLeft(6f);
r.setBorderWidthBottom(5f);
r.setBorderWidthRight(4f);
r.setBorderWidthTop(2f);
r.setBorderColorLeft(BaseColor.RED);
r.setBorderColorBottom(BaseColor.ORANGE);
r.setBorderColorRight(BaseColor.YELLOW);
r.setBorderColorTop(BaseColor.GREEN);
PdfPTable table2 = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Paragraph("cell 1"));
cell.cloneNonPositionParameters(r);
table2.addCell(cell);
document.add(table2);
document.newPage();
document.add(table);
document.newPage();
document.add(table);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
document.close();
}
}
public void pdftableTest(){
createPdfTable2();
createPdfTable11();
}
/**
* @param args
* @throws DocumentException
* @throws FileNotFoundException
*/
public static void main(String[] args) {
//String pdfPathName= "d:/PDF"+new Random().nextInt(10)+".pdf";
//new PDFTableDemo().createPdfTable1(pdfPathName);
new PDFTableDemo().createPdfTable9();
}
}
最近下载更多
最近浏览更多
1358849392 LV21
2022年11月23日
akbar2020 LV9
2022年9月4日
是你爸爸啊100 LV5
2022年8月29日
crosa_Don LV18
2022年6月7日
heifenglei LV7
2022年4月7日
nickshen111 LV8
2021年12月13日
来恬爸爸晋亚阳 LV3
2021年10月29日
疯狂的巨兔12138 LV4
2021年5月14日
阿玉之父 LV1
2021年4月22日
一字清华 LV8
2021年2月21日

