以下的方法只是一个模板,有些功能并没有完全覆盖。可根据自己的需求查询其他资料。
1.引入jar包或者pom依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2.工具类 PDFCommon.java
package com.pdf.common;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
public class PDFCommon {
// 生成空表格
public static PdfPCell getEmptyCell() throws Exception {
PdfPCell pdfPCell = getBodyPdfPCell_center();
pdfPCell.setPhrase(new Paragraph(" ", getPdfBodyFont()));
return pdfPCell;
}
// 设置表体的表格样式 字体居右
public static PdfPCell getBodyPdfPCell_right() {
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setMinimumHeight(12);
pdfPCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
// 设置表体的表格样式 字体居左
public static PdfPCell getBodyPdfPCell_left() {
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setMinimumHeight(12);
pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
public static PdfPCell getBodyPdfPCell_image() {
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setMinimumHeight(40);
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
// 设置表体的表格样式 字体居左
public static PdfPCell getBodyPdfPCell_left_top() {
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setMinimumHeight(12);
pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell.setVerticalAlignment(Element.ALIGN_TOP);
return pdfPCell;
}
// 设置表体的表格样式 字体居中
public static PdfPCell getBodyPdfPCell_center() {
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setMinimumHeight(12);
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
// 设置表头
public static Font getPdfTitleFont() throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 14F, Font.NORMAL);
fontChinese.setStyle("bold");
return fontChinese;
}
// 设置表体
public static Font getPdfBodyFont() throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 10F, Font.NORMAL);
return fontChinese;
}
// 设置表体-字体加粗
public static Font getPdfBodyFont_bold() throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 8.5F, Font.NORMAL);
fontChinese.setStyle("bold");
return fontChinese;
}
// 设置下划线 -> 内容下方
public static Chunk setUnderlineBlank(String content, float thickness, float yPosition) throws Exception {
Chunk chunk = new Chunk(content, PDFCommon.getPdfBodyFont());
chunk.setUnderline(thickness, yPosition);
return chunk;
}
}
3.导出的实现方法
package com.pdf;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.pdf.common.PDFCommon;
public class ExportPDF {
public void export(HttpServletResponse response, HttpServletRequest request) throws Exception {
// 创建文件
Document document = new Document();
document.setPageSize(PageSize.A4); // 设置A4
// 导出到浏览器
// response.reset();
// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
// String pdfFileName = "导出pdf-" + PDFCommon.getYYYYMMDD(nowDate);//文件的名称
// response.setHeader("Content-Disposition",
// "attachment;filename=" + URLEncoder.encode(pdfFileName, "UTF-8") + ".pdf");
// // 为了解决在IE浏览器中汉字乱码的问题
// response.setHeader("Last-Modified", new
// SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
//
// PdfWriter writer = PdfWriter.getInstance(document,
// response.getOutputStream());
// 导出到本地
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File("D:/导出pdf.pdf")));
// 设置页面布局
writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
// 打开文件
document.open();
float[] widths = { 125, 125, 125, 170, 125, 160, 125 };// 有几个表格
PdfPTable table = new PdfPTable(widths);
table.setWidthPercentage(95); // 设置表格占A4纸的95%
// 设置表头
PdfPCell total_1 = new PdfPCell();
total_1.setRowspan(1);
total_1.setColspan(10);
total_1.setHorizontalAlignment(Element.ALIGN_CENTER);
total_1.setVerticalAlignment(Element.ALIGN_MIDDLE);
total_1.disableBorderSide(13);//消除周围的表格线
// total_1.setBackgroundColor(BaseColor.YELLOW);
Paragraph paragraph = new Paragraph("导出pdf的表格 \r\n", PDFCommon.getPdfTitleFont());
total_1.setPhrase(paragraph);
table.addCell(total_1);
// 第1行
PdfPCell total_1_1 = PDFCommon.getBodyPdfPCell_center();
total_1_1.setRowspan(1);
total_1_1.setColspan(1);
total_1_1.setPhrase(new Paragraph("人员姓名", PDFCommon.getPdfBodyFont()));
table.addCell(total_1_1);
PdfPCell total_1_2 = PDFCommon.getBodyPdfPCell_center();
total_1_2.setRowspan(1);
total_1_2.setColspan(1);
total_1_2.setPhrase(new Paragraph("张三", PDFCommon.getPdfBodyFont()));
table.addCell(total_1_2);
PdfPCell total_1_3 = PDFCommon.getBodyPdfPCell_center();
total_1_3.setRowspan(1);
total_1_3.setColspan(1);
total_1_3.setPhrase(new Paragraph("身份证号码", PDFCommon.getPdfBodyFont()));
table.addCell(total_1_3);
PdfPCell total_1_4 = PDFCommon.getBodyPdfPCell_center();
total_1_4.setRowspan(1);
total_1_4.setColspan(1);
total_1_4.setPhrase(new Paragraph("100002100110121211", PDFCommon.getPdfBodyFont()));
table.addCell(total_1_4);
PdfPCell total_1_5 = PDFCommon.getBodyPdfPCell_center();
total_1_5.setRowspan(1);
total_1_5.setColspan(1);
total_1_5.setPhrase(new Paragraph("户籍地址", PDFCommon.getPdfBodyFont()));
table.addCell(total_1_5);
PdfPCell total_1_67 = PDFCommon.getBodyPdfPCell_center();
total_1_67.setRowspan(1);
total_1_67.setColspan(2);
total_1_67.setPhrase(new Paragraph("北京市金融街", PDFCommon.getPdfBodyFont()));
table.addCell(total_1_67);
for (int q = 0; q < 8; q++) {// 画8个大表格
PdfPCell total_Q = PDFCommon.getBodyPdfPCell_center();
total_Q.setRowspan(3);// 每一个大表格里有三个小表格
total_Q.setColspan(2);
total_Q.setPhrase(new Paragraph("大表格" + (q + 1), PDFCommon.getPdfBodyFont()));
table.addCell(total_Q);
for (int c = 0; c < 3; c++) {
PdfPCell total_Q_d = PDFCommon.getBodyPdfPCell_left();
total_Q_d.setRowspan(1);
total_Q_d.setColspan(4);
total_Q_d.setPhrase(new Paragraph("小表格" + (c + 1), PDFCommon.getPdfBodyFont()));
table.addCell(total_Q_d);
if (c == 0) {//第一列进行合并
PdfPCell total_Q_score = PDFCommon.getBodyPdfPCell_center();
total_Q_score.setRowspan(3);
total_Q_score.setColspan(1);
Chunk sigUnderline = new Chunk(" " + 5 + " ");
sigUnderline.setUnderline(0.1f, -2f);// 设置下划线
Paragraph pp = new Paragraph("", PDFCommon.getPdfBodyFont());
pp.add(sigUnderline);
pp.add("分");
total_Q_score.setPhrase(pp);
table.addCell(total_Q_score);
}
}
}
// 总分
PdfPCell total_ht = PDFCommon.getBodyPdfPCell_center();
total_ht.setRowspan(1);
total_ht.setColspan(6);
total_ht.setPhrase(new Paragraph("合计", PDFCommon.getPdfBodyFont()));
total_ht.setMinimumHeight(16F);
table.addCell(total_ht);
PdfPCell total_h = PDFCommon.getBodyPdfPCell_center();
total_h.setRowspan(1);
total_h.setColspan(1);
Chunk sigUnderline = new Chunk(" " + 100 + " ");
sigUnderline.setUnderline(0.1f, -2f);// 设置下划线
Paragraph pp = new Paragraph("", PDFCommon.getPdfBodyFont());
pp.add(sigUnderline);
pp.add("分");
total_h.setPhrase(pp);
table.addCell(total_h);
// 大专家意见
PdfPCell total_kb = PDFCommon.getBodyPdfPCell_center();
total_kb.setRowspan(1);
total_kb.setColspan(7);
total_kb.disableBorderSide(2);
total_kb.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
table.addCell(total_kb);
PdfPCell total_zj = PDFCommon.getBodyPdfPCell_left_top();
total_zj.setRowspan(3);
total_zj.setColspan(2);
total_zj.disableBorderSide(9);
total_zj.setPhrase(new Paragraph(" 大专家:", PDFCommon.getPdfBodyFont()));
table.addCell(total_zj);
PdfPCell total_zj_1 = PDFCommon.getBodyPdfPCell_left();
total_zj_1.setRowspan(1);
total_zj_1.setColspan(5);
total_zj_1.disableBorderSide(7);
total_zj_1.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
table.addCell(total_zj_1);
// 专家签字图片
Image zjImage = loadingPicture();
PdfPCell imCell = PDFCommon.getBodyPdfPCell_image();
imCell.setPhrase(new Paragraph(new Chunk(zjImage, 0, 0)));
imCell.setHorizontalAlignment(Element.ALIGN_LEFT);
imCell.setRowspan(1);
imCell.setColspan(5);
imCell.disableBorderSide(7);
table.addCell(imCell);
// 专家评估日期
PdfPCell total_zjdate = PDFCommon.getBodyPdfPCell_right();
total_zjdate.setRowspan(1);
total_zjdate.setColspan(5);
total_zjdate.disableBorderSide(5);
total_zjdate.setPhrase(
new Paragraph("第一个时间: " + PDFCommon.getYYYYMMDD_f(new Date()), PDFCommon.getPdfBodyFont()));
table.addCell(total_zjdate);
// 集体评审
PdfPCell total_jtyj_1 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_1.setRowspan(1);
total_jtyj_1.setColspan(7);
total_jtyj_1.disableBorderSide(2);
total_jtyj_1.setPhrase(new Paragraph("小专家意见: ", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_1);
PdfPCell total_jtyj_2 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_2.setRowspan(1);
total_jtyj_2.setColspan(7);
total_jtyj_2.disableBorderSide(3);
;
total_jtyj_2.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_2);
PdfPCell total_jtyj_3_1 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_3_1.setRowspan(1);
total_jtyj_3_1.setColspan(2);
total_jtyj_3_1.disableBorderSide(11);
total_jtyj_3_1.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_3_1);
PdfPCell total_jtyj_3_2 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_3_2.setRowspan(1);
total_jtyj_3_2.setColspan(2);
total_jtyj_3_2.disableBorderSide(15);
total_jtyj_3_2.setPhrase(new Paragraph("通过 [ √ ]", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_3_2);
PdfPCell total_jtyj_3_3 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_3_3.setRowspan(1);
total_jtyj_3_3.setColspan(3);
total_jtyj_3_3.disableBorderSide(7);
total_jtyj_3_3.setPhrase(new Paragraph("不通过 [ ]", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_3_3);
// 补充意见 头
PdfPCell total_jtyj_4_1 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_4_1.setRowspan(1);
total_jtyj_4_1.setColspan(7);
total_jtyj_4_1.disableBorderSide(3);
total_jtyj_4_1.setPhrase(new Paragraph("补充意见:", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_4_1);
// 补充意见 体
PdfPCell total_jtyj_5_1 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_5_1.setRowspan(1);
total_jtyj_5_1.setColspan(7);
total_jtyj_5_1.disableBorderSide(3);
total_jtyj_5_1.setPhrase(
new Paragraph(new Paragraph(" 专家的意见是通过! \r\n \r\n ", PDFCommon.getPdfBodyFont())));
table.addCell(total_jtyj_5_1);
// 专家
PdfPCell total_jtyj_6_1 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_6_1.setRowspan(1);
total_jtyj_6_1.setColspan(2);
total_jtyj_6_1.disableBorderSide(11);
total_jtyj_6_1.setPhrase(new Paragraph("专家1: ", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_6_1);
// 专家
PdfPCell total_jtyj_6_2 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_6_2.setRowspan(1);
total_jtyj_6_2.setColspan(2);
total_jtyj_6_2.disableBorderSide(15);
total_jtyj_6_2.setPhrase(new Paragraph(" 专家2:", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_6_2);
// 专家
PdfPCell total_jtyj_6_3 = PDFCommon.getBodyPdfPCell_left();
total_jtyj_6_3.setRowspan(1);
total_jtyj_6_3.setColspan(3);
total_jtyj_6_3.disableBorderSide(7);
total_jtyj_6_3.setPhrase(new Paragraph("专家3:", PDFCommon.getPdfBodyFont()));
table.addCell(total_jtyj_6_3);
// 图片1
Image image_1 = loadingPicture();
PdfPCell imCell_1 = PDFCommon.getBodyPdfPCell_image();
imCell_1.setRowspan(1);
imCell_1.setColspan(2);
imCell_1.setPhrase(new Paragraph(new Chunk(image_1, 0, 0)));
imCell_1.disableBorderSide(11);
table.addCell(imCell_1);
// 图片2
Image image_2 = loadingPicture();
PdfPCell imCell_2 = PDFCommon.getBodyPdfPCell_image();
imCell_2.setRowspan(1);
imCell_2.setColspan(2);
imCell_2.setPhrase(new Paragraph(new Chunk(image_2, 0, 0)));
imCell_2.disableBorderSide(15);
table.addCell(imCell_2);
// 图片3
Image image_3 = loadingPicture();
PdfPCell imCell_3 = PDFCommon.getBodyPdfPCell_image();
imCell_3.setRowspan(1);
imCell_3.setColspan(3);
imCell_3.setPhrase(new Paragraph(new Chunk(image_3, 0, 0)));
imCell_3.disableBorderSide(7);
table.addCell(imCell_3);
// 专家评估日期
PdfPCell total_jtdate = PDFCommon.getBodyPdfPCell_right();
total_jtdate.setRowspan(1);
total_jtdate.setColspan(7);
total_jtdate.disableBorderSide(1);
total_jtdate
.setPhrase(new Paragraph("第二个时间: " + PDFCommon.getYYYYMMDD_f(new Date()), PDFCommon.getPdfBodyFont()));
table.addCell(total_jtdate);
document.add(table);
document.close();
}
// 查询图片组装image
private Image loadingPicture() throws BadElementException, MalformedURLException, IOException {
File file = new File("E:/aa.png");
byte[] by = File2byte(file);
Image image = Image.getInstance(by);
image.scaleAbsolute(60, 20);// 调整图片大小(宽度 高度)
return image;
}
private byte[] File2byte(File tradeFile) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
}
4.测试方法
public static void main(String[] args) {
ExportPDF exportPDF = new ExportPDF();
try {
exportPDF.export(null, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
5.导出的效果