ITeye博客

Itext生成PDF打印

gojava1 · 2010-08-30 11:22 · 编程语言 · Markdown

用Itext包生成PDF打印文件,支持插入图片,多种格式混乱编排,还缺斜线没加入

iText-2.1.7.jar

将图片放D盘,将字体从网上下载下来指定位置,下载包,就可以直接运行生成PDF文件

public class dynamicTable {

public static void main(String args) { test2(); } private static void test2() { Document document = null; document = new Document(PageSize.A4, 20, 20, 40, 40); PdfPCell iCell = null; PdfPCell cell = null; PdfPTable tableq = null; PdfPTable subTable = null; String shuxing = "属性"; String shuxingzhi = "属性值"; try { PdfWriter.getInstance(document, new FileOutputStream("d:\coslpan1.pdf")); document.open(); BaseFont bfChinese = BaseFont.createFont("D:\draft/fonts/MSYH.TTF" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font fontChinese = new Font(bfChinese , 12 , Font.NORMAL, Color.BLACK);

        //new一个table
		tableq = new PdfPTable(1);
		float widq = { 500f };//table的大小
		tableq.setTotalWidth(widq);
		tableq.setLockedWidth(true);//锁定边框大小,可以使边框大小固定,缺点是,如果框里的值过长,则没法显示,所以如果值过长,则可以去掉这一行
		tableq.setHorizontalAlignment(Element.ALIGN_LEFT);// 设置左对齐
	    iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));    
		iCell.setFixedHeight(20);
		iCell.setBorder(0);// 设置无边框
		tableq.addCell(iCell);//将生成的cell方入改table,
		document.add(tableq);

		
		tableq = new PdfPTable(1);
		float widq1 = { 500f };
		tableq.setTotalWidth(widq1);
		tableq.setLockedWidth(true);
		tableq.setHorizontalAlignment(Element.ALIGN_LEFT);
		String tupian ="/ECDS_LOGO300.gif";
		Image imageLogo = Image.getInstance(tupian);
		imageLogo.scaleAbsolute(imageLogo.getPlainWidth() / 2, imageLogo.getPlainHeight() / 2);
		iCell = new PdfPCell(imageLogo);
		iCell.setFixedHeight(30);
		iCell.setBorder(0);// 设置无边框
		tableq.addCell(iCell);
		document.add(tableq);

// tableq = new PdfPTable(1); float widq3 = { 500f }; tableq.setTotalWidth(widq3); tableq.setLockedWidth(true); tableq.setHorizontalAlignment(Element.ALIGN_LEFT); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
// iCell = FontUtils.createPdfPCell(filePath1, fontStyletitle); // iCell.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置内容水平居中显示 iCell.setFixedHeight(30); iCell.setBorder(0);// 设置无边框 tableq.addCell(iCell); document.add(tableq); // tableq = new PdfPTable(2);//2table float widq4 = { 250f, 300f }; tableq.setTotalWidth(widq4); tableq.setLockedWidth(true); tableq.setHorizontalAlignment(Element.ALIGN_LEFT);

		iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));    

// iCell = FontUtils.createPdfPCell("出票日期: " + filePath1 + "", // fontStyle); iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell)); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell));

		iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));    
		iCell.setBorder(0);
		tableq.addCell(new PdfPCell(iCell));
		iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));    
		iCell.setBorder(0);
		tableq.addCell(new PdfPCell(iCell));
		document.add(tableq);

		tableq = new PdfPTable(6);
		float widq6 = { 30f, 50f, 195f, 30f, 50f, 195f };//生成6个table
		tableq.setTotalWidth(widq6);
		tableq.setLockedWidth(true);
		tableq.setHorizontalAlignment(Element.ALIGN_LEFT);

		subTable = new PdfPTable(1); //子table
		float iWid2 = { 30f };
		subTable.setTotalWidth(iWid2);
		subTable.setLockedWidth(true);
		  iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));    

// iCell = FontUtils.createPdfPCell("出\n\n票\n\n人", fontStyle); iCell.setHorizontalAlignment(Element.ALIGN_CENTER); subTable.addCell(iCell); cell = new PdfPCell(subTable); tableq.addCell(cell); document.add(tableq);

		subTable = new PdfPTable(2);
		float iWid6 = { 50f, 195f };
		subTable.setTotalWidth(iWid6);
		subTable.setLockedWidth(true);

// iCell = FontUtils.createPdfPCell("全 称", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
subTable.addCell(new PdfPCell(iCell));

		iCell.setFixedHeight(20);

