iText5 导出PDF

1.创建一个PDF
Document document = new Document(rect);//创建一个Document
 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\itext3.pdf"));//创建书写器(Writer) 与 document对象关联,通过书写器可以将文档写入磁盘中
document.open();//打开文档
document.add(new Paragraph("Hello iText"));// 写入文档内容     
document.close();//关闭文档


2
public class SecondPdf {

	public static void main(String[] args) throws DocumentException, IOException {
		Rectangle rect = new Rectangle(PageSize.A4);// 设置页面大小
		rect.setBackgroundColor(BaseColor.ORANGE);// 页面背景色
		Document document = new Document(rect);// 创建一个Document
		// 页边空白
		document.setMargins(10, 20, 30, 40); // 左,右,上,下
		PdfWriter writer = PdfWriter.getInstance(document,
				new FileOutputStream("E:\\itext3.pdf"));// 创建书写器(Writer) 与
														// document对象关联,通过书写器可以将文档写入磁盘中
		writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);// 设置PDF版本(默认1.4)
		writer.setEncryption("User".getBytes(), "Owner".getBytes(),
				PdfWriter.ALLOW_MODIFY_CONTENTS,
				PdfWriter.STANDARD_ENCRYPTION_128);// 设置密码需要包bcprov-jdk15on-147.jar

		// 文档属性
		document.addTitle("Title@sample"); // 标题
		document.addAuthor("Author@nicaisheng");// 作者
		document.addSubject("Subject@iText sample");// 主题
		document.addKeywords("Keywords@iText");// 关键字
		document.addCreator("Creator@iText");// 创建者

		//设置字体
		BaseFont bfChinese = BaseFont.createFont("STSong-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese = new Font(bfChinese, 14, Font.BOLD);// 创建字体,设置family,size,style,还可以设置color
		Font titleChinese = new Font(bfChinese, 20, Font.BOLD);
		Font BoldChinese = new Font(bfChinese, 14, Font.BOLD);
		Font subBoldFontChinese = new Font(bfChinese, 8, Font.BOLD); 
		
		document.open();// 打开文档
		Paragraph title = new Paragraph("起租通知书", titleChinese);// 设置标题
		title.setAlignment(Element.ALIGN_CENTER);// 设置对齐方式
		title.setLeading(1f);// 设置行间距
		document.add(title);

		title = new Paragraph("致:XXX公司", BoldChinese);
		title.setSpacingBefore(25f);// 设置段前空白宽度
		document.add(title);

		title = new Paragraph(
				"贵我双方签署的编号为 XXX有关起租条件已满足,现将租赁合同项下相关租赁要素明示如下:", fontChinese);
		title.setLeading(22f);
		title.setFirstLineIndent(20f);// 设置首行缩进
		document.add(title);

		float[] widths = { 10f, 25f, 30f, 30f };// 设置表格的列宽和列数 默认是4列
		PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
		table.setSpacingBefore(20f);
		table.setWidthPercentage(100);// 设置表格宽度为100%

		PdfPCell cell = null;
		//---表头
		cell = new PdfPCell(new Paragraph("期次",subBoldFontChinese));//
		cell.setFixedHeight(20);//设置单元格的高度
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("租金日",subBoldFontChinese));
		cell.setFixedHeight(20);//设置单元格的高度
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("期租金金额",subBoldFontChinese));
		cell.setFixedHeight(20);//设置单元格的高度
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
		table.addCell(cell);
		cell = new PdfPCell(new Paragraph("各期租金后\n剩余租金",subBoldFontChinese));
		cell.setFixedHeight(20);//设置单元格的高度
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
		table.addCell(cell);
		document.add(table);
		
		
		document.newPage();//创建新页
		document.add(new Paragraph("new Page"));
		
		
		document.newPage();
		document.add(new Paragraph("new Page2"));
		document.close();// 关闭文档
	}

}



bcprov-jdk15on-147.jar下载: http://www.bouncycastle.org/latest_releases.html

猜你喜欢

转载自ncs123.iteye.com/blog/1772548