您的位置:首页 > 其它

iText制作PDF文件 学习笔记 (四)

2017-10-09 16:04 363 查看

iText制作PDF文件   

(四)
表格的制作:
package com.java.pdf.fourth;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
* 创建日期:2017-10-9下午1:53:16
* 修改日期:
* 作者:ttan
* 描述:表格
*/
public class Pdf_setTable {
public static void main(String[] args) {
Document document = new Document();
PdfPTable t = new PdfPTable(3);
//设置列宽
t.setTotalWidth(500);
//锁定列宽
t.setLockedWidth(true);
//第一行
PdfPCell cell1_1 = new PdfPCell();
PdfPCell cell1_2 = new PdfPCell();
PdfPCell cell1_3 = new PdfPCell();
//第二行
PdfPCell cell2_3 = new PdfPCell();
//第三行
PdfPCell cell3_3 = new PdfPCell();
//第四行
PdfPCell cell4_3 = new PdfPCell();
//第五行
PdfPCell cell5_3 = new PdfPCell();
//第六行
PdfPCell cell6_1 = new PdfPCell();
PdfPCell cell6_3 = new PdfPCell();
//设置行高
cell1_1.setMinimumHeight(20);
cell1_2.setMinimumHeight(20);
cell1_3.setMinimumHeight(20);
cell2_3.setMinimumHeight(20);
cell3_3.setMinimumHeight(20);
cell4_3.setMinimumHeight(20);
cell5_3.setMinimumHeight(20);
cell6_1.setMinimumHeight(20);
cell6_3.setMinimumHeight(20);
//设置单元格边框颜色
cell1_1.setBorderColor(new BaseColor(255,0,0));
cell1_2.setBorderColor(new BaseColor(255,0,0));
cell1_3.setBorderColor(new BaseColor(255,0,0));
cell2_3.setBorderColor(new BaseColor(255,0,0));
cell3_3.setBorderColor(new BaseColor(255,0,0));
cell4_3.setBorderColor(new BaseColor(255,0,0));
cell5_3.setBorderColor(new BaseColor(255,0,0));
cell6_1.setBorderColor(new BaseColor(255,0,0));
cell6_3.setBorderColor(new BaseColor(255,0,0));
//设置单元格背景色
cell1_1.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
//合并列
//cell1_1.setColspan(3);
//合并行
cell1_1.setRowspan(5);
cell1_2.setRowspan(6);
//设置内容
cell1_1.setPhrase(new Phrase("1_1:"));
cell1_2.setPhrase(new Phrase("1_2:"));
cell1_3.setPhrase(new Phrase("1_3:"));
cell2_3.setPhrase(new Phrase("2_3:"));
cell3_3.setPhrase(new Phrase("3_3:"));
cell4_3.setPhrase(new Phrase("4_3:"));
cell5_3.setPhrase(new Phrase("5_3:"));
cell6_1.setPhrase(new Phrase("6_1:"));
cell6_3.setPhrase(new Phrase("6_3:"));
// 设置水平对齐方式
cell1_1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1_2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1_3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2_3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3_3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell4_3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5_3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6_1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6_3.setHorizontalAlignment(Element.ALIGN_LEFT);

t.addCell(cell1_1);
t.addCell(cell1_2);
t.addCell(cell1_3);
t.addCell(cell2_3);
t.addCell(cell3_3);
t.addCell(cell4_3);
t.addCell(cell5_3);
t.addCell(cell6_1);
t.addCell(cell6_3);

//新增一个表格,宽度相同
PdfPTable pt = new PdfPTable(5);
pt.setTotalWidth(500);
pt.setLockedWidth(true);
PdfPCell cells1_1 = new PdfPCell();
PdfPCell cells1_2 = new PdfPCell();
PdfPCell cells1_3 = new PdfPCell();
PdfPCell cells1_4 = new PdfPCell();
PdfPCell cells1_5 = new PdfPCell();
cells1_1.setMinimumHeight(30);
cells1_2.setMinimumHeight(30);
cells1_3.setMinimumHeight(30);
cells1_4.setMinimumHeight(30);
cells1_5.setMinimumHeight(30);
cells1_1.setPhrase(new Phrase("cells1_1"));
cells1_2.setPhrase(new Phrase("cells1_2"));
cells1_3.setPhrase(new Phrase("cells1_3"));
cells1_4.setPhrase(new Phrase("cells1_4"));
cells1_5.setPhrase(new Phrase("cells1_5"));
cells1_1.setBorderColor(new BaseColor(255,0,0));
cells1_2.setBorderColor(new BaseColor(255,0,0));
cells1_3.setBorderColor(new BaseColor(255,0,0));
cells1_4.setBorderColor(new BaseColor(255,0,0));
cells1_5.setBorderColor(new BaseColor(255,0,0));

pt.addCell(cells1_1);
pt.addCell(cells1_2);
pt.addCell(cells1_3);
pt.addCell(cells1_4);
pt.addCell(cells1_5);
try {
PdfWriter.getInstance(document, new FileOutputStream("table.pdf"));
document.open();
document.add(t);
document.add(pt);
} catch (Exception e) {
e.printStackTrace();
}finally{
document.close();
}
}
}

总结:API相当多,通过自己不断的尝试可以制成自己所需要的表格样式,应用上是十分广泛的,比如通过程序生成各种报表。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息