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. 优雅需要付出代价.从短期利益来看,对某个问题提出优雅的解决方法,似乎可能花你更多的时间.但当它终于能够正确执行并可轻易套用于新案例中,不需要花上数以时计,甚至以天计或以月计的辛苦代价时,你会看得 ...
随机推荐
- 3 学习UML图 学习DDD的基本概念
今天在学习DDD的过程中看到了大神 圣杰的博文 个人觉得非常经典 强烈推荐 在此提供url链接: UML类图10分钟快速入门 关于聚合 的理解 :一类没有紧密相关的东西聚在一起 ,分开了也可以独立存 ...
- matlab 工具函数 —— logdet(A)
当参数 A 是正定矩阵(positive definite)时,logdet 利用相关矩阵分解的性质,将比 log(det(A)) 获得更快的效率: function y = logdet(A) tr ...
- 使用WPF实现3D场景[一]
原文:使用WPF实现3D场景[一] 在这篇文章里,将介绍如何实现一个简单的三维场景,一个三维的空间,包括空间内的三维物体的组合. 首先介绍一下一个三维场景里的基本元素: 先是定义一个简单的三维的场景环 ...
- linux之tail -F命令异常file truncated
使用tail -F收集日志时,经常报出file truncated, 导致日志又重新读取.tail: `test.out' has appeared; following end of new fi ...
- SQL之Grant(分配权限)和Revoke(回收权限)
Grant Grant可以把指定的权限分配给特定的用户,如果这个用户不存在,则会创建一个用户 命令格式 grant 权限 on 数据库名.表名 to 用户名@登陆方式 identified by 'p ...
- Form submit
方法1:使用form onsubmit标签 return XXX()方法 <!--onsubmit--> <form id="formid" name=" ...
- facebook javascript api 使用
官方api文档:http://developers.facebook.com/docs 先简单的介绍下创建一个app(https://developers.facebook.com/apps),
- Image Captioning 经典论文合辑
Image Caption: Automatically describing the content of an image domain:CV+NLP Category:(by myself, y ...
- Word Embeddings: Encoding Lexical Semantics
Word Embeddings: Encoding Lexical Semantics Getting Dense Word Embeddings Word Embeddings in Pytorch ...
- 命令行程序如何获取HINSTANCE?
main() { HINSTANCE hinst = GetModuleHandle(NULL); }