itext7 springboot 集成,基础使用下载导出等

21 篇文章 0 订阅
4 篇文章 0 订阅

首先放一个自己写的pdfUtil,小伙伴们可以直接拷到项目里,没有代码侵染

package com.cpicdg.util.pdf;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import org.springframework.util.ResourceUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

/**
 * @author zyy
 * @program secl
 * @description pdf工具类
 * @date 2020-10-12 16:52
 **/

public class PdfUtil {
    /**
     * 功能描述 构建文档对象
     *
     * @param filePath 生成的pdf路径
     * @param pageSize pdf纸张大小
     * @return com.itextpdf.layout.Document
     * @date 2020/10/12 16:57
     */
    public static Document generatorDoc(String filePath, PageSize pageSize) throws FileNotFoundException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filePath));
        return new Document(pdfDoc, pageSize);
        /*
         * 构建完文档对象后,可以设置文档的标题等
         * .setTitle等
         *
         * */
    }

    /**
     * 功能描述 构建Table表格
     *
     * @param width 表格每一个列的宽度,支持多个参数,几个参数代表有几列
     * @return com.itextpdf.layout.element.Table
     * @date 2020/10/12 17:11
     */
    public static Table generatorTable(float... width) {
        //构建表格以100%的宽度
        return new Table(width).setWidth(UnitValue.createPercentValue(100));
    }

    /**
     * 功能描述 构建某一行的数据
     *
     * @param table 要构建的数据所处的行
     * @param data  要构建的数据
     * @return void
     * @date 2020/10/13 9:40
     */
    public static void generatorRow(Table table, String data) {
        Cell cell = new Cell();
        cell.add(new Paragraph(data).setTextAlignment(TextAlignment.CENTER));
        table.addCell(cell);
    }

    public static void generatorRow(Table table, String data, int rowSpan, int colSpan) {
        Cell cell = new Cell(rowSpan, colSpan);
        cell.add(new Paragraph(data).setTextAlignment(TextAlignment.CENTER));
        table.addCell(cell);
    }


    /**
     * 功能描述 构建图片对象
     *
     * @param path 图片路径
     * @return com.itextpdf.layout.element.Image
     * @date 2020/10/12 17:23
     */
    public static Image generatorImage(String path) throws MalformedURLException {
        return new Image(ImageDataFactory.create(path));
    }

    /**
     * 功能描述 构建字体
     *
     * @return com.itextpdf.kernel.font.PdfFont
     * @date 2020/10/12 16:59
     */
    public static PdfFont generatorFont() throws IOException {
        //中文字体
        String resourcePath = ResourceUtils.getURL("classpath:static").getPath();
        String path = resourcePath + "/font/simhei.ttf";
        //bfChinese = BaseFont.createFont(resourcePath.substring(0, resourcePath.indexOf("classes") + 7) + "/static/font/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        return PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H, true);
        //return PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
    }


}

具体用法 ,我这里是预览所以直接new PdfWriter(response.getOutputStream());如果要下载直接在上方添加

 String fileName = "XXXX";
   response.setHeader("Content-Disposition", "attachment; filename="
  + new String(fileName.getBytes("gb2312"), "8859_1") + ".pdf");
  // (1)生成document,document可以理解为五子棋的棋盘,棋盘固定后,往上面落子
  PdfWriter pdfWriter = new PdfWriter(response.getOutputStream());
  PdfDocument pdfDoc = new PdfDocument(pdfWriter);
  Document document = new Document(pdfDoc, PageSize.A4);
  // 设置字体
  document.setFont(PdfUtil.generatorFont());
  // 添加图片,这里我最上面添加两个图片用来做右上角logo和左上角logo
  // (2)获取图片对象
   String staticPath = ResourceUtils.getURL("classpath:static").getPath();
  // (2.1)添加左侧logo
  Image logoLeft = PdfUtil.generatorImage(staticPath + "/images/cpic_logo_left.png");
  // (2.1.1)缩放图片
  logoLeft.scaleToFit(150, 80);
  // (2.1.2)图片位置
  logoLeft.setFixedPosition(40, 793);
  // (2.1.3)设置图片
  document.add(logoLeft);
  // (2.2)添加右侧logo
  Image logoRight = PdfUtil.generatorImage(staticPath +"/images/cpic_logo_right.png");
  logoLeft.scaleToFit(200, 35);
  logoRight.setFixedPosition(290, 793);
  document.add(logoRight);
  // (3)设置标题
  // 设置第一个标题,标题在logo后面添加
  Paragraph firstTitle = new Paragraph("保险事故线上查勘联络单");
  // (3.1)设置标题与图标之间的间距
  firstTitle.setMarginTop(25);
  // (3.2)设置字体大小
  firstTitle.setFontSize(23);
  firstTitle.setTextAlignment(TextAlignment.CENTER);
  document.add(firstTitle);
  // (4)构建table
  Table table = PdfUtil.generatorTable(80, 150, 150, 150, 150);
  // (4.1)设置table字体
  table.setFont(PdfUtil.generatorFont());
  table.setMaxWidth(530);
  // (5) 生成cell,也就是table中的每个格子的内容
  // (5.1)第一行
  PdfUtil.generatorRow(table, "出险时间");
  document.add(table);
  document.close();
  public static void generatorRow(Table table, String data) {
        Cell cell = new Cell();
        cell.add(new Paragraph(data).setTextAlignment(TextAlignment.CENTER));
        table.addCell(cell);
    }

具体不懂可以下面评论,具体交流~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用iText7将Excel转换为PDF,你需要完成以下步骤: 1.添加iText7依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core</artifactId> <version>7.1.15</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>layout</artifactId> <version>7.1.15</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>kernel</artifactId> <version>7.1.15</version> </dependency> ``` 2.读取Excel文件 使用Apache POI库读取Excel文件,将数据保存在一个二维数组中。以下是一个示例代码片段: ```java FileInputStream inputStream = new FileInputStream(new File("path/to/excel")); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); int rows = sheet.getPhysicalNumberOfRows(); int cols = sheet.getRow(0).getPhysicalNumberOfCells(); String[][] data = new String[rows][cols]; for (int i = 0; i < rows; i++) { Row row = sheet.getRow(i); for (int j = 0; j < cols; j++) { Cell cell = row.getCell(j); data[i][j] = cell.toString(); } } workbook.close(); ``` 3.创建PDF文档 使用iText7创建一个新的PDF文档,并设置页面大小和边距。以下是一个示例代码片段: ```java PdfDocument pdfDoc = new PdfDocument(new PdfWriter("path/to/pdf")); Document doc = new Document(pdfDoc, PageSize.A4); doc.setMargins(36, 36, 36, 36); ``` 4.将Excel数据写入PDF 将Excel数据写入PDF文档中,使用PdfTable类创建一个表格,并将数据添加到表格中。以下是一个示例代码片段: ```java PdfTable table = new PdfTable(cols, true); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { table.addCell(data[i][j]); } } doc.add(table); ``` 5.关闭文档 完成后,关闭PDF文档和输入流。 ```java doc.close(); inputStream.close(); ``` 以上就是使用iText7将Excel转换为PDF的基本步骤。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值