java写出PDF
1\包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2、画格子
@RequestMapping("capitalBill")
public ModelAndView capitalBill(
int type, //1日\2月\3季度\4年
String dateBegin,//yyyy-MM-dd
String dateEnd,//yyyy-MM-dd
HttpServletRequest request,
HttpServletResponse response, HttpSession session
) throws Exception{
ModelAndView mov = new ModelAndView("/platformCapitalBill");
//查询数据
if(type==1){
//日报表数据, 某天0:00到23:59:59
//查询日报表对象
}else if(type==2){//月
}else if(type==3){//季度
}else if(type==4){//年
}
//展示数据
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment;filename="
+ new String("报表".getBytes("gb2312"), "ISO8859-1")
+ ".pdf");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// 获取响应数据流
ServletOutputStream out = response.getOutputStream();
// 创建文档对象,A4纸大小,40边距
Document document = new Document(PageSize.A4, 80, 80, 80, 80);
PdfWriter.getInstance(document, stream);
// 解决中文不显示,设定初始字体
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
BaseFont baseFont_V = BaseFont.createFont("STSong-Light","UniGB-UCS2-V", BaseFont.NOT_EMBEDDED);
Font fonttitle = new Font(baseFont, 16, Font.BOLD);
Font font12bold = new Font(baseFont, 12, Font.BOLD);
Font font8 = new Font(baseFont, 8);
Font font8bold = new Font(baseFont, 8, Font.BOLD);
// 换行
Paragraph enterParagraph = new Paragraph("\n");
// 打开文档
document.open();
//获取路径,用于获取图片等资源
ServletContext servletContext = request.getSession()
.getServletContext();
String basePath = servletContext.getRealPath("/");
// 创建标题
Paragraph fileTile = new Paragraph("环链平台资金日报表", fonttitle);
fileTile.setAlignment(Paragraph.ALIGN_CENTER);
fileTile.setSpacingAfter(5);// 设置与后边段落间距
document.add(fileTile);
// 下载日期
SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
Paragraph ParaDate = new Paragraph("下载日期: " + sf.format(new Date()),font8);
ParaDate.setAlignment(Paragraph.ALIGN_LEFT);
ParaDate.setIndentationLeft(150);
document.add(ParaDate);
// 创建一个表格
PdfPTable table = new PdfPTable(6);
table.setTotalWidth(new float[] { 28, 56, 163, 28, 56, 163 });// 去掉左右各40,515
table.setLockedWidth(true);
table.setSpacingBefore(5);
// 第一行
PdfPCell tableCell = new PdfPCell(new Paragraph("报表日期:", new Font(baseFont, 8, Font.BOLD)));
tableCell.setColspan(2);
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("2039-12-12", new Font(baseFont, 8)));
tableCell.setColspan(4);
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
// 第二行
tableCell = new PdfPCell(new Paragraph("上期余额:", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(2);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("财务环链支付在关联方之间形成新的债权债务", font8));
tableCell.setColspan(4);
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
// 第10行
tableCell = new PdfPCell(new Paragraph("其中:", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(1);
//tableCell.setRowspan(1);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("服务费存入:", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(1);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("100.00", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(2);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
// 第11行
tableCell = new PdfPCell(new Paragraph("其他存入", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(2);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("00", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("其它转出:", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(2);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("00", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
// 第12行
tableCell = new PdfPCell(new Paragraph("合计:", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(2);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("0.0", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("合计:", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setColspan(2);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("0.0", font8bold));
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("本期全额", new Font(baseFont, 8, Font.BOLD)));
tableCell.setColspan(2);
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
tableCell = new PdfPCell(new Paragraph("2039-12-12", new Font(baseFont, 8)));
tableCell.setColspan(4);
tableCell.setMinimumHeight(20);
tableCell.setUseAscender(true);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(tableCell);
//添加
document.add(table);
// 关闭文档
document.close();
// 文档输出
stream.writeTo(out);
out.flush();
out.close();
return mov;
}
java写出PDF的更多相关文章
- 如何用java写出无副作用的代码
搞java的同学们可能对无副作用这个概念比较陌生,这是函数式编程中的一个概念,无副作用的意思就是: 一个函数(java里是方法)的多次调用中,只要输入参数的值相同,输出结果的值也必然相同,并且在这个函 ...
- 2017.12.1 如何用java写出一个菱形图案
上机课自己写的代码 两个图形原理都是一样的 1.一共有仨个循环 注意搞清楚每一层循环需要做的事情 2.第一层循环:是用来控制行数 3.第二层循环控制打印空格数 4.第三层循环是用来循环输出星星 imp ...
- java写出图形界面
1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...
- java写出进程条代码
package com.ds; import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import ...
- 《重学 Java 设计模式》PDF 出炉了 - 小傅哥,肝了50天写出18万字271页的实战编程资料
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! @ 目录 一.前言 二.简介 1. 谁发明了设计模式? 2. 我怎么学不会设计模式? 3. 适 ...
- 【原创】怎样才能写出优雅的 Java 代码?这篇文章告诉你答案!
本文已经收录自 JavaGuide (59k+ Star):[Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 本文比较简短,基本就是推荐一些对于写好代码非常有用的文章或者 ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 如何写出无法维护的代码(JAVA版)
程序命名(针对那些不能混淆的代码) 容易输入的名字.比如:Fred,asdf 单字母的变量名.比如:a,b,c, x,y,z,或者干脆上中文比如(阿隆索肯德基) 有创意地拼写错误.比如:SetPint ...
- 如何写出好的Java代码?
1. 优雅需要付出代价.从短期利益来看,对某个问题提出优雅的解决方法,似乎可能花你更多的时间.但当它终于能够正确执行并可轻易套用于新案例中,不需要花上数以时计,甚至以天计或以月计的辛苦代价时,你会看得 ...
随机推荐
- SharePoint Iframe 一个错误此内容不能显示在一帧<继续>
在之前的SharePoint网站iframe引用中,我们遇到过以下的问题,就是其他系统或者不通环境的SharePoint网站,引用SharePoint页面会报错"此内容不能显示在一个框架中& ...
- mac_开发机初始化环境
#安装 homebrew 类似 yum ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...
- 2017 JavaScript 开发者的学习图谱
码云项目推荐 前端框架类 1.项目名称: 基于 Vue.js 的 UI 组件库 iView 项目简介:iView 是一套基于 Vue.js 的 UI 组件库,主要服务于 PC 界面的中后台产品. 特性 ...
- jquery ready和window onload区别
window onload是指标签加载完成,并且标签资源加载完成: jquery ready是指标签加载完成,标签资源可能未加载完成 $(document).ready(function(){});= ...
- ASP FirstWeb
//html <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" co ...
- C# 不重启程序修改并保存配置文件(appSettings节点)
原文:C# 不重启程序修改并保存配置文件(appSettings节点) private static void UpdateAppConfig(string newKey, string newVal ...
- 没必要看源码。。把文档学通就已经牛逼了(我们大多还是在应用层,还达不到研究的程度。附class与examples大全链接)
[学霸]深圳-鑫 2017/7/11 13:54:07只是学习怎么用QT的话,不用看源码.看帮助文档就很好要学习编码风格与思路,就看看源码 [学神]武汉-朝菌 2017/7/11 13:54:39没必 ...
- Git 将子文件夹分离为一个新的库
前面的需求 公司Android的项目上,想要将一些module抽取出来,作为一个可以被其它项目上使用的. 所以使用了git submodule的方案. 为了将代码库中的一个文件夹分离后,作为一个单独的 ...
- 改善C#程序的建议6:在线程同步中使用信号量
原文:改善C#程序的建议6:在线程同步中使用信号量 所谓线程同步,就是多个线程之间在某个对象上执行等待(也可理解为锁定该对象),直到该对象被解除锁定.C#中对象的类型分为引用类型和值类型.CLR在这两 ...
- 为DataGridTemplateColumn设置快捷菜单
<DataGrid.ContextMenu> <ContextMenu> <MenuItem Command="{x:Static ApplicationCom ...