Spring Boot写一个简单的PDF到Word的转换程序

时间:2024-03-15 17:25:43
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.springframework.stereotype.Service; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @Service public class PdfToWordConverterService { public byte[] convertPdfToWord(byte[] pdfBytes) throws IOException { try (PDDocument pdfDocument = PDDocument.load(new ByteArrayInputStream(pdfBytes)); XWPFDocument wordDocument = new XWPFDocument()) { PDFTextStripper pdfTextStripper = new PDFTextStripper(); String text = pdfTextStripper.getText(pdfDocument); XWPFParagraph paragraph = wordDocument.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(text); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); wordDocument.write(outputStream); return outputStream.toByteArray(); } } }