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);
    }
}
相关推荐
SL_staff21 分钟前
告别报表重复开发:JVS-BI 模板市场的参数化继承实践
java·数据分析·数据可视化
橙子家23 分钟前
参数校验类异常(Argument Exception)【C# Exception 详解系列一】
后端
掘金挖土41 分钟前
前端手摸手跑路之 AI 应用开发(九)
前端·后端
全栈弄潮儿1 小时前
Python实战第8期:函数
后端·python·编程语言
码事漫谈1 小时前
你让 AI “尽力做”,它反而可能做不好
后端
SL_staff2 小时前
从邮箱传文档到协同流重构:企业文档协作的系统性破局实践
java·开源·github
孟健2 小时前
LLM API 安全防线:从 OpenCode 盗刷事件到 LiteLLM 多模型容灾实操
人工智能·后端·架构
程序员cxuan2 小时前
我用 WorkBuddy + ima 把公众号文章做成了知识库。
人工智能·后端·程序员
程序员cxuan3 小时前
玩了一天 REA,我号快没了
人工智能·后端·程序员