最近在做导出pdf文件的功能,参考了很多资料和demo,完成了转出pdf的功能,并适合项目的pdf工具类,现贴出具体的工具类和demo,如有不对的地方欢迎指正

1.pdf工具类PdfUtil.java

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class PdfUtil {

static HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
private static String localchinese = request.getSession().getServletContext()
.getRealPath("pdfFile/simsun.ttf");

/**
* 设置字体
*
* @param size 字体大小
* @return
*/
public static Font createFont(int size){
Font font = null;
try {
BaseFont bfChinese = BaseFont.createFont(localchinese, "Identity-H", true);
font = new Font(bfChinese, size, Font.BOLD);
} catch (Exception e) {
e.printStackTrace();
}
return font;
}
/**
* 设置字体
*
* @param name
* @param encoding
* @param embedded
* @param size 字体大小
* @return
*/
public static Font createFont(String name, String encoding, boolean embedded,int size){
Font font = null;
try {
BaseFont bfChinese = BaseFont.createFont(name, encoding, embedded);
font = new Font(bfChinese, size, Font.BOLD);
} catch (Exception e) {
e.printStackTrace();
}
return font;
}
/**
* 创建cell
*
* @param value 内容
* @param font 字体样式
* @return
*/
public static PdfPCell createCell(String value,Font font){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
return cell;
}

/**
* 创建cell,水平垂直样式
*
* @param value 内容
* @param font 字体样式
* @param verAlign 垂直样式
* @param horAlign 水平样式
* @return
*/
public static PdfPCell createCell(String value,Font font,int verAlign,int horAlign){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(verAlign);
cell.setHorizontalAlignment(horAlign);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/**
* 创建cell,合并单元格
*
* @param value 内容
* @param font 字体样式
* @param verAlign 垂直样式
* @param horAlign 水平样式
* @param colspan 和并列数
* @return
*/
public static PdfPCell createCell(String value,Font font,int verAlign,int horAlign,int colspan){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(verAlign);
cell.setHorizontalAlignment(horAlign);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
}

/**
* 创建表格
*
* @param colNumber 列数
* @return
*/
public static PdfPTable createTable(int colNumber,int totalWidth){
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(totalWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}

/**
* 创建表格
*
* @param widths 列宽数组
* @return
*/
public static PdfPTable createTable(float[] widths,int totalWidth){
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(totalWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
/**
* 创建表格
*
* @param colNumber 表格列数
* @param widths 每列的宽度
* @return
*/
public static PdfPTable createTable(int colNumber ,float[] widths){
PdfPTable table = new PdfPTable(colNumber);
try {
table.setWidthPercentage(90.0F);
table.setWidths(widths);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
/**
* 创建表格
*
* @param colNumber 表格列数
* @param widths 每列的宽度
* @return
*/
public static PdfPTable createTable(int colNumber ,int[] widths){
PdfPTable table = new PdfPTable(colNumber);
try {
table.setWidthPercentage(90.0F);
table.setWidths(widths);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
}

2.pdf工具类的调用

public PdfPTable createScoreTable(AssessmentRange assessmentRange) throws DocumentException, IOException {
Font font = PdfUtil.createFont(12);
PdfPTable table = PdfUtil.createTable(6, new int[] {57, 76, 57, 60, 57, 60});

PdfPCell cell = PdfUtil.createCell("上报单位", font);
table.addCell(cell);
cell = PdfUtil.createCell(assessmentRange.getOrgName(), font);
table.addCell(cell);
cell = PdfUtil.createCell("上报时间", font);
table.addCell(cell);
cell = PdfUtil.createCell(assessmentRange.getReportDate(), font);
table.addCell(cell);
cell = PdfUtil.createCell("考核总分", font);
table.addCell(cell);
cell = PdfUtil.createCell(assessmentRange.getAssessTotalScore(), font);
table.addCell(cell);

return table;
}

3.往document中添加cell,

document.add(export.createScoreTable(assessmentRange));

其他关于Document的代码在其他文章已有,本篇就不在列出。

希望大家多多指正,共同学习

Java中导出pdf文件,pdf工具类demo相关推荐

  1. 关于Java中何时使用static和工具类相关知识

    2019独角兽企业重金招聘Python工程师标准>>> 一.使用static修饰变量:当对象中出现共享数据时,该数据被静态修饰,对象中的特有数据要定义成非静态存放于堆内存中. 二.使 ...

  2. Java中的数组和Arrays工具类

    数组 数组的特点 Java语言中的数组不属于基本数据类型而是引用数据类型 , 所以数组对象是在堆内存当中存储的 , 数组的父类是Object 数组是一个数据的集合 , 本质是一个容器可以存储" ...

  3. Java中的RSA加解密工具类:RSAUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt;import com.mirana.frame.utils.log.LogUtils; ...

  4. Java中Date日期时间的工具类

    package me.xueyao.date;import java.sql.Timestamp; import java.text.ParseException; import java.text. ...

  5. java 中 针对数组进行的工具类

    1.遍历数组的方法: public static void printfArray(int[] arr)  2. 获取数组中最大值: public static int getMax(int[] ar ...

  6. JAVA中MD5加密(MD5工具类)

    转自:https://blog.csdn.net/starry7953810/article/details/79924156 为什么只有加密,没有解密呢?欢迎大佬留言解答 package utilw ...

  7. (后端)Java中关于金额大小写的工具类

    /*** 金额小数转换成中文大写金额* * @author Neil Han* */private static final String UNIT[] = { "万", &quo ...

  8. java中金额元转万元工具类

    public static void main(String[] args) {     // 具体的金额(单位元)     String value = "88000067898" ...

  9. java aes 工具类_Java中的AES加解密工具类:AESUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...

  10. java中使用jxl导出excel表格的工具类(全网唯一亲测可用,在原来基础上扩展)

    java中后台导出excel的话,有两种方案,一是使用poi(不过由于是windows版本的,存在不兼容,但功能更多,更强大),而是使用jxl(纯java编写,不过兼容,简单一些),可以设置输出的ex ...

最新文章

  1. iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用(下)
  2. android de,什么是Android Pre-Dexing,以及如何提高性能?
  3. CrossOver 12 发布,Windows 模拟器
  4. 2012年6月份第4周51Aspx源码发布详情
  5. 解决Docker容器时区不一致的问题
  6. CodeForces 501B - Misha and Changing Handles
  7. 【浙江大学PAT真题练习乙级】1009 说反话 (20分)真题解析
  8. html中首行缩进怎么写,HTML怎么实现首行缩进两个字符?
  9. 字节跳动测开发实习面试
  10. 弓箭传说微信小程序抖音小程序开发
  11. 第五讲 中外数学名题趣题欣赏与解析
  12. 轻松教你苹果airdrop怎么用
  13. 感知机算法在鸢尾花数据集上的实践
  14. linux对外开放端口号
  15. 立创开源 BGA162-809H
  16. 阿德莱德计算机专业本科,阿德莱德大学计算机科学专业详细解读 成为IT大神就靠它...
  17. Lodash.js:实用的工具库
  18. VMware Workstation 快照与克隆的使用
  19. 倩女幽魂pc端总是显示已于服务器断开,win10运行倩女幽魂已停止工作的解决方法...
  20. python学习36:给IDLE添加行号(采用IDLEX的LineNumbers.py)python3.8也可以用(亲测有效)

热门文章

  1. Oracle函数——比较函数
  2. POI操作Excel:cell的背景颜色对照表
  3. dw网页设计期末设计一个网页_Dw网页设计制作九个小技巧,你都会吗?
  4. php job框架,GitHub - zanphp/job-server
  5. 计算机仿真技术生物,基于计算机仿真技术的人体生理特性和病理机制研究
  6. python学习第一课
  7. 信息系统与信息系统安全
  8. 【转】面向程序员的数据库访问性能优化法则
  9. python忽略警告
  10. operate mow 3.15