`
zhy584520
  • 浏览: 180687 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JAVA利用iText导出PDF

    博客分类:
  • j2ee
 
阅读更多

最新项目中客户要求把一些表格导成PDF发邮件形式发送给特定的用户,本文是利用iText生成PDF,采用策略模式解藕。

所需要引入的包:iText-2.0.8.jar

PdfUtilsBean接口类代码如下:

/**
 *@author liudonggan Email:ldg123456@sina.com
 *@date 2011-7-18 上午10:47:53
 *@version v1.0
 **/
public interface PdfUtilsBean {

	public void CreatePdf(String fileName,String picPath,int id);
}

 PdfRoadKbUtilsBean实现类(implements PdfUtilsBean)代码如下:

package com.ys.product.bean;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
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.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.ys.product.service.ISgKbRoadDetailService;
import com.ys.util.Utils;

/**
 *@author liudonggan Email:ldg123456@sina.com
 *@date 2011-7-18 上午10:53:31
 *@version v1.0
 **/
public class PdfRoadKbUtilsBean implements PdfUtilsBean {

	private SgKbRoadDetail sgKbRoadDetail=new SgKbRoadDetail();
	private ISgKbRoadDetailService sgKbRoadDetailService;
	public PdfRoadKbUtilsBean(ISgKbRoadDetailService sgKbRoadDetailService)
	{
		this.sgKbRoadDetailService=sgKbRoadDetailService;
	}

	private final int CELLHIGHT=30;//行高
	public void CreatePdf(String fileName,String picPath,int id) {
		// TODO Auto-generated method stub
		File file=new File(fileName);
		FileOutputStream out = null;
		sgKbRoadDetail=sgKbRoadDetailService.findKbRoad(id);
		try
		{
			Document document=new Document(PageSize.A4, 50, 50, 50, 50);
		   out = new FileOutputStream(file);
		   PdfWriter writer = PdfWriter.getInstance(document, out);
		   document.open();
		   
		   Image img=Image.getInstance(picPath+"\\gdcd.jpg");
		   img.scalePercent(70f);
		   img.setAlignment(Element.ALIGN_CENTER);
		   document.add(img);
		   Paragraph title1 = new Paragraph("\n");
		   document.add(title1);
		   
		   img=Image.getInstance(picPath+"\\gdcd1.jpg");
		   img.scalePercent(70f);
		   img.setAlignment(Element.ALIGN_CENTER);
		   document.add(img);
		   
		   Font font = FontFactory.getFont(FontFactory.HELVETICA,
				     18, Font.BOLD, new Color(0, 0, 0));
		   BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体

		   //中文大小为20,加粗
		   font = new Font(bfChinese, 20, Font.BOLD);
		   Paragraph title = new Paragraph("XX标题",font);
		   title.setAlignment(Element.ALIGN_CENTER);
		   document.add(title);
		   title1 = new Paragraph("\n");
		   document.add(title1);
		   //创建表格对象
		   PdfPTable table = new PdfPTable(6);
		   int[] cellsWidth = {10,10,10,10,10,10};
		   table.setWidths(cellsWidth);
		   table.setWidthPercentage(100);
		   table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		   table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		   table.getDefaultCell().setFixedHeight(CELLHIGHT);
		   //设置表格边框颜色
		   table.getDefaultCell().setBackgroundColor(new Color(0, 0, 0));
		   //设置单元格的边距间隔等
		   table.getDefaultCell().setPadding(0);
		   table.getDefaultCell().setBorderWidth(0);
		   //单元格对象
		   PdfPCell cell = new PdfPCell();

	    	Font font1 = new Font(bfChinese, 9, Font.HELVETICA);
	 
	    	
	    	Paragraph cel = new Paragraph("填报单位:"+sgKbRoadDetail.getUnitName(),font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setColspan(6);
	    	cell.setBorder(0);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph("事故基本情况",font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setColspan(6);
	    	cell.setFixedHeight(CELLHIGHT);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph("事故发生时间",font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setColspan(1);
	    	cell.setFixedHeight(CELLHIGHT);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph(Utils.getMinute2(sgKbRoadDetail.getFssj()),font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setColspan(3);
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setNoWrap(true);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph("天气情况",font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setColspan(1);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph(sgKbRoadDetail.getTqqk(),font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setColspan(1);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph("事故地点",font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setColspan(1);
	    	cell.setFixedHeight(CELLHIGHT);
	    	table.addCell(cell);
	    	
	    	cel = new Paragraph(sgKbRoadDetail.getSgdd(),font1);
	    	cel.setAlignment(Element.ALIGN_CENTER);
	    	cell=new PdfPCell(cel);
	    	cell.setColspan(3);
	    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    	cell.setFixedHeight(CELLHIGHT);
	    	cell.setNoWrap(true);
	    	table.addCell(cell);
	
	    	
	    	
		   //将表格对象添加到小节对象中
		   document.add(table);
		   document.close();

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			   if (out != null){

				    try {

				     //关闭输出文件流
				     out.close();
				    } catch (IOException e1) {

				    }
			   }
		}
	}

}

 

PdfUtils类 代码如下

import java.io.File;

/**
 *@author liudonggan Email:ldg123456@sina.com
 *@date 2011-7-18 上午10:50:48
 *@version v1.0
 **/
public class PdfUtils {

	private PdfUtilsBean pdfUtilsBean=null;
	private String fileName="";
	private String picPath="";
	private int id=-1;
	public PdfUtils(PdfUtilsBean pdfUtilsBean,String fileName,String picPath,int id)
	{
		this.pdfUtilsBean=pdfUtilsBean;
		this.fileName=fileName;
		this.picPath=picPath;
		this.id=id;
	}
	public void CreatePdf()
	{
		this.pdfUtilsBean.CreatePdf(fileName,picPath,id);
	}
	public void DeletePdf() throws Exception
	{
		File file=new File(fileName);
		if(file.exists())
		{
			file.delete();
		}
	}
}

 

4
2
分享到:
评论
9 楼 wenm168 2016-12-15  
这里有iText 5.5导出pdf, 比较常用的功能及表格画斜线、文档插入绝对位置图片等:http://www.anyrt.com/blog/list/itextpdf.html
8 楼 mao_siyu 2014-09-25  
这个jar 包 和 这代码里面的类 不一致了,楼主 能改一下吗? 因为我刚刚学习这功能,都不知道应该怎么去修改。
7 楼 ae6623 2013-11-25  
zhy584520 写道
ae6623 写道
高并发的时候,会内存溢出。

并发这块没有测试过,现在你解决了没有,方便的话透露下怎么解决的啦


http://blog.csdn.net/ae6623/article/details/11590611


【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常

 //for循环中添加如下代码
 int _MAX_ROWS = 1000;//最大行数,之后清理
 int row_count = 0;//初始值
 if (++row_count % _MAX_ROWS == 0) {
                            //datatable是我的一个PdfPTable的new出来的一个实例                           
                            // add table to Document
                            document.add(datatable);
                            // delete _MAX_ROWS from table to free memory
                            datatable.deleteBodyRows();
                            // let iText manage when table header written
                            datatable.setSkipFirstHeader(true);//防止释放后一页出现两次表头。
 } 


6 楼 zhy584520 2013-10-21  
ae6623 写道
高并发的时候,会内存溢出。

并发这块没有测试过,现在你解决了没有,方便的话透露下怎么解决的啦
5 楼 ae6623 2013-09-06  
高并发的时候,会内存溢出。
4 楼 zhy584520 2011-12-12  
hubinDong 写道
麻烦楼主把效果图发上来看下。。。初学

你把代码拷下来就可以跑的,稍改一下
3 楼 hubinDong 2011-12-09  
麻烦楼主把效果图发上来看下。。。初学
2 楼 zhy584520 2011-07-27  
幻想的sky 写道
嘿嘿,写的蛮好的。值得研究!

经常过来支持下哇
1 楼 幻想的sky 2011-07-26  
嘿嘿,写的蛮好的。值得研究!

相关推荐

Global site tag (gtag.js) - Google Analytics