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);
    }
}
相关推荐
一路向北North3 分钟前
Spring Security OAuth2.0(18):资源服务测试
java·后端·spring
用户990450177800921 分钟前
若依工作流主流方案对接
后端
触底反弹36 分钟前
🚀 Node.js path 和 fs 模块,从回调地狱到 async/await 的进化之路!
javascript·后端·node.js
极光代码工作室1 小时前
基于SpringBoot的订单管理系统
java·springboot·web开发·后端开发
xiaoduzi19911 小时前
ConcurrentHashMap学习
java·学习
折哥的程序人生 · 物流技术专研1 小时前
《Java 100 天进阶之路》第53篇:volatile与JMM(2026版)
java·volatile·内存屏障·可见性·java内存模型·jmm·java100天进阶
米饭不加菜1 小时前
使用万用表判断三极管(BJT)好坏
java·开发语言
Csvn1 小时前
Python 开发技巧 · 生成器(Generator)进阶 —— 从 yield 到 yield from,写出省内存的生产者-消费者模式
后端·python
geovindu1 小时前
CSharp: Dijkstra Algorithms
开发语言·后端·算法·c#
Maynor9961 小时前
AI Coding 零基础实战教程|第五部分:完整项目案例实操
java·前端·人工智能·claude code·ai coding