package com.book.web;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.book.entity.Shop;
import com.book.util.BaseDao;
import com.book.util.DateFormatHelper;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**导出pdf
* @author liuluogen 2011-01-20
*
*/
public class PDFAction extends DispatchAction
{
private BaseDao baseDao;
@SuppressWarnings("unchecked")
public ActionForward outPDF(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
try {
Document document = new Document();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PdfWriter p = PdfWriter.getInstance(document, ba);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 8, Font.NORMAL);
Font fontHead = new Font(bfChinese, 10, Font.BOLD);
Table datatable = new Table(4); // 新生成一个表,参数表示列
float[] headerwidths = { 30, 30, 24, 60 };// 设置每一列的宽度
datatable.setWidths(headerwidths);
datatable.setPadding(3);
datatable.setBorder(1);
datatable.addCell(new Phrase("用户详细信息", fontHead));
Cell cell = new Cell("");
cell.setColspan(3);
datatable.addCell(cell);
datatable.addCell(new Phrase("客户编号", fontChinese));
datatable.addCell(new Phrase("HZ00000001", fontChinese));
datatable.addCell(new Phrase("联系人", fontChinese));
datatable.addCell(new Phrase(" Contact Name ", fontChinese));
// DefaultHorizontalAlignment
// datatable.addColumns(2);
datatable.addCell(new Phrase("客户名称(中文)", fontChinese));
datatable.addCell(new Phrase("HZ-CSPC", fontChinese));
datatable.addCell(new Phrase("联系电话(传真)", fontChinese));
datatable.addCell(new Phrase(" 0752-9999999(0752-0000000) ",
fontChinese));
//
// datatable.addColumns(3);
datatable.addCell(new Phrase("客户名称(英文", fontChinese));
datatable.addCell(new Phrase("惠州-CSPC", fontChinese));
datatable.addCell(new Phrase("电子信箱", fontChinese));
datatable.addCell(new Phrase(" Contact@Mail.com ", fontChinese));
// datatable.addColumns(4);
datatable.addCell(new Phrase("客户类型", fontChinese));
datatable.addCell(new Phrase("钻石客", fontChinese));
datatable.addCell(new Phrase("站点URL", fontChinese));
datatable.addCell(new Phrase(" www.cspc.com ", fontChinese));
// datatable.addColumns(5);
datatable.addCell(new Phrase(" 地址 ", fontChinese));
datatable.addCell(new Phrase(" 惠州-中海壳 ", fontChinese));
cell = new Cell("");
cell.setColspan(2);
datatable.addCell(cell);
document.add(datatable);
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public ActionForward outPDFShop(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
try{
Document document = new Document();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PdfWriter p = PdfWriter.getInstance(document, ba);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 8, Font.NORMAL);
Font fontHead = new Font(bfChinese, 10, Font.BOLD);
Table datatable = new Table(4); // 新生成一个表,参数表示列
float[] headerwidths = { 10, 30, 24, 60 };// 设置每一列的宽度
datatable.setWidths(headerwidths);
datatable.setPadding(3);
datatable.setBorder(1);
datatable.addCell(new Phrase("序号", fontHead));
datatable.addCell(new Phrase("书城编号", fontHead));
datatable.addCell(new Phrase("书城名称", fontHead));
datatable.addCell(new Phrase("创建时间", fontHead));
//添加书城列表
List<Shop> shop = baseDao.findAll(Shop.class);
int i=1;
for (Shop shop2 : shop){
datatable.addCell(new Phrase( String.valueOf(i++), fontChinese));
datatable.addCell(new Phrase(shop2.getWh(), fontChinese));
datatable.addCell(new Phrase(shop2.getShopName(), fontChinese));
datatable.addCell(new Phrase(DateFormatHelper.getStringDate(shop2.getCreateTime()), fontChinese));
}
document.add(datatable);
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void setBaseDao(BaseDao baseDao)
{
this.baseDao = baseDao;
}
}
分享到:
相关推荐
非常简单的html导pdf,解压后可以点击demo.html直接看到效果
广联达软件报错及解决办法.pdf
本文档包含报表模板以及往模板嵌入一个子报表的全部过程,并包含完整导出PDF的SourceCode
NULL 博文链接:https://zmx.iteye.com/blog/583482
这本书中有学习数论的基础知识以及提高,适合初学者.
Solidedge工程图批量转换为PDF软件 32位系统,ST4,ST6 测试可用 支持文件夹批量转换
flying sauser,基于iText自身的问题,让我们开始寻求可以真正意义上模版技术,那么flying sauser就是为了这个问题出现的产品,能解析HTML和CSS,而且能输出成image,PDF等格式。具体使用,请访问:...
首先告诉大家如何看自己的pdf文档字体是否嵌入:在pdf格式的文件中,点“文件”-> “文档属性”-> “字体”,显示的就是你文档中用的所有字体了,每一个字体后面,如果注明了“embeded(已嵌入)”或“embeded subset...
个人花大量时间整理出的简历模板,内容丰富,无论是应届生,还是职场老手,都有相应的模板,节约自身时间,提升效率。 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源,包含简历模板、PPT模板、...
奈末office批量转PDF助手是一款是一款简单好用的格式转换软件。软件无需打开文件,就能实现将多个Word,Excel,PowerPoint文档批量转换成PDF文件,软件界面美观简洁、简单全面、实用方便,可快速上手,轻轻松松完成...
学习 Salesforce 基本内容.pdf
虚拟打印机(打印机将内容转换成PDF文件)Adobe_PDF_Printer7
Allegro导出的PDF档工程装配图如何实现索取功能
最新公路同望预算软件操作教程.pdf
CrystalReport PDF 导出
将Excel工作簿快速、批量转换为PDF电子文档
门拓导光柱Light Guides for Frontpanels