1. 首先需要引入以下依赖:
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.23</version>
</dependency>
<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.14</version>
</dependency>
  1. 编写接口代码:
@RestController
public class PdfController {

    @GetMapping(value = "/pdf/split")
    public void splitPdf(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
        // 将 MultipartFile 转成 File
        File pdfFile = multipartFileToFile(file);
        // 获取 pdf 文件的第一页
        PDDocument document = PDDocument.load(pdfFile);
        PDPage page = document.getPage(0);
        // 获取第一页的宽度和高度
        float pageWidth = page.getMediaBox().getWidth();
        float pageHeight = page.getMediaBox().getHeight();
        // 将第一页分成三部分
        float partHeight = pageHeight / 3;
        Rectangle2D.Float part1Area = new Rectangle2D.Float(0, 0, pageWidth, partHeight);
        Rectangle2D.Float part2Area = new Rectangle2D.Float(0, partHeight, pageWidth, partHeight);
        Rectangle2D.Float part3Area = new Rectangle2D.Float(0, partHeight * 2, pageWidth, partHeight);
        // 截取每部分并保存为 jpg 文件
        BufferedImage part1Image = getPartImage(page, part1Area);
        BufferedImage part2Image = getPartImage(page, part2Area);
        BufferedImage part3Image = getPartImage(page, part3Area);
        File part1File = saveImage(part1Image);
        File part2File = saveImage(part2Image);
        File part3File = saveImage(part3Image);
        // 将三部分文件压缩为 zip 文件并下载
        compressAndDownloadZip(Arrays.asList(part1File, part2File, part3File), response);
        // 关闭 PDDocument
        document.close();
    }

    // 获取指定矩形区域的 BufferedImage
    private BufferedImage getPartImage(PDPage page, Rectangle2D.Float area) throws IOException {
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 300);
        return Scalr.crop(image, (int) area.getX(), (int) area.getY(), (int) area.getWidth(), (int) area.getHeight(), null);
    }

    // 将 BufferedImage 保存为文件
    private File saveImage(BufferedImage image) throws IOException {
        File file = File.createTempFile("part", ".jpg");
        ImageIO.write(image, "jpg", file);
        return file;
    }

    // 将文件列表压缩为 zip 并下载
    private void compressAndDownloadZip(List<File> fileList, HttpServletResponse response) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(bos);
        for (File file : fileList) {
            zos.putNextEntry(new ZipEntry(file.getName()));
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            fis.close();
            zos.closeEntry();
        }
        zos.close();
        byte[] bytes = bos.toByteArray();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("split.zip", "UTF-8"));
        response.setContentLength(bytes.length);
        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.flush();
        out.close();
    }

    // 将 MultipartFile 转成 File
    private File multipartFileToFile(MultipartFile multipartFile) throws IOException {
        File file = File.createTempFile("temp", ".pdf");
        multipartFile.transferTo(file);
        return file;
    }

}
  1. 测试接口:

可以使用 Postman 或类似工具来测试接口,以下是一个示例请求:

GET http://localhost:8080/pdf/split?file=@/path/to/test.pdf

其中,@ 符号后面的路径是本地的 pdf 文件路径。测试成功后,会自动下载名为 split.zip 的文件,其中包含三个 jpg 文件,分别为第一页的三个部分的截图。

用springboot开放一个接口将pdf文件中第一页纵向平分成三部分并将每部分截取出一个jpg文件最后将这三个文件压缩成一个zip文件下载

原文地址: https://www.cveoy.top/t/topic/bp6Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录