求一份Java 动态生成 pdf文件的示例~ 求一份Java 动态生成 pdf文件的示例,与数据库结合的就更好,如果核心代码能多点注释就.....最好至于需要的两个组件包我自己有,谢谢各位友人帮忙了... 另外问下哪位达人有spring的中文pdf,不胜感激. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://download.csdn.net/source/989301Java生成PDF完整示例,并解决中文乱码问题,不要资源分!快去下呀! 哪位有的话,请发送至[email protected]我的邮箱,期待.... 推荐用ECSIDE,自动生成XSL,WORK,PDF等,可以映射数据库 //课程管理PDF public ActionForward courcemanagePDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { Document document = new Document(); document.setPageSize(PageSize.A4); try { ByteArrayOutputStream ops = new ByteArrayOutputStream(); PdfWriter.getInstance(document,ops); document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font headFont1 = new Font(bfChinese, 14, Font.BOLD); Font headFont2 = new Font(bfChinese, 12, Font.BOLD); Font headFont3 = new Font(bfChinese, 10, Font.NORMAL); float[] widths = {200,200f,300f}; PdfPTable table = new PdfPTable(widths); table.setSpacingBefore(130f); table.setTotalWidth(535); table.setLockedWidth(true); table.getDefaultCell().setBorder(0); PdfPCell cell = new PdfPCell(new Paragraph("BEST人力资源公司",headFont1)); cell.setBorder(0); cell.setColspan(3); table.addCell(cell); //PDF标题 cell = new PdfPCell(new Paragraph("课程管理", headFont1)); // cell.setBorder(0); cell.setFixedHeight(40); cell.setColspan(3); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); //列标题 String cloumTitle[]=new String[]{"课程名称","使用教材","描述"}; int i=0,n=cloumTitle.length; while(i<n){ cell = new PdfPCell(new Paragraph(cloumTitle[i], headFont2)); cell.setFixedHeight(20); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); i++; } //报表数据 CourcemanageDAO courcedao=new CourcemanageDAO(); List ctli=courcedao.findAll();//课程管理所有信息 if(ctli.size()>0) for(int k=0;k<ctli.size();k++){ Courcemanage ctf=(Courcemanage)ctli.get(k); //列数据 cell = new PdfPCell(new Paragraph(ctf.getCourcename(),headFont2)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(ctf.getBookname(),headFont2)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(ctf.getTmemo(),headFont2)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } String tempNOWtime=new Date().toLocaleString(); tempNOWtime=tempNOWtime.split(" ")[0]; cell = new PdfPCell(new Paragraph(tempNOWtime,headFont3)); cell.setFixedHeight(30); cell.setColspan(3); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); document.add(table); document.close(); response.setContentType("application/pdf"); response.setContentLength(ops.size()); ServletOutputStream out = response.getOutputStream(); ops.writeTo(out); out.flush(); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } return null; }详细查看:http://hi.baidu.com/cqdyblog/blog/item/a966aadf1c61195395ee374a.html 用frameset还是其他技术 怎么实现鼠标点到哪里图片就到哪里 Java中如何实现VC中的waitformultipleobjects方法 List的用法, 不解??? 急。。。servlet中如何取出被选择的checkbox的值 请问那里有Resin For Unix下载???? 如何捕捉 页面关闭(主要是非法关闭,如拔网线) 简单问题:java里函数的参数怎么能带出值来? 几个广告JS,放到js文件中失效,求指教 关于微信小程序后端(作者纯小白,文章算是作业) tomcat下相对路径问题 数据库连接池
public ActionForward courcemanagePDF(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { Document document = new Document();
document.setPageSize(PageSize.A4);
try {
ByteArrayOutputStream ops = new ByteArrayOutputStream();
PdfWriter.getInstance(document,ops);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font headFont1 = new Font(bfChinese, 14, Font.BOLD);
Font headFont2 = new Font(bfChinese, 12, Font.BOLD);
Font headFont3 = new Font(bfChinese, 10, Font.NORMAL);
float[] widths = {200,200f,300f};
PdfPTable table = new PdfPTable(widths);
table.setSpacingBefore(130f);
table.setTotalWidth(535);
table.setLockedWidth(true);
table.getDefaultCell().setBorder(0);
PdfPCell cell = new PdfPCell(new Paragraph("BEST人力资源公司",headFont1));
cell.setBorder(0);
cell.setColspan(3);
table.addCell(cell);
//PDF标题
cell = new PdfPCell(new Paragraph("课程管理", headFont1));
// cell.setBorder(0);
cell.setFixedHeight(40);
cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
//列标题
String cloumTitle[]=new String[]{"课程名称","使用教材","描述"};
int i=0,n=cloumTitle.length;
while(i<n){
cell = new PdfPCell(new Paragraph(cloumTitle[i], headFont2));
cell.setFixedHeight(20);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
i++;
}
//报表数据
CourcemanageDAO courcedao=new CourcemanageDAO();
List ctli=courcedao.findAll();//课程管理所有信息
if(ctli.size()>0)
for(int k=0;k<ctli.size();k++){
Courcemanage ctf=(Courcemanage)ctli.get(k);
//列数据
cell = new PdfPCell(new Paragraph(ctf.getCourcename(),headFont2));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(ctf.getBookname(),headFont2));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(ctf.getTmemo(),headFont2));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
}
String tempNOWtime=new Date().toLocaleString();
tempNOWtime=tempNOWtime.split(" ")[0];
cell = new PdfPCell(new Paragraph(tempNOWtime,headFont3));
cell.setFixedHeight(30);
cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell); document.add(table);
document.close();
response.setContentType("application/pdf");
response.setContentLength(ops.size());
ServletOutputStream out = response.getOutputStream();
ops.writeTo(out);
out.flush();
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
return null;
}详细查看:http://hi.baidu.com/cqdyblog/blog/item/a966aadf1c61195395ee374a.html