// iCell = FontUtils.createPdfPCell("帐 号", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setFixedHeight(20); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("开户银行", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
subTable.addCell(new PdfPCell(iCell));

		cell = new PdfPCell(subTable); // 用这个table初始外层table的cell
		cell.setColspan(2); // 设置它跨2列
		tableq.addCell(cell);
		document.add(tableq);

		subTable = new PdfPTable(1);
		float iWid12 = { 30f };
		subTable.setTotalWidth(iWid12);
		subTable.setLockedWidth(true);

// iCell = FontUtils.createPdfPCell("收\n\n票\n\n人", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER); subTable.addCell(iCell); cell = new PdfPCell(subTable); tableq.addCell(cell); document.add(tableq);

		subTable = new PdfPTable(2);
		float iWid16 = { 50f, 195f };
		subTable.setTotalWidth(iWid16);
		subTable.setLockedWidth(true);

// iCell = FontUtils.createPdfPCell("全 称", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER); subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
subTable.addCell(new PdfPCell(iCell));

		iCell.setFixedHeight(20);

// iCell = FontUtils.createPdfPCell("帐 号", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER); subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setFixedHeight(20); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("开户银行", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER); subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
subTable.addCell(new PdfPCell(iCell));

		cell = new PdfPCell(subTable); // 用这个table初始外层table的cell
		cell.setColspan(2);//设置跨3行
		tableq.addCell(cell);

		tableq.addCell(subTable);
		document.add(tableq);
		
		tableq = new PdfPTable(7); 
		float widqw = { 80f, 60f, 110f, 60f, 110f, 60f, 70f };
		tableq.setTotalWidth(widqw);
		tableq.setLockedWidth(true);
		tableq.setHorizontalAlignment(Element.ALIGN_LEFT);

// iCell = FontUtils.createPdfPCell("出票保证信息", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setFixedHeight(20); tableq.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("保证人名称:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setFixedHeight(20); iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setFixedHeight(20); iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("保证人地址:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setFixedHeight(20); iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setFixedHeight(20); iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("保证日期:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setFixedHeight(20); iCell.setBorder(0); tableq.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setFixedHeight(20); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); tableq.addCell(new PdfPCell(iCell)); document.add(tableq);

		tableq = new PdfPTable(3);
		float widq6w2 = { 80f, 40f, 430f };
		tableq.setTotalWidth(widq6w2);
		tableq.setLockedWidth(true);
		tableq.setHorizontalAlignment(Element.ALIGN_LEFT);

		subTable = new PdfPTable(1);//40f
		float iWid2e2 = { 80f };
		subTable.setTotalWidth(iWid2e2);
		subTable.setLockedWidth(true);

// iCell = FontUtils.createPdfPCell("评级信息由出票人、承兑人自己记载、仅供参考",fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
subTable.addCell(iCell); cell = new PdfPCell(subTable); tableq.addCell(cell); document.add(tableq);

		subTable = new PdfPTable(7);//430
		float iWid2w22 = { 40f, 50f,130f,50f,60f,70f,70f };
		subTable.setTotalWidth(iWid2w22);
		subTable.setLockedWidth(true);

// iCell = FontUtils.createPdfPCell("出票人:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell("评级主体:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("信用等级:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("评级到期日:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); subTable.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell("承兑人:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorder(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("评级主体:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("信用等级:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("评级到期日:", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorderWidthRight(0); iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); subTable.addCell(new PdfPCell(iCell));

// iCell = FontUtils.createPdfPCell("11", fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorderWidthLeft(0); iCell.setBorderWidthTop(0); iCell.setBorderWidthBottom(0); subTable.addCell(new PdfPCell(iCell));

		cell = new PdfPCell(subTable);
		cell.setColspan(2); 
		tableq.addCell(cell);
		document.add(tableq);

		tableq = new PdfPTable(2);
		float iWidd6 = { 80f, 420f };
		tableq.setTotalWidth(iWidd6);
		tableq.setLockedWidth(true);
		tableq.setHorizontalAlignment(Element.ALIGN_LEFT);

// iCell = FontUtils.createPdfPCell("出票人备注", fontStyle); iCell = new PdfPCell(new Paragraph(shuxing,fontChinese));
iCell.setBorder(0);// 设置无边框 tableq.addCell(new PdfPCell(iCell)); // iCell = FontUtils.createPdfPCell(filePath1, fontStyle); iCell = new PdfPCell(new Paragraph(shuxingzhi,fontChinese));
iCell.setBorder(0);// 设置无边框 tableq.addCell(new PdfPCell(iCell)); document.add(tableq);

	} catch (Exception de) {
		de.printStackTrace();
	}
	document.close();
	System.out.println("OK");
}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。