spring boot3识别PDF图纸

1、安装maven依赖

复制代码
<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>3.0.7</version>
            <scope>compile</scope>
        </dependency>

2、建立service和实现类

2.1 IDrawingPdfService接口

复制代码
public interface IDrawingPdfService {
    String extractText(MultipartFile file);
}

2.2 DrawingPdfServiceImpl实现类

复制代码
package com.example.flowable_eng.service.impl;

import com.example.flowable_eng.service.IDrawingPdfService;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import technology.tabula.*;
import technology.tabula.extractors.SpreadsheetExtractionAlgorithm;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DrawingPdfServiceImpl implements IDrawingPdfService {
    @Override
    public String extractText(MultipartFile file) {

        try (InputStream inputStream = file.getInputStream();
             PDDocument document = Loader.loadPDF(inputStream.readAllBytes())) {
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);
            return stripper.getText(document);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

3、控制器测试

复制代码
package com.example.flowable_eng.controller;

import com.example.flowable_eng.service.IDrawingPdfService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
public class DrawingPdfController {
    @Resource
    private IDrawingPdfService drawingPdfService;

    @PostMapping("/extractText")
    public String extractText(@RequestParam("file") MultipartFile file) {
        return drawingPdfService.extractText(file);
    }
}
相关推荐
噢,我明白了5 小时前
java中唯一键和幂等键的应用
java·后端
程序猿乐锅5 小时前
【黑马点评 | 第八篇】Redisson分布式锁
java·数据库·spring boot·redis·分布式·spring·缓存
GreenTea5 小时前
我把 Agent 的 while 循环拆掉了:一种你可能没想到的 Agent 架构
前端·后端·架构
考虑考虑5 小时前
synchronized字符串常量
java·后端·java ee
长谷深风1116 小时前
Tool与Skill:AI能力设计的分水岭
java·人工智能·ai·大模型·aiagent
m0_587383006 小时前
广州24小时自助健身房解决方案实战指南与系统部署要点
java·spring·小程序·架构·需求分析
Escalating_xu6 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
周杰偷奶茶6 小时前
【Java】数据类型与变量
java·开发语言
code斗6 小时前
Java数据结构:堆详解
java·开发语言·数据结构
颜进强6 小时前
16 · NestJS LifecycleEvents 生命周期事件:五个钩子、三条边界,和 rag-server 的优雅停机
前端·后端·ai编程