700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > JAVA导出pdf文件

JAVA导出pdf文件

时间:2024-01-27 19:30:30

相关推荐

JAVA导出pdf文件

1、实现结果(没有颜色需求)

2、引入jar包:textpdf-5.513.1.jar

3、纯手打,如果复制可能会报错,建议参考。

关键点:1、主要涉及的类 PdfpTable 和 PdfpCell两个了类 ,一个是表格,一个是表格单元

2、document记得open()和close()

3、属性设置,一般就是字体,是否居中,是否有边框等,如果所示

//设置整个表格的列数

PdfPTable table=new PdfPTable(24);

table.setWidthPercentage(100f);//表格的占比

//开始规划表格

PdfPCell PdfPCell1 =new PdfPCell(new Phrase("党(总)支部",textfont));

PdfPCell1.setColspan(6);//合并6列

PdfPCell1.setPaddingTop(10);//内上边距

PdfPCell1.setPaddingBottom(10);//内下边距

PdfPCell1.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中

PdfPCell1.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中

table.addCell(leftTitleOne);

//接口地址@RequestMapping("/downloadPDF")public ModelAndView downloadPDF(HttpServletRequest request , HttpServletResponse reponse) throws Exception{ModelAndView mav = new ModelAndView();File file = null;String fileName = null;List<ActivityClaimDTO> dto = new ActivityClaimDTO();try{//查询需要填充的数据dto = activityClaimService.searchData();file = generateFile(dto);outSinglePdf(file,response)}catch (Exception ex){ex.printStackTrace();}return mav;}public File generateFile(List<ActivityClaimDTO> dto) throws Exception{//设置页面大小Document document = new Document(PageSize.A4);String filePath = ConstantsUtil.getPdfTempPath();File file = new File(filePath+".pdf");file.createNewFile();PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(file));pdfWriter.open();document.open();new PdfCreaterByIText().generatePdf(document,dto);document.close();return file}public void outSinglePdf(File file , HttpServletResponse) throws Exception{BufferedOutputStream bos = null;FileInputStream fileinputstream = null;try{fileinputstream = new FileInputStream(file);String fileName = "文件名"+".PDF";response.setContentType("application/x-msdownload");response.setContentType("*/*;charset=gbk");response.setHeader("Content-Disposition","attachment;filename=\""+new String(fileName.getBytes("GBK")),"ISO8859_1"+"\"");bos = new BufferedOutputStream(response.getOutputStream);int l = 0;byte abyte0[] = new byte[2048];while(-1 != (l = fileinputstream.read(abyte0,0,abyte0.length))){bos.write(abyte0,0,l);}bos.close();fileinputstream.close();}catch(Exception ex){ex.printStachTrace();}finally{try{if(bos != null){bos.close();}}catch(Exception ex){ex.printStachTrace();}try{if(fileinputstream != null){fileinputstream.close();}}catch(Exception ex){ex.printStachTrace();}}}public void generatePdf(Document document , List<ActivityClaimDTO> dto) throws Exception{//设置字体BaseFont bf = BaseFont.createFont("/fzfangsong_GBK.TTF",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);titlefont = new Font(bf,25,Font.BLOD);headfont = new Font(bf,25,Font.BLOD);textfont = new Font(bf,25,Font.BLOD);keyfont = new Font(bf,25,Font.BLOD);//设置标题Paragraph paragraph1 = new Paragraph("标题名",titlefont);paragraph1.setAlignment(Element.ALIGN_CENTER);//设置居中paragraph1.setSpacingBefore(10);//行间距paragraph1.setSpacingAfter(20);//设置整个表格的列数PdfPTable table=new PdfPTable(24);table.setWidthPercentage(100f);//表格的占比//开始规划表格PdfPCell PdfPCell1 =new PdfPCell(new Phrase("党(总)支部",textfont));PdfPCell1.setColspan(6);//合并6列PdfPCell1.setPaddingTop(10);//内上边距PdfPCell1.setPaddingBottom(10);//内下边距PdfPCell1.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中PdfPCell1.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中table.addCell(leftTitleOne);Paragraph orgIdName= new Paragraph(dto.getOrgIdName(),titlefont);PdfPCell PdfPCell2 =new PdfPCell(orgIdName);PdfPCell2.setColspan(6);PdfPCell2.setPaddingTop(10);//内上边距PdfPCell2.setPaddingBottom(10);//内下边距PdfPCell2.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中PdfPCell2.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中table.addCell(PdfPCell2);PdfPCell PdfPCell1 =new PdfPCell(new Phrase("活动日期",textfont));PdfPCell1.setColspan(6);PdfPCell1.setPaddingTop(10);//内上边距PdfPCell1.setPaddingBottom(10);//内下边距PdfPCell1.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中PdfPCell1.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中table.addCell(leftTitleOne);SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy年M月d日")String date = dataFormat.format(dto.getDate())PdfPCell PdfPCell2 =new PdfPCell(new Phrase(date,textfont));PdfPCell2.setColspan(6);PdfPCell2.setPaddingTop(10);//内上边距PdfPCell2.setPaddingBottom(10);//内下边距PdfPCell2.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中PdfPCell2.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中table.addCell(PdfPCell2);PdfPCell PdfPCell1 =new PdfPCell(new Phrase("活动名称",textfont));PdfPCell1.setColspan(6);PdfPCell1.setPaddingTop(10);//内上边距PdfPCell1.setPaddingBottom(10);//内下边距PdfPCell1.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中PdfPCell1.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中table.addCell(leftTitleOne);Paragraph activityName= new Paragraph(dto.getActivityName(),textfont);PdfPCell PdfPCell2 =new PdfPCell(activityName);PdfPCell2.setColspan(18);PdfPCell2.setPaddingTop(10);//内上边距PdfPCell2.setPaddingBottom(10);//内下边距PdfPCell2.setVerticalAlignment(Cell.ALIGN_MIDDLE);//居中PdfPCell2.setHorizontalAlignment(Cell.ALIGN_CENTER);//居中table.addCell(PdfPCell2); //以此类推即可,纯手打就不往下打了document.add(paragraph1);document.add(table);}

4、如有疑问可随时评论,也可以一起讨论下。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。