当前位置: 首页 > news >正文

Java图片转word

该方法可以控制一页是否只显示存放一张图片

第一步

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency>

第二步

package com.example.demo.file.word;import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;public class ImageToWordWithPOI {public static void main(String[] args) {String folderPath = "C:\\Users\\EDY\\Desktop\\测试图片"; // 替换为你的图片文件夹路径String outputFilePath = "C:\\Users\\EDY\\Desktop\\output.docx"; // 输出Word文档的路径// 选择是否每页只插入一个图片boolean oneImagePerPage = true;Path folder = Paths.get(folderPath);try {// 获取所有图片文件的路径List<Path> imageFiles = Files.walk(folder).filter(Files::isRegularFile).filter(path -> isImageFile(path.toString())).collect(Collectors.toList());if (!imageFiles.isEmpty()) {XWPFDocument document = new XWPFDocument();try (FileOutputStream out = new FileOutputStream(outputFilePath)) {int imageCount = 0;for (Path path : imageFiles) {try {// 如果选择每页只插入一个图片,并且已经插入过图片,则先添加一个分页符if (oneImagePerPage && imageCount > 0) {XWPFParagraph paragraph = document.createParagraph();XWPFRun run = paragraph.createRun();run.addBreak(BreakType.PAGE);}insertImageToWordDocument(document, path.toFile(), Units.pixelToEMU(400), Units.pixelToEMU(400));imageCount++;} catch (IOException | InvalidFormatException e) {e.printStackTrace();System.out.println("处理图片时发生错误。");}}// 写入文档document.write(out);System.out.println("图片已成功插入到Word文档中。");} catch (IOException e) {e.printStackTrace();System.out.println("创建Word文档时发生错误。");}} else {System.out.println("指定的文件夹下没有找到图片。");}} catch (IOException e) {e.printStackTrace();System.out.println("读取文件夹时发生错误。");}}private static boolean isImageFile(String filename) {String[] imageExtensions = {"png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff", "webp"};for (String ext : imageExtensions) {if (filename.toLowerCase().endsWith(ext)) {return true;}}return false;}private static void insertImageToWordDocument(XWPFDocument document, File imageFile, int width, int height) throws IOException, InvalidFormatException {// 创建一个新的段落用于插入图片XWPFParagraph paragraph = document.createParagraph();XWPFRun run = paragraph.createRun();run.addPicture(new FileInputStream(imageFile), XWPFDocument.PICTURE_TYPE_JPEG, imageFile.getName(), width, height);}
}

http://www.lryc.cn/news/479361.html

相关文章:

  • 立体视觉的核心技术:视差计算与图像校正详解
  • PaddleNLP的FAQ问答机器人
  • 2024年12月中国多场国际学术会议,EI检索录用!
  • 日语学习的难易程度
  • java-web-web后端知识小结
  • 常见的排序算法(二)
  • spark的RDD分区的设定规则
  • 【点云网络】voxelnet 和 pointpillar
  • HAL库硬件IIC驱动气压传感器BMP180
  • 探索Python音频处理的奥秘:Pydub库的魔法
  • LeetCode 热题100(七)【链表】(2)
  • 计算机网络 TCP/IP体系 网络层
  • 迈入国际舞台,AORO M8防爆手机获国际IECEx、欧盟ATEX防爆认证
  • 实习作假:阿里健康实习做了RABC中台,还优化了短信发送流程
  • Unity中IK动画与布偶死亡动画切换的实现
  • java导出word文件(手绘)
  • ssm070基于SSM框架的校园代购服务订单管理系统的设计与实现+vue(论文+源码)_kaic
  • Java项目实战II基于Spring Boot的秒杀系统设计与实现(开发文档+数据库+源码)
  • FastAPI —— 请求参数验证
  • 第七篇: BigQuery中的复杂SQL查询
  • 【SQL实验】高级查询(难点.三)含附加数据库操作
  • qt QFileSystemModel详解
  • element plus中修改el-table的样式
  • 深入理解封装与接口:Java程序设计的核心思想与最佳实践
  • linux 下调试 mpu6050 三轴加速度
  • C语言心型代码解析
  • 【LeetCode】【算法】647. 回文子串
  • 介绍6种常见的基于知识图谱推荐算法的类型和各自的优缺点
  • 使用python拟合二元曲线系数
  • go 集成viper 配置管理