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的更多相关文章

  1. 如何用java写出无副作用的代码

    搞java的同学们可能对无副作用这个概念比较陌生,这是函数式编程中的一个概念,无副作用的意思就是: 一个函数(java里是方法)的多次调用中,只要输入参数的值相同,输出结果的值也必然相同,并且在这个函 ...

  2. 2017.12.1 如何用java写出一个菱形图案

    上机课自己写的代码 两个图形原理都是一样的 1.一共有仨个循环 注意搞清楚每一层循环需要做的事情 2.第一层循环:是用来控制行数 3.第二层循环控制打印空格数 4.第三层循环是用来循环输出星星 imp ...

  3. java写出图形界面

    1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...

  4. java写出进程条代码

    package com.ds; import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import ...

  5. 《重学 Java 设计模式》PDF 出炉了 - 小傅哥,肝了50天写出18万字271页的实战编程资料

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! @ 目录 一.前言 二.简介 1. 谁发明了设计模式? 2. 我怎么学不会设计模式? 3. 适 ...

  6. 【原创】怎样才能写出优雅的 Java 代码?这篇文章告诉你答案!

    本文已经收录自 JavaGuide (59k+ Star):[Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 本文比较简短,基本就是推荐一些对于写好代码非常有用的文章或者 ...

  7. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  8. 如何写出无法维护的代码(JAVA版)

    程序命名(针对那些不能混淆的代码) 容易输入的名字.比如:Fred,asdf 单字母的变量名.比如:a,b,c, x,y,z,或者干脆上中文比如(阿隆索肯德基) 有创意地拼写错误.比如:SetPint ...

  9. 如何写出好的Java代码?

    1. 优雅需要付出代价.从短期利益来看,对某个问题提出优雅的解决方法,似乎可能花你更多的时间.但当它终于能够正确执行并可轻易套用于新案例中,不需要花上数以时计,甚至以天计或以月计的辛苦代价时,你会看得 ...

随机推荐

  1. 至HDFS附加内容

    在最近的项目开发中遇到的问题: 需要产生良好hdfs文件的其他内容.但使用在线版1.0.3.见发现官方文件,于1.0.4支持的文件的版本号之后append 一下是向hdfs中追加信息的操作方法 假设你 ...

  2. TOP计划猿10最佳实践文章

    本文转自:EETproject教师专辑 http://forum.eet-cn.com/FORUM_POST_10011_1200263220_0.HTM?click_from=8800111934, ...

  3. matlab 正态分布相关 API

    randn:标准正太分布(μ=0,σ=1) normrnd:正态分布随机数,(需要手动指定 μ,σ,二者均是标量) mvnrnd:多变量正态分布随机数,(需要手动指定 μ,σ(二者为向量))

  4. [Gevent]gevent 网络抓取问答

    我听说过gevent基于事件的异步处理功能 如何高效率,该项目已很少使用,今天是没什么学习一些简单的使用. 有正式书面一个非常好的教程 中国版的地址:http://xlambda.com/gevent ...

  5. springboot 集成单元测试

    官网参考地址 1. 添加依赖 <!-- 测试 --> <dependency> <groupId>org.springframework.boot</grou ...

  6. spring boot 集成mybatis报错Missing artifact

    1. pom文件中的oracle依赖提示Missing artifact,需要手动下载并导入maven参考 oracle依赖下载地址 (ojdbc6.jar) cd到下载的ojdbc6.jar所在路径 ...

  7. WPF Dispatcher的使用

     <Window x:Class="DispatcherExam.MainWindow"         xmlns="http://schemas.micro ...

  8. MyBatis 问题 & 解决

    # 问题 Invalid bound statement (not found) # 解决 <mappers> 标签的包括的是 SQL 语句存在的地方,此外 <mapper> ...

  9. WPF滚动条嵌套,响应鼠标滑轮事件的处理

    在C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果是鼠标滚动整个S ...

  10. SQL Server 2008收缩日志文件--dbcc shrinkfile参数说明

    原文:SQL Server 2008收缩日志文件--dbcc shrinkfile参数说明 DBCC SHRINKFILE 收缩相关数据库的指定数据文件或日志文件大小. 语法 DBCC SHRINKF ...