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);
    }
}
相关推荐
妙码生花9 分钟前
从 PHP 到 AI + Golang,程序员自救转型手记(四十三):前后端数据验证
后端·go·ai编程
long31610 分钟前
Java 新手入门与实战开发指南
java·开发语言
YuePeng19 分钟前
别再让 AI 直接写 SQL 了:一个注解搞定十亿行数据的语义层
后端·github
半夜里咳嗽的狼25 分钟前
Go 1.25 的 WaitGroup.Go 省了两行代码,也补不上这三个并发边界
后端·go
Lihua奏26 分钟前
身份验证:登录之后,服务器怎么一直认得你?
后端
XuCoder27 分钟前
Redis 缓存和 MySQL 数据不一致怎么办?双写一致性一次讲透
后端
执笔画流年呀30 分钟前
Linux搭建Java项目部署环境
java·linux·运维
程序员天天困33 分钟前
Arthas ognl 表达式从入门到实战:掌握在线调试最强的表达式引擎
java·jvm·后端
Escape1 小时前
为什么你的 AI 越聊越傻?从 Token 到 Agent,彻底搞懂 AI Agent的秘密㊙️
前端·人工智能·后端
IT_Octopus1 小时前
Spring Boot 中 ThreadLocal 请求上下文的完整生命周期
java·spring boot·spring