新项目里有个pdf的生成文件,折腾了两天,终于可以 用了,现在 记下来,方便后来者。
有些代码重复的地方,大家凑合着看了,项目太急,也不想优化了。
里面大部分地方都做了注释,一看就会明白了。
废话不多说,直接上代码
先添加依赖
5.5.12
com.itextpdf
itextpdf
${itextpdf.version}
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.util.ResourceUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PdfUtil {
public static final Document document = new Document(PageSize.A4, 36, 72, 58, 100);
public void createPdf() {
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("b.pdf"));
document.open();
document.newPage();
// 标题
document.add(new Paragraph("Customer Name and DL Number in header"));
// 中间段落
document.add(midelParagraph());
document.add(getParagraph2("1801W Jefferson"));
document.add(getParagraph2("Phonenix AZ 85007"));
document.add(new Paragraph("\n"));
//第一个table
document.add(firstTable(null, null, null, null, null, null));
document.add(new Paragraph("\n"));
//第二个table
document.add(secondTable(null, null, null, null, null, null, null));
document.add(new Paragraph("\n"));
//第三个table
document.add(thirdTable(null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null));
document.add(new Paragraph("\n"));
document.newPage();
document.add(new Paragraph("Customer Name and DL Number in header"));
document.add(new Paragraph("\n"));
document.add(fourth(null, null, null, null, null, null));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(fifthTitle());
document.add(fifth());
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(sixthTitle());
document.add(sixth());
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(seventhTitle("Saturday,July 15,2017"));
document.add(sixth());
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(seventhTitle("Thursday,Auguest17,2017"));
List strs = new ArrayList<>();
strs.add("Event Row");
strs.add("Time");
strs.add("Description");
strs.add("BAC");
document.add(titleListFour(strs));
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置下划线
*
* @return
*/
public static Chunk getChunk(String str) {
// 创建Chunk对象,设置下划线的厚度为0.1
Chunk underline = new Chunk(str);
underline.setUnderline(0.1f, -1f);
// 创建Chunk对象,设置下划线的厚度为1
Chunk strike = new Chunk(str);
strike.setUnderline(1f, 3f);
return underline;
}
/**
* 设置段落居中
*
* @return
*/
public static Paragraph getParagraph(String str) {
Paragraph pg = new Paragraph(str, FontFactory.getFont(FontFactory.COURIER, 16, Font.BOLD));
pg.setAlignment(1);
return pg;
}
/**
* 设置下划线段落居中
*
* @param str
* @return
*/
public static Paragraph getParagraph2(String str) {
Paragraph pg = new Paragraph(getChunk(str));
pg.setAlignment(1);
return pg;
}
/**
* 设置下划线段落居中加大字体
*
* @param str
* @return
*/
public static Paragraph getParagraph3(String str) {
Paragraph pg = new Paragraph(getChunk(str));
pg.setFont(FontFactory.getFont(FontFactory.COURIER, 18, Font.BOLD));
pg.setAlignment(1);
return pg;
}
/**
* 中间段落
*
* @return
* @throws
*/
public static Paragraph midelParagraph() {
Paragr