【无标题】

package com.ruoyi.web.controller.system;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class PdfUtils {

    /**
     * 插入文本字段到指定位置
     * @param filePath   文件路径
     * @param pageNumber  字体的页数
     * @param x            字段位置X
     * @param y            字段位置Y
     * @param text         字段文本
     * @param fileOutPath   文件输出路径
     */
    public static void insertTextAtPosition(String filePath, int pageNumber, float x, float y,
                                            String text,String fileOutPath){

        try {
            PDDocument document = PDDocument.load(new File(filePath));
            PDPage page = document.getPage(pageNumber - 1);
            //PDPageContentStream.AppendMode.APPEND  使用枚举pdf格式为追加
            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
            //字段设置
            contentStream.setFont(PDType1Font.HELVETICA, 12);
            contentStream.beginText();
            contentStream.newLineAtOffset(x, y);
            contentStream.showText(text);
            contentStream.endText();
            contentStream.close();
            // FileOutputStream outputStream = new FileOutputStream(fileOutPath);
            document.save(new File(fileOutPath));
        /*    // 输出到 HttpServletResponse
            response.setContentType("application/pdf");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + new File(fileOutPath).getName());
             out = response.getOutputStream();
            document.save(out);*/
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 插入图片到指定位置插入文本字段到指定位置
     * @param filePath  文件路径
     * @param imagePath 图片路径
     * @param pageNumber   页数
     * @param x         图片位置
     * @param y        图片位置
     * @param width    图片大小
     * @param height   图片大小
     * @param fileOutPath    输出路径
     * @param response
     * @throws IOException
     */
    public static void insertImage(String filePath, String imagePath, int pageNumber, float x, float y, float width, float height,
                                   String fileOutPath, HttpServletResponse response) throws IOException {
        ServletOutputStream out=null;
        try  {
            PDDocument document = PDDocument.load(new File(filePath));
            PDPage page = document.getPage(pageNumber - 1);
            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)
            PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, document);
            contentStream.drawImage(pdImage, x, y, width, height);
            contentStream.close();
            document.save(new File(fileOutPath));
            // 输出到 HttpServletResponse
            response.setContentType("application/pdf");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + new File(fileOutPath).getName());
            out = response.getOutputStream();
            document.save(out);
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null){
                out.close();
            }
        }
    }
}

    <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.27</version>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值