入门
添加依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
初体验代码
public class pdfTest {
public static void main(String[] args) throws FileNotFoundException,
DocumentException {
//实现A4纸页面 并且横向显示(不设置则为纵向)
// 1.新建document对象
Document document = new Document(PageSize.A4);// 建立一个Document对象
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream("d:/demo.pdf"));
// 打开文档
document.open();
// 生成第一页
document.newPage();
//设置作者
document.addAuthor("tomaotcc");
//设置创建日期
document.addCreationDate();
// 设置创建者
document.addCreator("tomaotcc.com");
// 设置生产者
document.addProducer();
// 设置关键字
document.addKeywords("my");
//设置标题
document.addTitle("Set Attribute Example");
//设置主题
document.addSubject("An example to show how attributes can be added to pdf files.");
// 加入文档内容
document.add(new Paragraph("my first pdf demo"));
// 关闭文档
document.close();
pdfWriter.close();
}
}
测试
package com.fawkes.project.example.common.utils.pdf;
import com.fawkes.project.example.common.model.DamMomAttachmentInfo;
import com.fawkes.project.example.common.model.DamMomRiskInfo;
import com.fawkes.project.example.common.utils.DateUtils;
import com.fawkes.project.example.common.utils.ToolUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class a {
// 定义全局的字体静态变量
private static Font titlefont; //标题字体样式
private static Font headfont;
private static Font keyfont;
private static Font textfont;
// 最大宽度
private static int maxWidth = 520;
// 静态代码块
static {
try {
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new Font(bfChinese, 16, Font.BOLD);
headfont = new Font(bfChinese, 14, Font.BOLD);
keyfont = new Font(bfChinese, 10, Font.BOLD);
textfont = new Font(bfChinese, 10, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
// 1.新建document对象
Document document = new Document(PageSize.A4);// 建立一个Document对象
document.setPageCount(1);//设置生成pdf页数 超过一页会自动增加
// 2.建立一个书写器(Writer)与document对象关联
File file = new File("D:\\PDFDemo.pdf");
file.createNewFile();
PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream(file));
// 3.打开文档
document.open();
//4.生成PDF
generatePDF(document);
// 5.关闭文档
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void generatePDF(Document document) throws DocumentException, IOException {
// 段落
Paragraph title = new Paragraph( "课程表", titlefont);
title.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
title.setIndentationLeft(12); //设置左缩进
title.setIndentationRight(12); //设置右缩进
title.setFirstLineIndent(24); //设置首行缩进
title.setLeading(20f); //行间距
title.setSpacingBefore(5f); //设置段落上空白
title.setSpacingAfter(10f); //设置段落下空白
document.add(title);
// 设置一级段落
Paragraph p1 = new Paragraph("一:课程要求", headfont);
p1.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右
p1.setIndentationLeft(6); //设置左缩进
p1.setIndentationRight(6); //设置右缩进
p1.setFirstLineIndent(4); //设置首行缩进
p1.setLeading(20f); //行间距
p1.setSpacingBefore(5f); //设置段落上空白
p1.setSpacingAfter(10f); //设置段落下空白
document.add(p1);
Paragraph textTianmu = new Paragraph("大家在平时一定要积极佩戴口罩,做好各种防护措施,减少感染新冠病毒的问题,从而让自己更加健康。", textfont);
textTianmu.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右
textTianmu.setIndentationLeft(6); //设置左缩进
textTianmu.setIndentationRight(6); //设置右缩进
textTianmu.setFirstLineIndent(4); //设置首行缩进
textTianmu.setLeading(20f); //行间距
textTianmu.setSpacingBefore(5f); //设置段落上空白
textTianmu.setSpacingAfter(10f); //设置段落下空白
document.add(textTianmu);
Paragraph textTable = new Paragraph("课程内容", keyfont);
textTable.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
textTable.setIndentationLeft(12); //设置左缩进
textTable.setIndentationRight(12); //设置右缩进
textTable.setFirstLineIndent(24); //设置首行缩进
textTable.setLeading(18f); //行间距
textTable.setSpacingBefore(5f); //设置段落上空白
textTable.setSpacingAfter(10f); //设置段落下空白
document.add(textTable);
// 表格 列数 宽度
PdfPTable table = createTable(new float[]{30, 30, 30, 90, 30, 20,80});
//table.addCell(createCell("美好的一天", headfont, Element.ALIGN_LEFT, 6, 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));
table.addCell(createCell("第六列", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("图片", keyfont, Element.ALIGN_CENTER));
Integer totalQuantity = 0;
for (int i = 0; i < 6 ; i++) {
table.addCell(createCell( "第一节", textfont));
table.addCell(createCell( "第二节", textfont));
table.addCell(createCell( "第三节", textfont));
table.addCell(createCell( "第四节", textfont));
table.addCell(createCell( "第五节", textfont));
table.addCell(createCell( "第六节", textfont));
Image image = Image.getInstance( "https://pics7.baidu.com/feed/b812c8fcc3cec3fd8830334286e7b23487942771.jpeg@f_auto?token=20db41fe820073a641986b73f4cbd20f");
image.setAlignment(Image.ALIGN_LEFT);
image.scalePercent(20); //依照比例缩放
image.setAbsolutePosition(40, 60);
table.addCell(createCell(image, null, 1, 1, 3));
totalQuantity++;
}
table.addCell(createCell( "合计", textfont));
table.addCell(createCell( " ", textfont));
table.addCell(createCell( " ", textfont));
table.addCell(createCell( " ", textfont));
table.addCell(createCell( " ", textfont));
table.addCell(createCell( " ", textfont));
table.addCell(createCell( "100", textfont));
document.add(table);
//设置图片
Image image = Image.getInstance( "https://pics7.baidu.com/feed/b812c8fcc3cec3fd8830334286e7b23487942771.jpeg@f_auto?token=20db41fe820073a641986b73f4cbd20f");
image.setAlignment(Image.ALIGN_LEFT);
image.scalePercent(40); //依照比例缩放
// image.setAbsolutePosition(40, 60);
document.add(image);
Paragraph p2 = new Paragraph();
p2.add(new Chunk(new DottedLineSeparator()));
Image image2 = Image.getInstance( "https://pics7.baidu.com/feed/b812c8fcc3cec3fd8830334286e7b23487942771.jpeg@f_auto?token=20db41fe820073a641986b73f4cbd20f");
image2.setAlignment(Image.ALIGN_LEFT);
image2.scalePercent(40); //依照比例缩放
//image2.setAbsolutePosition(80, 120);
document.add(image2);
document.add( p2);
Paragraph p11 = new Paragraph();
p11.add(new Chunk(new LineSeparator()));
document.add(image);
document.add( p11);
Paragraph textTianmu1 = new Paragraph("大家在平时一定要积极佩戴口罩,做好各种防护措施,减少感染新冠病毒的问题,从而让自己更加健康。", textfont);
textTianmu1.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右
textTianmu1.setIndentationLeft(6); //设置左缩进
textTianmu1.setIndentationRight(6); //设置右缩进
textTianmu1.setFirstLineIndent(4); //设置首行缩进
textTianmu1.setLeading(20f); //行间距
textTianmu1.setSpacingBefore(5f); //设置段落上空白
textTianmu1.setSpacingAfter(10f); //设置段落下空白
document.add(textTianmu1);
// 直线
//Paragraph p2 = new Paragraph();
// p1.add(new Chunk(new LineSeparator()));
// 点线
//Paragraph p2 = new Paragraph();
//p2.add(new Chunk(new DottedLineSeparator()));
// 超链接
// Anchor anchor = new Anchor("baidu");
// anchor.setReference("www.baidu.com");
// 定位
// Anchor gotoP = new Anchor("goto");
// gotoP.setReference("#top");
}
/**
* 设置图片表格
*
* @param image
* @param font
* @param align
* @param colspan
* @param rowspan
* @return
*/
public static PdfPCell createCell(Image image, Font font, int align, int colspan, int rowspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setImage(image);
return cell;
}
/**
* 创建指定列宽、列数的表格
*
* @param widths
* @return
*/
public static 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;
}
/**
* 创建单元格(指定字体、水平..)
*
* @param value 内容
* @param font 字体
* @param align
* @return
*/
public static PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/**
* 设置基本表格
*
* @param value 内容
* @param font 字体
* @return
*/
public static PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
return cell;
}
}
文件如下