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. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  2. 编程军规 —— Java 篇

    提高代码的可读性,规避容易出现的错误. 0. 共性 对象或引用的非空性判断: 强制类型转换时: 函数返回时: 函数的输入参数: 任务执行的成功或失败判断: 文件打开:网络连接:数据库连接: 内存申请: ...

  3. Ubuntu logomaker sh: 1: pngtopnm: not found 解决方案

    暂时未找到logomaker的方法,来解决 命令替换,在文件夹: pngtopnm open_show.png > temp.ppm ppmquant 224 temp.ppm >temp ...

  4. Poco logger 日志使用小析

    Poco logger 日志使用小析 Poco logger 日志使用小析 日志 logger 库选择 Pocologger 架构简析 步骤一 生成消息 步骤二 写入logger 步骤三 导入chan ...

  5. LeetCode OJ平台Sort Colors讨论主题算法

    原题如下面,这意味着无序排列(由0,1,2组成).一号通.组织成若干阵列0-几个1-几个2这样的序列. Given an array with n objects colored red, white ...

  6. Mybatis缓存 缓存配置文件 good

    一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  7. 手把手教你开发Nginx模块

    前面的哪些话 关于Nginx模块开发的博客资料,网上很多,很多.但是,每篇博客都只提要点,无法"step by step"照着做,对于初次接触Nginx开发的同学,只能像只盲目的蚂 ...

  8. HALCON 光圈和景深的关系

    光圈越大,越亮,景深越小 光圈越小,越暗,景深越大 景深为成像清晰的那个范围

  9. c# 全局钩子实现扫码枪获取信息。

    原文:c# 全局钩子实现扫码枪获取信息. 1.扫描枪获取数据原理基本相当于键盘数据,获取扫描枪扫描出来的数据,一般分为两种实现方式. a)文本框输入获取焦点,扫描后自动显示在文本框内. b)使用键盘钩 ...

  10. delphi2009(10,xe)下indy10发送utf8字符串

    最近实现一个功能,使用delphi2009以TCP调用Java端的接口,接口要求先发送字符串的长度,然后再发送字符串内容,并且字符串要求是utf8格式的 调试了好长时间,才终于发现解决办法,或者说发现 ...