java生成PDF文件
来源:互联网 发布:单片机中jb是什么意思 编辑:程序博客网 时间:2024/05/16 10:35
转载自:http://blog.csdn.net/dinphi/article/details/14122311
1、需要导入itext.jar和iTextAsian.jar 下载地址:http://sourceforge.net/projects/itext/files/
- import java.awt.Color;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Element;
- import com.lowagie.text.Font;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Phrase;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfCell;
- import com.lowagie.text.pdf.PdfPCell;
- import com.lowagie.text.pdf.PdfPRow;
- import com.lowagie.text.pdf.PdfPTable;
- import com.lowagie.text.pdf.PdfWriter;
- import com.sun.java_cup.internal.internal_error;
- public class PDFReport{
- Document document = new Document();// 建立一个Document对象
- private static Font headfont ;// 设置字体大小
- private static Font keyfont;// 设置字体大小
- private static Font textfont;// 设置字体大小
- static{
- BaseFont bfChinese;
- try {
- //bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
- keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
- textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public PDFReport(File file) {
- document.setPageSize(PageSize.A4);// 设置页面大小
- try {
- PdfWriter.getInstance(document,new FileOutputStream(file));
- document.open();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- int maxWidth = 520;
- public PdfPCell createCell(String value,com.lowagie.text.Font font,int align){
- PdfPCell cell = new PdfPCell();
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- cell.setHorizontalAlignment(align);
- cell.setPhrase(new Phrase(value,font));
- return cell;
- }
- public PdfPCell createCell(String value,com.lowagie.text.Font font){
- PdfPCell cell = new PdfPCell();
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setPhrase(new Phrase(value,font));
- return cell;
- }
- public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan){
- PdfPCell cell = new PdfPCell();
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- cell.setHorizontalAlignment(align);
- cell.setColspan(colspan);
- cell.setPhrase(new Phrase(value,font));
- return cell;
- }
- public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan,boolean boderFlag){
- PdfPCell cell = new PdfPCell();
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- cell.setHorizontalAlignment(align);
- cell.setColspan(colspan);
- cell.setPhrase(new Phrase(value,font));
- cell.setPadding(3.0f);
- if(!boderFlag){
- cell.setBorder(0);
- cell.setPaddingTop(15.0f);
- cell.setPaddingBottom(8.0f);
- }
- return cell;
- }
- public PdfPTable createTable(int colNumber){
- PdfPTable table = new PdfPTable(colNumber);
- try{
- table.setTotalWidth(maxWidth);
- table.setLockedWidth(true);
- table.setHorizontalAlignment(Element.ALIGN_CENTER);
- table.getDefaultCell().setBorder(1);
- }catch(Exception e){
- e.printStackTrace();
- }
- return table;
- }
- public PdfPTable createTable(float[] widths){
- PdfPTable table = new PdfPTable(widths);
- try{
- table.setTotalWidth(maxWidth);
- table.setLockedWidth(true);
- table.setHorizontalAlignment(Element.ALIGN_CENTER);
- table.getDefaultCell().setBorder(1);
- }catch(Exception e){
- e.printStackTrace();
- }
- return table;
- }
- public PdfPTable createBlankTable(){
- PdfPTable table = new PdfPTable(1);
- table.getDefaultCell().setBorder(0);
- table.addCell(createCell("", keyfont));
- table.setSpacingAfter(20.0f);
- table.setSpacingBefore(20.0f);
- return table;
- }
- public void generatePDF() throws Exception{
- PdfPTable table = createTable(4);
- table.addCell(createCell("学生信息列表:", keyfont,Element.ALIGN_LEFT,4,false));
- table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
- table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));
- table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
- table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));
- for(int i=0;i<5;i++){
- table.addCell(createCell("姓名"+i, textfont));
- table.addCell(createCell(i+15+"", textfont));
- table.addCell(createCell((i%2==0)?"男":"女", textfont));
- table.addCell(createCell("地址"+i, textfont));
- }
- document.add(table);
- document.close();
- }
- public static void main(String[] args) throws Exception {
- File file = new File("D:\\text.pdf");
- file.createNewFile();
- new PDFReport(file).generatePDF();
- }
- }
0 0
- JAVA生成PDF文件
- java生成PDF文件
- java生成PDF文件
- java 生成pdf文件
- java生成PDF文件
- java生成pdf文件
- java生成PDF文件
- Java生成PDF文件
- java生成PDF文件
- java 生成pdf文件
- Java生成PDF文件
- java生成PDF文件
- java生成pdf文件代码
- Java Servlet PDF文件生成
- java-pdf-(itext+adobe acrobat+pdf模板)生成pdf文件
- java 其他文件转成pdf java生成pdf
- 从 Java 应用程序动态生成 PDF 文件
- 生成PDF文件的Java库iText
- java中常见异常的结构和分类
- 使用性能计数器收集性能数据
- ZooKeeper 典型应用场景一览
- jQuery获取选中复选框的值
- CodeBlocks在Windows下安装与配置
- java生成PDF文件
- CodeForces 552C-Vanya and Scales【思维】
- 对正则表达式的理解
- MySQL-5.7.11绿色版安装
- python的一些细节(1)
- HNOI模拟 2016.3.31 百步穿杨
- mnt/sdcard和storage/sdcard的区别
- Java的动态代理
- 从 Java 应用程序动态生成 PDF 文件