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

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相当多,通过自己不断的尝试可以制成自己所需要的表格样式,应用上是十分广泛的,比如通过程序生成各种报表。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值