前言

需求:根据存储的行为记录,生成附件,附件样式如下:

字段17及之后的是需要循环生成的。

java pdfbox动态生成页码 java生成pdf报表_java pdfbox动态生成页码

后台代码

1、添加Maven依赖

<!-- PDF工具类 -->
		  <dependency>
		   <groupId>com.itextpdf</groupId>
		   <artifactId>itextpdf</artifactId>
		   <version>5.5.13</version>
		  </dependency>
		  <!-- PDF中文支持 -->
		  <dependency>
			  <groupId>com.itextpdf</groupId>
			  <artifactId>itext-asian</artifactId>
			  <version>5.2.0</version>
		  </dependency>
		<dependency>
			<groupId>com.itextpdf.tool</groupId>
			<artifactId>xmlworker</artifactId>
			<version>5.5.13.1</version>
		</dependency>

2、appication.properties

# 自定义存储或读取路径
# 注:自定义路径时,默认的四个文件夹下中的“META-INF下的resoures文件夹”仍然有效,其他三个文件夹失效
# 注:搜索文件时,自定义的文件夹的优先级要高于默认的四个文件夹
web.upload-path=C:/PDF/
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,file:{web.upload-path}
springboot.mvc.static-path-pattern=/**

3、后端代码

@Value("${web.upload-path}")
private String uploadPath;
public String generateOrgDocumentPdf() throws AppException, IOException, DocumentException {
		String newPDFPath = uploadPath  + "信息采集表.pdf";
		createTableFile(newPDFPath );
		return newPDFPath;
    }
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.*;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itextpdf.text.pdf.PdfWriter;
/**
     * 方法简介:生成PDF
     *
     * 作者:     tcy
     * 时间:     2022-02-28
     */
    public void createTableFile(String newPDFPath) throws AppException, IOException, DocumentException {

        //生成PDF文档

        //新建Document对象,并打开
        Document document = new Document(PageSize.A4);

        try{
            //创建文件
            File file = new File(newPDFPath);
            
            PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(file));
           
            //文档属性
            document.addTitle("Titl");
            document.addAuthor("Author");
            document.addSubject("Subject");
            document.addKeywords("Keywords");
            document.addCreator("Creator");
            //页边空白
            document.setMargins(60, 60, 80, 40);
            document.open();

            //添加内容
            //1、标题
            document.add(PdfFontUtils.getFont(1, "信息采集表"));
            //创建一个空行
//            document.add(Chunk.NEWLINE );

            //2、表格
            //2.1 设置表格列数:6
            PdfPTable table = new PdfPTable(6);
            //2.2 指定表格每一列宽度
            table.setWidths(new float[] {0.15f,0.15f,0.15f,0.15f,0.15f,0.25f});

            //2.3添加第一行内容
            //2.3.1添加第1列内容
            PdfPCell cell = new PdfPCell(PdfFontUtils.getFont(7, "字段1"));
            //设置第一行第一列背景颜色
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            table.addCell(cell);
            //2.3.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            table.addCell(cell);
            //2.3.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段2"));
            //设置背景颜色
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            table.addCell(cell);
            //2.3.4添加第4列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            table.addCell(cell);
            //2.3.5添加第5列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段3"));
            //设置背景颜色
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            table.addCell(cell);
            //2.3.6添加第6列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            table.addCell(cell);

            //2.4添加第2行,跨所有行
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段4"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(6);
            table.addCell(cell);

            //2.5添加第三行内容
            //2.5.1添加第1列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段5"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(3);
            table.addCell(cell);
            //2.5.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7,"字段6"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(2);
            table.addCell(cell);
            //2.5.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段7"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            table.addCell(cell);

            //2.6添加第四行内容
            //2.6.1添加第1列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7,  "内容"));
            //设置跨行数
            cell.setColspan(3);
            table.addCell(cell);
            //2.6.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7,  "内容"));
            //设置跨行数
            cell.setColspan(2);
            table.addCell(cell);
            //2.6.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7,  "内容"));
            table.addCell(cell);

            //2.7添加第五行内容
            //2.7.1添加第1列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段8"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(2);
            table.addCell(cell);
            //2.7.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段9"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(3);
            table.addCell(cell);
            //2.7.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7,"字段10"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            table.addCell(cell);

            //2.8添加第六行内容
            //2.8.1添加第1列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            //设置跨行数
            cell.setColspan(2);
            table.addCell(cell);
            //2.8.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            //设置跨行数
            cell.setColspan(3);
            table.addCell(cell);
            //2.8.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            table.addCell(cell);

            //2.9添加table
            document.add(table);

            //3、新建一个表-8列
            table = new PdfPTable(8);
            //3.1添加第1行,跨所有行
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段11"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(8);
            table.addCell(cell);

            //3.2添加第2行内容
            //3.2.1添加第1列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段12"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            cell.setColspan(2);
            table.addCell(cell);
            //3.2.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段13"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            cell.setColspan(2);
            table.addCell(cell);
            //3.2.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段14"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            cell.setColspan(2);
            table.addCell(cell);
            //3.2.4添加第4列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段15"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            cell.setColspan(2);
            table.addCell(cell);

            //3.3添加第3行内容
            //3.3.1添加第1列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            cell.setColspan(2);
            table.addCell(cell);
            //3.3.2添加第2列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            cell.setColspan(2);
            table.addCell(cell);
            //3.3.3添加第3列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            cell.setColspan(2);
            table.addCell(cell);
            //3.3.4添加第4列内容
            cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
            cell.setColspan(2);
            table.addCell(cell);

            //3.4添加第4行,跨所有行
            cell = new PdfPCell(PdfFontUtils.getFont(7, "字段16"));
            cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
            //设置跨行数
            cell.setColspan(8);
            table.addCell(cell);

            //循环加入行为记录表格-length:需要循环添加的数据的多少
            for (int i = 1; i <= length; i++){

                //取数字的中文表达
                String num = this.getNumberChinese(Integer.toString(i));
                //3.5添加第5行(字段17),跨所有行-------我这里的字段17格式类似于:(一)行为一
                cell = new PdfPCell(PdfFontUtils.getFont(6, "(" + num + ")、行为" + num));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                //设置跨行数
                cell.setColspan(8);
                table.addCell(cell);

                //3.6添加第6行
                //3.6.1添加第1列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段18"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                cell.setColspan(2);
                table.addCell(cell);
                //3.6.2添加第2列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段19"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                cell.setColspan(2);
                table.addCell(cell);
                //3.6.3添加第3列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段20"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                cell.setColspan(2);
                table.addCell(cell);
                //3.6.4添加第4列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段21"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                cell.setColspan(2);
                table.addCell(cell);

                //3.7添加第7行
                //3.7.1添加第1列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                cell.setColspan(2);
                table.addCell(cell);
                //3.7.2添加第2列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                cell.setColspan(2);
                table.addCell(cell);
                //3.7.3添加第3列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                cell.setColspan(2);
                table.addCell(cell);
                //3.7.4添加第4列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7,"内容"));
                cell.setColspan(2);
                table.addCell(cell);

                //3.8添加第8行,跨所有行
                cell = new PdfPCell(PdfFontUtils.getFont(6, "字段22"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                //设置跨行数
                cell.setColspan(8);
                table.addCell(cell);

                //3.10添加第9行
                //3.10.1添加第1列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段23"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.10.2添加第2列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.10.3添加第3列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段24"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.10.4添加第4列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.10.5添加第5列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段25"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.10.6添加第6列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.10.7添加第7列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段26"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.10.8添加第8列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7,"内容"));
                table.addCell(cell);

                //3.11添加第10行
                //3.11.1添加第1列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段27"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.11.2添加第2列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.11.3添加第3列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段28"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.11.4添加第4列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.11.5添加第5列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段29"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.11.6添加第6列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.11.7添加第7列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段30"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.11.8添加第8列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);

                //3.12添加第11行
                //3.12.1添加第1列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段31"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.12.2添加第2列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.12.3添加第3列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段32"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.12.4添加第4列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.12.5添加第5列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段33"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.12.6添加第6列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.12.7添加第7列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段34"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.12.8添加第8列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);

                //3.13添加第12行
                //3.13.1添加第1列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段35"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.13.2添加第2列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.13.3添加第3列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段36"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.13.4添加第4列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.13.5添加第5列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段37"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.13.6添加第6列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);
                //3.13.7添加第7列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "字段38"));
                cell.setBackgroundColor(new BaseColor(0xC0,0xC0,0xC0));
                table.addCell(cell);
                //3.13.8添加第8列内容
                cell = new PdfPCell(PdfFontUtils.getFont(7, "内容"));
                table.addCell(cell);

            }

            //3.14添加table
            document.add(table);
        }catch (Exception e){
            e.printStackTrace();
        }

        //4、关闭document
        document.close();
    }

    /**
     * 方法简介:将数字123转为中文格式一二三
     *
     * 作者:     tcy
     * 时间:     2022-02-28
     */
    private String getNumberChinese(String number) {

        String result = "";
        //存储数字与中文表示的键值对
        Map<String,String> map= new HashMap<String,String>();
        map.put("1","一");
        map.put("2","二");
        map.put("3","三");
        map.put("4","四");
        map.put("5","五");
        map.put("6","六");
        map.put("7","七");
        map.put("8","八");
        map.put("9","九");

        int len = number.length();
        if (len == 1){
            result = map.get(number);
        }else if (len == 2){
            result = map.get(number.substring(0,1)) + "十" + map.get(number.substring(1,2));
        }

        return result;
    }

4、PdfFontUtils 工具类

import java.io.IOException;
import java.net.URL;
import org.springframework.core.io.ClassPathResource;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;

public class PdfFontUtils {
         
    /**
     * 文档排版
     */
    public static Paragraph getFont(int type, String text) throws IOException{
    	BaseFont songti = null;
    	BaseFont heiti = null;
    	BaseFont fangsong = null;
    	URL url = new ClassPathResource("META-INF/resources/static/font/STZHONGS.TTF").getURL();
    	String song = url.getPath();
    	url = new ClassPathResource("META-INF/resources/static/font/SIMHEI.TTF").getURL();
    	String hei = url.getPath();
    	url = new ClassPathResource("META-INF/resources/static/font/STFANGSO.TTF").getURL();
    	String fang = url.getPath();
        try {
            /**
             * 设置字体
             * 
             * windows路径字体
             * FONT_TYPE=C:/Windows/fonts/simsun.ttc
             * linux路径字体 宋体 (如果没有这个字体文件,就将windows的字体传上去)
             * FONT_TYPE=/usr/share/fonts/win/simsun.ttc
             */
        	songti = BaseFont.createFont(song, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        	heiti = BaseFont.createFont(hei, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        	fangsong = BaseFont.createFont(fang, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font font1 = new Font(songti);
        Font font2 = new Font(heiti);
        Font font3 = new Font(fangsong);
        if(1 == type){//1-标题
            font1.setSize(22f);
        } else if(2 == type){//2-标题一
        	font2.setSize(16f);
        } else if(3 == type){//3-标题二
        	font3.setSize(16f);
        } else if(4 == type){//4-标题三
        	font3.setSize(16f);
        } else if(5 == type){//5-正文
        	font3.setSize(10.5f);
        } else if(6 == type){//6-左对齐
        	font3.setSize(10.5f);
        } else {
        	font3.setSize(10.5f);//默认大小
        }
        //注: 字体必须和 文字一起new
        Paragraph paragraph = null;
        if(1 == type){
        	paragraph = new Paragraph(text, font1);
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
            paragraph.setSpacingBefore(40f);//上间距
            paragraph.setSpacingAfter(30f);//下间距
        } else if(2 == type){//2-标题一
        	paragraph = new Paragraph(text, font2);
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认
            paragraph.setFirstLineIndent(40);
            paragraph.setSpacingBefore(20f);//上间距
            paragraph.setSpacingAfter(30f);//下间距
        } else if(3 == type){
        	paragraph = new Paragraph(text, font3);
        	 paragraph.setFirstLineIndent(40);
            paragraph.setSpacingBefore(20f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(4 == type){//4-标题三
        	paragraph = new Paragraph(text, font3);
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFirstLineIndent(40);
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
            paragraph.setLeading(40f);//设置行间距
        } else if(5 == type){
        	paragraph = new Paragraph(text, font3);
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED); 
            paragraph.setFirstLineIndent(40);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(6 == type){//左对齐
        	paragraph = new Paragraph(text, font3);
            paragraph.setAlignment(Element.ALIGN_LEFT); 
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        }else if(7 == type){
            paragraph = new Paragraph(text, font3);
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        }
        return paragraph;
    }
}