PDF组件练习 iText

来源:互联网 发布:职业测评软件 编辑:程序博客网 时间:2024/04/30 04:13

一、用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大小的文档  默认为打印机纸张大小A4Document 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();}//设置响应文档类型 为pdfresponse.setContentType("application/pdf");//设置响应数据大小response.setContentLength(stream.size());//为输出流的大小//获得响应数据ServletOutputStream out=response.getOutputStream();//得到response中的输出流//将pdf数据流写入响应数据中stream.writeTo(out);out.flush();out.close();}


原创粉丝点击