不熟悉的基础的可参考:
itext 生成 PDF(一)_itextpdf_猪之仆的博客-CSDN博客
itext 生成 PDF(二)_itext2_猪之仆的博客-CSDN博客
itext 生成 PDF(四) 表格绝对定位_猪之仆的博客-CSDN博客
itext 生成 PDF(五) 使用外部字体_猪之仆的博客-CSDN博客
public class PdfFontTestController {
public static Map<String, BaseFont> baseFontMap = new HashMap<>(30);
static {
try {
baseFontMap.put("bfChinese", BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
baseFontMap.put("LiSu",BaseFont.createFont("C:\\Windows\\Fonts\\SIMLI.TTF", BaseFont.IDENTITY_H,BaseFont.EMBEDDED)); //隶书
baseFontMap.put("fangsong",BaseFont.createFont("C:\\Windows\\Fonts\\simfang.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED)); //仿宋
}catch (Exception e){
log.error(e.toString());
}
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\2.pdf");
file.createNewFile();
Document document = new Document(PageSize.A4, 70, 70, 50, 60);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
Paragraph paragraph = new Paragraph("1111中国00", new Font(baseFontMap.get("bfChinese"), 12, Font.UNDEFINED));
document.add(paragraph);
// Paragraph paragraph1 = new Paragraph("33333中国00", new Font(baseFontMap.get("bfChinese1"), 12, Font.UNDEFINED));
// document.add(paragraph1);
Paragraph paragraph2 = new Paragraph("11你是中国人000", new Font(baseFontMap.get("LiSu"), 12, Font.NORMAL));
document.add(paragraph2);
Paragraph paragraph2S = new Paragraph("11你是中国人00000", new Font(baseFontMap.get("fangsong"), 12, Font.NORMAL));
document.add(paragraph2S);
//横向
setPageSizeHen(document);
document.newPage();
Font liSu = new Font(baseFontMap.get("LiSu"), 12, Font.NORMAL);
PdfPTable table1 = PdfTable.createTable(new int[]{1});
table1.addCell(PdfTable.createCellCommon("检查科室与收费项目", liSu, 1, PdfTable.notBor, PdfTable.paddingFive, 1, null));
document.add(table1);
PdfPTable table2 = PdfTable.createTable(new int[]{1, 2, 1, 1});
table2.addCell(PdfTable.createCellCommon("检查科室", liSu, 1, PdfTable.borZS, PdfTable.paddingFive,1, null));
table2.addCell(PdfTable.createCellCommon("收费项目名称", liSu, 1, PdfTable.borZS, PdfTable.paddingFive,1, null));
table2.addCell(PdfTable.createCellCommon("检查状态", liSu, 1, PdfTable.borZS, PdfTable.paddingFive,1, null));
table2.addCell(PdfTable.createCellCommon("检查日期", liSu, 1, PdfTable.borZYS, PdfTable.paddingFive,1, null));
document.add(table2);
//纵向
setPageSizeShu(document);
document.newPage();
Paragraph paragraph3 = new Paragraph("11你是中国人000", new Font(baseFontMap.get("LiSu"), 12, Font.NORMAL));
document.add(paragraph3);
document.close();
System.out.println("Text added to pdf ..");
}
/**
* @Author
* @Description 设置页面横向
* @Date
* @param
* @return
**/
public static void setPageSizeHen(Document document){
//横向
Rectangle pageSize = new Rectangle(PageSize.A4.getHeight(), PageSize.A4.getWidth());
pageSize.rotate();
document.setPageSize(pageSize);
}
/**
* @Author
* @Description 设置页面竖向
* @Date
* @param
* @return
**/
public static void setPageSizeShu(Document document){
//竖向
Rectangle pageSize = new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight());
pageSize.rotate();
document.setPageSize(pageSize);
}
}
输出效果: