itextpdf添加表格元素_itext生成pdf文件-表格
生成pdf常用的插件有iReport、和itext,这里将使用itext生成pdf文件。
多于的话不说直接上demo和需要的jar,如果pdf中有图片要画的话可以用jfreeChart画。
package com.pdf;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFReport1 {
int maxWidth = 520;
private static Font keyfont; // 设置字体大小
private static Font textfont; // 设置字体大小
// 建立一个Document对象
Document document = new Document();
static {
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
keyfont = new Font(bfChinese, 8, Font.BOLD); // 设置字体大小
textfont = new Font(bfChinese, 8, Font.NORMAL); // 设置字体大小
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置pdf样式
* @param file
*/
public PDFReport1(File file) {
// 设置页面大小
document.setPageSize(PageSize.A4);
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加表格前的说明
* @param value
* @param font
* @param align
* @param colspan
* @param boderFlag
* @return
*/
public PdfPCell createCell(String value, com.lowagie.text.Font font, int align, int colspan, boolean boderFlag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(3.0f);
if (!boderFlag) {
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
}
return cell;
}
/**
* 向单元格添加字符串、设置单元格属性
* @param value 字符
* @param font字体
* @param align 对齐方式
* @return
*/
public PdfPCell createCell(String value, com.lowagie.text.Font font, int align) {
PdfPCell cell = new PdfPCell();
//设置单元格对齐方式
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
//设置边框颜色
cell.setBorderColor(new Color(15, 15, 15));
//设置单元格背景颜色
cell.setBackgroundColor(new Color(118, 59, 167));
return cell;
}
/**
* 向单元格添加字符串、设置单元格属性
* @param value 字符
* @param font字体
* @return
*/
public PdfPCell createCell(String value, com.lowagie.text.Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
cell.setBorderColor(new Color(15, 15, 15));
cell.setBackgroundColor(new Color(59,162,167));
return cell;
}
/**
* 向单元格中添加图片
* @param image 图片
*/
public PdfPCell createCell() {
PdfPCell cell = new PdfPCell();
try {
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Image image = Image.getInstance ("D:\\Arrows_Black_InTable_Down.png");
//设置图片大小
//image.scaleAbsoluteWidth(7);
//image.scaleAbsoluteHeight(11);
image.scaleAbsolute(7,11);
//添加图片
cell.addElement(image);
//设置边框颜色
cell.setBorderColor(new Color(15, 15, 15));
//设置单元格背景颜色
cell.setBackgroundColor(new Color(59,162,167));
} catch (Exception e) {
e.printStackTrace();
}
return cell;
}
public PdfPCell createCell(String value, com.lowagie.text.Font font, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPTable createTable(int colNumber) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public PdfPTable createBlankTable() {
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public void generatePDF() throws Exception {
PdfPTable table = createTable(5);
table.addCell(createCell("学生信息列表:", keyfont, Element.ALIGN_LEFT, 5, false));
table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("升降", keyfont, Element.ALIGN_CENTER));
for (int i = 0; i < 5; i++) {
table.addCell(createCell("姓名" + i, textfont));
table.addCell(createCell(i + 15 + "", textfont));
table.addCell(createCell((i % 2 == 0) ? "男" : "女", textfont));
table.addCell(createCell("地址" + i, textfont));
//添加图片
table.addCell(createCell());
}
document.add(table);
document.close();
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\text.pdf");
file.createNewFile();
new PDFReport1(file).generatePDF();
}
}
itextpdf添加表格元素_itext生成pdf文件-表格相关推荐
- itext总页数_itext 生成pdf文件添加页眉页脚
原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...
- itextpdf 5.5.6版本生成pdf文件 字体设置三种方式
1.第一种是引用window本地系统字体(这里以常见的宋体为例) BaseFont bf =BaseFont.createFont("C:/WINDOWS/Fonts/simsun.ttf& ...
- 使用iText生成PDF文件中创建表格
前言 使用iText的JAR包如下 <dependency><groupId>com.itextpdf</groupId><artifactId>ite ...
- SpringBoot实现生成pdf文件(含表格)
参考文章 1. [JAVA] java动态生成PDF文档 2. [iText5 生成PDF]纯Java代码实现生成PDF(自定义表格.文本水印.单元格样式) 3. Java PDF中table的生成和 ...
- Java使用itextpdf生成PDF文件,用浏览器下载
浏览器下载生成PDF文件 1.引入jar包 <dependency><groupId>com.itextpdf</groupId><artifactId> ...
- java用itextPDF生成PDF文件保存至本地并上传至ftp服务器
标题java用itextPDF生成PDF文件保存至本地并上传至ftp服务器 所需jar :itext-asian-5.2.0.jar,itextpdf-5.5.5.jar,commons-net-3. ...
- itextpdf通过pdf模板生成pdf文件
itextpdf通过pdf模板生成pdf文件,设置粗体字体 1.创建pdf模板 2.使用模板生成pdf 3.itext自带的字体列表 4.遇到的坑 1.创建pdf模板 可以使用PDFFescape网站 ...
- 记-ItextPDF+freemaker 生成PDF文件---导致服务宕机
摘要:已经上线的项目,出现服务挂掉的情况. 介绍:该服务是专门做打印的,业务需求是生成PDF文件进行页面预览,主要是使用ItextPDF+freemaker技术生成一系列PDF文件,其中生成流程有:解 ...
- java 生成字体文件,java使用itext生成pdf文件-设置字体,itextpdf,import com.l
java使用itext生成pdf文件-设置字体,itextpdf,import com.limport com.lowagie.text.Document;import com.lowagie.tex ...
最新文章
- 怎么一个好的短信验证码接口接入到自己的企业网站和APP程序当中选择
- XSS挑战之旅闯关笔记
- cmd orcal 中文乱码
- 深度学习核心技术精讲100篇(十二)-DCGAN(对抗生成网络)算法应用及代码实现
- APICloud开发者进阶之路 | 超级实用技巧
- python print format
- 20172303 2017-2018-2 《程序设计与数据结构》实验五报告
- SQL中自增(AUTO_INCREMENT)字段介绍
- JNI 概述 (翻译)
- python判断不等_Python爬取620首虾米歌曲,揭秘五月天为什么狂吸粉?!
- (转载)New poker 2总算放出新固件了!
- excel数据导入matlab失败,Excel数据导入matlab
- 5类6类7类网线对比_五类/超五类/六类/超六类/七类等多类网线的比较
- 计算机处理器性能排名,2019电脑cpu处理器性能排名:AMD 32核撕裂者遥遥领先(2)...
- [原创]火箭发动机设计---民科版
- QScrollArea样式设置
- 手Q真的输给微信了吗?
- 区块链发展第三阶段:去中心化金融
- [SWPU2019]Web3
- 0047 抽象类、模板设计模式