java : iText PDF 起步
2016-06-11 13:37
489 查看
FirstPDF.java
编译 javac -cp iText/iText-2.1.7.jar;iText/iTextAsian.jar;. -encoding UTF-8 FirstPDF.java
执行 java -cp iText/iText-2.1.7.jar;iText/iTextAsian.jar;. FirstPDF
//package com.itext; import java.awt.Color; import java.io.FileOutputStream; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class FirstPDF { public static void main(String[] args) { // 创建文档对象,A4纸大小 Document document = new Document(PageSize.A4); try { // 输出为E:\itest.pdf文件 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\itest.pdf ")); //设置文档作者 document.addAuthor("itext"); //设置文档标题 document.addTitle("This is itext pdf file"); //设置主题 document.addSubject("First pdf"); //设置关键字 document.addKeywords("iText"); // 打开文档 document.open(); // 在pdf文件中写入文字 document.add(new Paragraph("Hello World, Hello iText")); //设置字体的文字 document.add(new Paragraph("Hello World, Hello iText",FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLDITALIC))); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL); //设置为中文 Paragraph pragraph=new Paragraph("你好,这是中文", FontChinese); document.add(pragraph); // 创建2行3列的表 Table table = new Table(3, 2); // 设置表格边框颜色 table.setBorderColor(new Color(220, 100, 100)); // 设置表格边距 table.setPadding(5); // 设置表格间距 table.setSpacing(3); // 设置表格线条宽度 table.setBorderWidth(3); //创建单元格对象 Cell cell = new Cell(" Header 1 "); //将单元格添加到表格中 table.addCell(cell); cell = new Cell(" Header 2 "); //设置单元格占2列 cell.setColspan(2); table.addCell(cell); //将普通文本添加到表格中 table.addCell(" Cell 1 "); table.addCell(" Cell 2 "); table.addCell(" Cell 3 "); //将表格添加到文档中 document.add(table); // 关闭文档 document.close(); } catch (Exception e) { e.printStackTrace(); } } }
编译 javac -cp iText/iText-2.1.7.jar;iText/iTextAsian.jar;. -encoding UTF-8 FirstPDF.java
执行 java -cp iText/iText-2.1.7.jar;iText/iTextAsian.jar;. FirstPDF
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树