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);
    }
}
相关推荐
quan26317 分钟前
20260529,日常开发-数据库主从问题
java·mysql·主从·延迟
Cosolar9 分钟前
QwenPaw Agent 实现原理深度剖析
后端·面试·架构
JacksonMx26 分钟前
@Transactional 最佳实践
java·spring boot·spring·性能优化
Sincerelyplz32 分钟前
【AI会议纪要实践】mapReduce、RAG 与结构化输出
java·后端·agent
过期动态39 分钟前
【LeetCode 热题 100】接雨水
java·数据结构·算法·leetcode·职场和发展
zavoryn40 分钟前
后端接入 AI Agent:Tool Calling 网关、幂等与审计日志实战
后端·架构
zhangjw341 小时前
第15篇:Java多线程零基础入门,进程线程、线程创建方式、线程生命周期、线程安全彻底吃透
java·开发语言·面试
蝈理塘(/_\)大怨种1 小时前
类和对象 (上)
java·开发语言
swipe2 小时前
混合检索 RAG 的工程化实践:不是多查几路,而是把召回、重排和上下文预算管好
后端·langchain·llm
我材不敲代码2 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python