其他分享
首页 > 其他分享> > 自定义PDF导出

自定义PDF导出

作者:互联网

package com.tutor.teacherservice.service;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;

/**
 * @Auther Anear
 * @Date 2021/6/1
 */
public class PdfTest {

    public static void main(String[] args) throws Exception {
        createAllPdf();
    }

    /**
     * 抠模板
     *
     * @throws Exception
     */
    public static void createAllPdf() throws Exception {  // 第一步,实例化一个document对象
        Document document = new Document();
        // 第二步,设置要到出的路径
        FileOutputStream out = new FileOutputStream("D:/workbook111.pdf");
        //如果是浏览器通过request请求需要在浏览器中输出则使用下面方式
        //OutputStream out = response.getOutputStream();
        // 第三步,设置字符
        BaseFont bfChinese = BaseFont.createFont("C:/Users/Anear/IdeaProjects/tutor-system/tutor-teacher-service/src/main/resources/ttf/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font fontZH = new Font(bfChinese, 12.0F, 0);
        Font fontChinese1 = new Font(bfChinese, 16, Font.BOLD);
        // 第四步,将pdf文件输出到磁盘
        PdfWriter writer = PdfWriter.getInstance(document, out);
        // 第五步,打开生成的pdf文件

        document.open();
        // 第六步,设置内容
        String title = "上汽通用汽车员工租车申请表";
        //document.add(new Paragraph(new Chunk("Logo", fontZH)));
        //document.add(new Paragraph(new Chunk(title, fontZH).setLocalDestination(title)));
        PdfPTable titleTable = new PdfPTable(5);
        titleTable.setWidthPercentage(100.0F);

        PdfPCell logoCell = new PdfPCell(new Phrase("LOGO", fontZH));
        logoCell.setColspan(1);
        logoCell.setBorderWidthTop(0);//取消边框展示
        logoCell.setBorderWidthRight(0);
        logoCell.setBorderWidthBottom(0);
        logoCell.setBorderWidthLeft(0);

        //ClassPathResource classPathResource = new ClassPathResource("C:/Users/Anear/IdeaProjects/tutor-system/tutor-teacher-service/src/main/resources/logo/logo.png");
        Image image = Image.getInstance("C:/Users/Anear/IdeaProjects/tutor-system/tutor-teacher-service/src/main/resources/logo/logo.png");
        logoCell.setImage(image);//展示图片logo
        logoCell.setBorderWidth(120);
        logoCell.setFixedHeight(40);
        titleTable.addCell(logoCell);

        PdfPCell titleCell = new PdfPCell(new Phrase(title, fontChinese1));
        titleCell.setColspan(3);
        titleCell.setBorderWidthTop(0);
        titleCell.setBorderWidthRight(0);
        titleCell.setBorderWidthBottom(0);
        titleCell.setBorderWidthLeft(0);
        titleCell.setVerticalAlignment(Element.ALIGN_MIDDLE);//水平垂直居中
        titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        titleTable.addCell(titleCell);
        document.add(titleTable);

        PdfPCell kongCell = new PdfPCell(new Phrase("", fontChinese1));
        kongCell.setBorderWidthTop(0);
        kongCell.setBorderWidthRight(0);
        kongCell.setBorderWidthBottom(0);
        kongCell.setBorderWidthLeft(0);
        titleTable.addCell(kongCell);
        document.add(titleTable);

        document.add(new Paragraph(new Chunk("申请日期:2021-06-01", fontZH)));
        document.add(new Paragraph("\n"));
        // 创建table,注意这里的2是两列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列
        PdfPTable table = new PdfPTable(6);
        table.setWidthPercentage(100.0F);
        table.setHeaderRows(1);
        table.getDefaultCell().setHorizontalAlignment(1);

        PdfPCell cell = new PdfPCell(new Phrase("个人信息", fontZH));
        cell.setColspan(6);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);


        table.addCell(new Paragraph("员工姓名", fontZH));
        table.addCell(new Paragraph("张亮Zhang Liang", fontZH));
        table.addCell(new Paragraph("工号", fontZH));
        table.addCell(new Paragraph("2849", fontZH));
        table.addCell(new Paragraph("工作部门", fontZH));
        table.addCell(new Paragraph("上汽通用JQSP", fontZH));
        table.addCell(new Paragraph("身份证号", fontZH));
        table.addCell(new Paragraph("320322199711125310", fontZH));
        table.addCell(new Paragraph("联系电话", fontZH));
        table.addCell(new Paragraph("17601251600", fontZH));
        table.addCell(new Paragraph("单位联系电话", fontZH));
        table.addCell(new Paragraph("18994131575", fontZH));
        PdfPCell cell2 = new PdfPCell(new Phrase("车辆需求信息", fontZH));
        cell2.setColspan(6);
        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell2);
        table.addCell(new Paragraph("申购车型", fontZH));
        table.addCell(new Paragraph("全新一代别克君威豪华型", fontZH));
        table.addCell(new Paragraph("颜色", fontZH));
        table.addCell(new Paragraph("天空蓝", fontZH));
        table.addCell(new Paragraph("价格(指导价)", fontZH));
        table.addCell(new Paragraph("888888", fontZH));
        PdfPCell cell3 = new PdfPCell(new Phrase("审核情况", fontZH));
        cell3.setColspan(6);
        cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell3);
        table.addCell(new Paragraph("最终购车价格", fontZH));
        PdfPCell cell4 = new PdfPCell(new Phrase("88888", fontZH));
        cell4.setColspan(3);
        table.addCell(cell4);
        table.addCell(new Paragraph("部门审核盖章", fontZH));
        table.addCell(new Paragraph("", fontZH));
        PdfPCell cell5 = new PdfPCell(new Phrase("开票信息", fontZH));
        cell5.setColspan(6);
        cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell5);
        table.addCell(new Paragraph("与员工关系", fontZH));
        PdfPCell cell6 = new PdfPCell(new Phrase("本人", fontZH));
        cell6.setColspan(5);
        table.addCell(cell6);
        table.addCell(new Paragraph("开票人姓名", fontZH));
        PdfPCell cell7 = new PdfPCell(new Phrase("张亮", fontZH));
        cell7.setColspan(5);
        table.addCell(cell7);
        table.addCell(new Paragraph("身份证号", fontZH));
        PdfPCell cell8 = new PdfPCell(new Phrase("320322199711125310", fontZH));
        cell8.setColspan(5);
        table.addCell(cell8);
        document.add(table);
        document.add(new Paragraph(new Chunk("( 注:本身清单自申请日的当季内有效)", fontZH).setLocalDestination(title)));
        document.add(new Paragraph("\n"));
        // 第七步,关闭document
        document.close();
        System.out.println(out);
        System.out.println("导出pdf成功~");
    }
}

添加jar包


<!--pdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>

标签:自定义,导出,new,addCell,Paragraph,PdfPCell,table,PDF,fontZH
来源: https://www.cnblogs.com/anpieBlog/p/14870175.html