package com.example.demo;
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 java.io.FileOutputStream;
class DemoApplicationTests {
public static void main(String[] args) throws Exception {
// 新建document
Document document = new Document();
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 参数是对文档对象 和 文路径
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/18358/Desktop/test.pdf"));
// 打开文档
document.open();
//设置字体
// 系统字体 C:/WINDOWS/Fonts/simkai.ttf 目录下 右键字体属性 SIMFANG.TTF
BaseFont bfChinese = BaseFont.createFont("C:\\Windows.old\\Windows\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//标题文字
Font TitleFont=new Font(bfChinese,20,Font.BOLD);
//加粗文字
Font boldFont=new Font(bfChinese,12,Font.BOLD);
//普通文字
Font font = new Font(bfChinese, 20);
String partition=" ";
//标题
//字体大小
Paragraph title = new Paragraph("标题", TitleFont);
//设置居中
title.setAlignment(Element.ALIGN_CENTER);
//段后间距
title.setSpacingAfter(20f);
//添加到文档
document.add(title);
//耳牌编号
font = new Font(bfChinese,12);
Paragraph earTag = new Paragraph("编号:0862164",boldFont);
//设置居中
earTag.setAlignment(Element.ALIGN_CENTER);
//段后间距
earTag.setSpacingAfter(15f);
//添加到文档
document.add(earTag);
//注册育肥场名称 注册编号
Paragraph lineOne = new Paragraph("名称:",boldFont);
lineOne.add(new Chunk("公司").setUnderline(1f,-5f));
lineOne.add(new Chunk(partition+"注册编号:"));
lineOne.add(new Chunk("HJF048").setUnderline(1f,-5f));
//设置居中
lineOne.setAlignment(Element.ALIGN_CENTER);
//段后间距
lineOne.setSpacingAfter(15f);
//添加到文档
document.add(lineOne);
//认可兽医 检验检疫机关确认
Paragraph lineTwo = new Paragraph(":",boldFont);
lineTwo.add(new Chunk("认可").setUnderline(1f,-5f));
lineTwo.add(new Chunk(partition+"确认:"));
lineTwo.add(new Chunk("确认").setUnderline(1f,-5f));
//设置居中
lineTwo.setAlignment(Element.ALIGN_CENTER);
//段后间距
lineTwo.setSpacingAfter(15f);
//添加到文档
document.add(lineTwo);
//设置表格样式
//创建一个表格指定表格列数为8
PdfPTable table = new PdfPTable(8);
//表格宽度百分比
table.setWidthPercentage(90);
//创建单元格
PdfPCell cell = new PdfPCell();
//设置内容水平垂直居中,设置固定高度
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//cell.setFixedHeight(80);固定高度
cell.setMinimumHeight(80);//设置表格最小高度
// 1row
cell.setPhrase(new Phrase("品\n\n种",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
// 2row
cell.setPhrase(new Phrase("购\n\n进\n\n情\n\n况",boldFont));
cell.setRowspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("购进时间",boldFont));
cell.setRowspan(1);
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("淘",boldFont));
table.addCell(cell);
//3row
cell.setPhrase(new Phrase("18-03-01",font));
table.addCell(cell);
cell.setPhrase(new Phrase("450kg",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("无",font));
table.addCell(cell);
//4row
cell.setPhrase(new Phrase("育\n\n肥\n\n期\n\n情\n\n况",boldFont));
cell.setRowspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
cell.setRowspan(1);
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("法",boldFont));
cell.setColspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("处理方法",boldFont));
table.addCell(cell);
//5row
cell.setPhrase(new Phrase("18-3-11/6-21",font));
cell.setColspan(1);
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
cell.setColspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("无",font));
table.addCell(cell);
//6row
cell.setPhrase(new Phrase("出",boldFont));
cell.setColspan(1);
cell.setRowspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("时间",boldFont));
cell.setRowspan(1);
table.addCell(cell);
cell.setPhrase(new Phrase("出",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("起止时间",boldFont));
table.addCell(cell);
cell.setPhrase(new Phrase("隔离结果",boldFont));
cell.setColspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
cell.setColspan(1);
table.addCell(cell);
cell.setPhrase(new Phrase("",boldFont));
table.addCell(cell);
//7row
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("",font));
table.addCell(cell);
cell.setPhrase(new Phrase("健康",font));
cell.setColspan(2);
table.addCell(cell);
cell.setPhrase(new Phrase("深圳",font));
cell.setColspan(1);
table.addCell(cell);
cell.setPhrase(new Phrase("无",font));
table.addCell(cell);
document.add(table);
// 5.关闭文档
document.close();
}
}
Springboot整合itextpdf生成Pdf
最新推荐文章于 2024-05-24 10:01:44 发布