package com.example.demo.service; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.xwpf.usermodel.XWPFComment; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; @Service public class WordService { public String getDocxComments(MultipartFile multipartFile) { StringBuilder stringBuilder = new StringBuilder(); try (InputStream fis = multipartFile.getInputStream(); XWPFDocument document = new XWPFDocument(fis)) { // 获取文档中的所有批注 XWPFComment[] comments = document.getComments(); for (XWPFComment comment : comments) { // 提取批注信息 String author = comment.getAuthor(); String text = comment.getText(); // 获取批注文本内容 String id = comment.getId(); // 获取批注ID stringBuilder.append(text); } } catch (Exception e) { e.printStackTrace(); } return stringBuilder.toString(); } public String getDocComments(MultipartFile multipartFile) { StringBuilder stringBuilder = new StringBuilder(); try (InputStream fis = multipartFile.getInputStream(); WordExtractor extractor = new WordExtractor(fis)) { // 尝试提取批注文本 String[] comments = extractor.getCommentsText(); if (comments != null) { for (String comment : comments) { stringBuilder.append(comment); } } } catch (Exception e) { e.printStackTrace(); } return stringBuilder.toString(); } }