PDF组件练习 iText

一、用pdf组件iText输出文本

/**
 * 用iText输出文本(包括中文)
 * @author Administrator
 *
 */
public class PDFTest {
	public static void main(String[] args) {
		Document document=new Document(PageSize.A4);//生成一个文档  参数为大小 可无
		try {
			//输出d:/itext.pdf文件
			PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("d:/itext.pdf"));
			//打开文档
			document.open();
			//在pdf文档中写入文字 默认只支持英文
			document.add(new Paragraph("hello itext"));
			//添加中文,
			//1、直接引用系统路径中的字体  就是直接找到系统中字体的位置 进行引用
			//BaseFont bfChinese=BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			//2、创建中文字体,iTextAsian包就是对系统字体的封装 
			//用baseFont创建一个字体  第一个参数为  字体名字(asian包中 cmaps下的属性文件名) 第二个参数为  编码格式(属性文件名下所对应的的编码格式)
			//第三个参数为 输出中文的三种字体选择方式
			/**
			 * 1、使用iTextAsian.jar中的字体
    			BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
				2、使用Windows系统字体(TrueType)
        		BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
				3、使用资源字体(ClassPath)
    			BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);   
			 */
			BaseFont bfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
			Font fontChinese=new Font(bfChinese,12,Font.NORMAL);
			
			//添加中文段落文字
			document.add(new Paragraph("第一个pdf例子 蕤茂盛",fontChinese));
			//添加文档标题
			document.addTitle("iText练习");
			//添加文档作者
			document.addAuthor("朱国志");
			//。。。
			document.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}


二、用iText输出表格

public class MyPdfTable {
	public static void main(String[] args) {
		Document document=new Document();//创建一个文档
		try {
			PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("d://itextTable.pdf"));
			//创建一个3行3列的表
			PdfPTable table=new PdfPTable(3);//表格的列数
			PdfPCell cell=new PdfPCell(new Paragraph("header with colspan3"));//创建了一个单元格
			cell.setColspan(3);//合并单元格
			table.addCell(cell);//将创建好的cell单元格添加到table中
			table.addCell("1.1");//添加一个单元格 内容为 "1.1"
			table.addCell("2.1");
			table.addCell("3.1");
			table.addCell("1.2");
			table.addCell("2.2");
			table.addCell("3.2");
			table.addCell("1.3");
			table.addCell("2.3");
			table.addCell("3.3");
			//打开文档
			document.open();
			//将表格添加到文档中
			document.add(table);
			//关闭文档
			document.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
三、iText输出图像并设置文档属性

/**
 * 用iText输出图像并设置文档属性
 * @author Administrator
 *
 */
public class MyPdfImage {
	public static void main(String[] args) {
		//创建一个A4大小的文档  默认为打印机纸张大小A4
		Document document = new Document(PageSize.A4);
		try {
			//创建一个pdf输出流
			PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("d:\\itextImage.pdf"));
			//设置文档作者
			document.addAuthor("张国强");
			//设置文档标题
			document.addTitle("我的itext输出的pdf");
			//设置主题
			document.addSubject("Image Pdf");
			//设置关键字
			document.addKeywords("itext");
			//打开文档
			document.open();
			//在文档中写入文字
			document.add(new Paragraph("hello iText pdf"));
			//创建图片对象 ,参数为图片的文件名
			Image bmp=Image.getInstance("d:\\image1.jpg");
			//图片的规模百分比  即图片相对于文档的大小百分比
			bmp.scalePercent(100f);//
			//将图片添加到文档中
			document.add(bmp);
			//关闭文档
			document.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

四、用servlet输出pdf

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//创建文档对象,a4大小
		Document document=new Document(PageSize.A4);
		//创建一个字节数组输出流
		ByteArrayOutputStream stream=new ByteArrayOutputStream();
		try {
			//创建一个pdf输出流
			PdfWriter writer=PdfWriter.getInstance(document, stream);
			//打开文档
			document.open();
			//向pdf中写入文字
			document.add(new Paragraph("Hello world , Hello iText!"));
			//关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//设置响应文档类型 为pdf
		response.setContentType("application/pdf");
		//设置响应数据大小
		response.setContentLength(stream.size());//为输出流的大小
		//获得响应数据
		ServletOutputStream out=response.getOutputStream();//得到response中的输出流
		//将pdf数据流写入响应数据中
		stream.writeTo(out);
		out.flush();
		out.close();
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值