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();}
- PDF组件练习 iText
- 使用Java组件itext 生成pdf介绍
- 使用Java组件itext 生成pdf介绍
- 使用itext组件生成PDF文件
- 轻松使用IText组件制作pdf报表
- 运用Java组件itext生成pdf
- 使用Java组件itext 生成pdf介绍
- 使用Java组件itext生成pdf介绍
- Java中使用iText组件制作pdf报表
- Java中使用iText组件制作pdf报表
- iText:iText构造PDF文件
- Java开发实用工具及组件应用(JFreeChart、PDF组件-iText、jasper report报表、Maven)
- itext pdf模板生成pdf
- 生成pdf,使用itext
- pdf文件加密(iText)
- iText生成PDF
- 利用iText写PDF
- iText 建立PDF简介
- 讨厌的老流氓hao123和新杯具zol
- 苹果设备型号(全)
- matlab
- C# 创建和连接Access数据库文件
- Python中的内置的模块 和第三方的模块
- PDF组件练习 iText
- ios常用代码
- nohup命令及其输出文件
- Qt-菜单栏
- oracle数据库连接错误解决方法
- centos 文件新建、删除、移动、复制等命令
- 有用的网站
- Google面试题之经典鸡蛋问题
- 终端服务器超出了最大允许连接数解决办法