好记性不如烂笔头4-JAVA生成PDF文件

使用iText JAR生成PDF

在很多的场合,需要使用PDF文件,有时候也要设置PDF文件的表格和文字等。

如果让一个PDF工具类非常灵活,那么这个工具类就会很复杂;一般根据实际情况,写几个合适的工具类来分别实现不同要求;

需要引入第三方jar包:iText-2.1.4.jar
和iTextAsian.jar

package com.daily;

import java.awt.Color;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.daily.EasyExcel.Content;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Phrase;

import com.lowagie.text.Table;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

/**

*生成pdf文件,书写普通文字,制作表格,设置文字颜色和简单排版等

*@author 范芳铭

*/

public class EasyPdf {

privatevoid createPdfFile(String outFilename,Map model,List<Content> table)    throws Exception{

try{

OutputStream file = newFileOutputStream(new File(outFilename));

Document document = newDocument();

PdfWriter.getInstance(document,file);

document.open();

document.add(newParagraph("Hello PDF"));  //简单文字

document.add(new Paragraph(newDate().toString()));  //简单日期

//设置字体,表格等内容

BaseFont bfComic =BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

Font font = newFont(bfComic, 16,Font.NORMAL);//声明字体对象

font = newFont(bfComic,16,1,new Color(255,0,0));//设置字体

String header =(String)model.get("header");//获取标题

Paragraph headerParagraph =new Paragraph(header,font);

headerParagraph.setAlignment(Paragraph.ALIGN_CENTER);//设置格式

document.add(headerParagraph);//添加到Document对象中

font = new Font(bfComic,10, Font.NORMAL);//声明字体样式

//设置PDF中表格的表头

String title[] = {"code","名称"};

Table t = newTable(title.length);//声明表格对象

t.normalize();

t.setAutoFillEmptyCells(true);

t.setAlignment(Table.ALIGN_CENTER);//居中

t.setWidth(108);//设置宽度

for(int i = 0 ; i <title.length ; i++){

Cell celltitle = newCell();//声明单元格

celltitle.add(newPhrase(title[i],font));//循环加入表头信息

celltitle.setHorizontalAlignment(Element.ALIGN_CENTER);

t.addCell(celltitle);

}

t.endHeaders();

//加入表格内容

Content content = null;

for (int i = 0; i <table.size(); i++) {

// 得到要写入表格的记录

content = table.get(i);

CelloneCell = new Cell();  //声明单元格对象

Paragraphpgdata = new Paragraph(content.getCode(),font);

oneCell.setHorizontalAlignment(Element.ALIGN_CENTER);//对齐方式

oneCell.addElement(pgdata);

t.addCell(oneCell);

CelltwoCell = new Cell();  //声明单元格对象

twoCell.setHorizontalAlignment(Element.ALIGN_CENTER);//对齐方式

twoCell.addElement(pgdata);

t.addCell(twoCell);

}

document.add(t);

String copyright =(String)model.get("copyright");

ParagraphcopyrightParagraph = new Paragraph(copyright,font);

copyrightParagraph.setAlignment(Paragraph.ALIGN_BOTTOM);

document.add(copyrightParagraph);

document.close();

file.close();

} catch (Exception e) {

e.printStackTrace();

}

}

publicstatic void main(String[] args) throws Exception{

//初始化数据

Mapmodel = new HashMap();

//文件标题头

model.put("header","
详细信息");

//文件版权信息

model.put("copyright","版权所有:
阿饭同学的博客 http://blog.csdn.net/ffm83");

//生成表格的内容,内部类的构造有点特殊

List<Content>contents = new ArrayList<Content>();

contents.add(newEasyPdf().new Content("100","name100"));

contents.add(newEasyPdf().new Content("101","name101"));

contents.add(newEasyPdf().new Content("102","name102"));

longstart = System.currentTimeMillis();

EasyPdfpdf = new EasyPdf();

pdf.createPdfFile("d:/ffm83/easy.pdf",model,contents);

longend = System.currentTimeMillis();

System.out.println("生成pdf文件耗时:"+ (end - start) + "(毫秒)");

}

//一个简单的保存简单信息的内部类

publicclass Content {

privateString code;

private String name;

public Content(String code,String name){

this.code= code;

this.name= name;

}

publicString getCode() {

returncode;

}

publicvoid setCode(String code) {

this.code= code;

}

publicString getName() {

returnname;

}

publicvoid setName(String name) {

this.name= name;

}

}

}

时间: 2024-07-30 13:36:58

好记性不如烂笔头4-JAVA生成PDF文件的相关文章

JAVA生成PDF文件

生成PDF文件是主要应用的是ITEXT插件 1 import java.awt.Color; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import com.lowagie.text.Cell; 9 import com.lowagie.text.

java生成pdf文件 --- Table

Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.org/com.lowagie.html 一.主要步骤 1.新建document对象 第一个参数是页面大小.接下来的参数分别是左.右.上和下页边距. Document document =new Document(PageSize.A4,50,50,30,20); 2.建立一个书写器(Writer)与d

java调用wkhtmltopdf生成pdf文件,美观,省事

最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多东西,样式,字体等等,好麻烦,毕竟博主这么懒的人,哈哈.-------jstarseven 言归正传: java生成pdf文件,方式很多,以上提到的哪几种方式具体可以参考这篇文章<讲解很详细>:http://blog.csdn.net/ouyhong123/article/details/2640

Java Itext 生成PDF文件

利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.OutputStream; import java.math.BigDecimal; import java.net.URL; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Arr

好记性不如烂笔头48-java拦截器-JDK自带动态代理和CGLIB效率比较(3)

Java中自带的动态代理的类必须要实现一个接口,而且据说使用反射的效率也并不是很高.于是CGLIB就诞生了. 使用CGLib实现动态代理,完全不受代理类必须实现接口的限制,而且CGLib底层采用ASM字节码生成框架,使用字节码技术生成代理类,理论上比使用Java反射效率要高. 那么我们测试下,这个运行的效率如何. 1. 测试的准备情况 准备1:好记性不如烂笔头46-java拦截器-彻底理解动态代理的概念(1) http://blog.csdn.net/ffm83/article/details/

使用flying-saucer,利用HTML来生成PDF文件(裴东辉)

1.导入maven依赖 <flyingSaucer.version>9.1.0</flyingSaucer.version> <!-- flying-saucer --> <dependency>    <groupId>org.xhtmlrenderer</groupId>     <artifactId>flying-saucer-pdf</artifactId>     <version>${

Freemarker+IText生成pdf文件

最近项目中遇到要用html生成pdf文件的需求,一开始研究了下前端插件jspdf,使用h5 canvas绘图生成图片,再把图片生成pdf文件,遇到了各种各样的问题,生成的pdf文件达到20多M,height超过5000浏览器就崩溃,有兴趣的童鞋可以尝试一下,该方案LZ最终放弃了. 接着开始尝试服务端生成,使用freemaker模板生成静态html文件,通过iext生成pdf,网上很多关于renderer.setDocument(dom,null)的用法,LZ尝试后发现效率奇低,最后放弃了,直接使

好记性不如烂笔头89-spring3学习(10)-spring3常用schema(持续整理)

Spring3有很多的schema,这些schema对应很多不同的功能.使用这些常用的schema,能让我们避免重复发明轮子,提高我们的开发效率. 我以为这是一个很常见的信息,到网上随便整理下就能完成,可能用的关键词不对,怎么找也没有找到相对比较全面的. 正所谓,好记性不如烂笔头,自己动手把自己能收集到的整理下来,以后需要找资料也能方便点. spring-beans-3.0.xsd 用途:spring3最主要的schema,用于配置bean 命名空间: http://www.springfram

电子凭证 : Java 生成 Pdf

来源:蛙牛, my.oschina.net/lujianing/blog/894365 如有好文章投稿,请点击 → 这里了解详情 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等.方便用户查看,下载,打印.目前常用的解决方案是,把相关数据信息,生成对应的pdf文件返回给用户. 本文源码:http://git.oschina.net/lujianing/java_pdf_demo 2.iText iText是著名的开放源码的站点sourcefo