将图片自适应大小插入到word文档

您所在的位置:网站首页 批量插图片word 将图片自适应大小插入到word文档

将图片自适应大小插入到word文档

2024-07-12 23:33:08| 来源: 网络整理| 查看: 265

将图片自适应大小插入到word文档 概述 demo 优化1 优化2 优化3 汇总 完整的代码 概述

如果手工新建word文档,插入图片是可以做到图片自适应等比缩放的,那么如何通过程序的方式来完成呢。

demo

通过一番搜索,了解到Java操作word添加图片的方法,Adding Images to a Word Document using Java; 重要的就是这几步

Step 1: Creating a blank document Step 2: Creating a Paragraph Step 3: Creating a File output stream of word document at the required location Step 4: Creating a file input stream of the image by specifying its path Step 5: Retrieving the image file name and image type Step 6: Setting the width and height of the image in pixels Step 7: Adding the picture using the addPicture() method and writing into the document Step 8: Closing the connections

其中添加图片的重要代码如下,

XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); // Step 3: Creating a File output stream of word // document at the required location FileOutputStream fout = new FileOutputStream( new File("D:\\WordFile.docx")); // Step 4: Creating a file input stream of image by // specifying its path File image = new File("D:\\Images\\image.jpg"); FileInputStream imageData = new FileInputStream(image); // Step 5: Retrieving the image file name and image // type int imageType = XWPFDocument.PICTURE_TYPE_JPEG; String imageFileName = image.getName(); // Step 6: Setting the width and height of the image // in pixels. int width = 450; int height = 400; // Step 7: Adding the picture using the addPicture() // method and writing into the document run.addPicture(imageData, imageType, imageFileName, Units.toEMU(width), Units.toEMU(height));

首先创建一个段落,然后XWPFRun辅助类来添加图片,添加图片需要知道图片的像素大小。

优化1

关于图片大小的获取,demo代码中并没有处理,应该这么操作,

BufferedImage img = ImageIO.read(image); int width = img.getWidth(); int height = img.getHeight() 优化2

重要的是图片缩放计算,首先要直到常规A4纸张的大小,A4纸内容区域的大小。 A4纸宽=210mm,高=297mm,页面的内容左右边距分别都是31.8mm,上下边距分别是25.4mm; 只有这些尺寸是不够的,还不清楚这些尺寸可以保存多少像素。

通过搜索发现,A4纸在屏幕上的像素尺寸信息,https://cloud.tencent.com/developer/article/1505088; 分辨率是96像素/英寸时,A4纸的尺寸的图像的像素是794×1123;(默认),而1英寸=2.54cm。

这句话的意思是,如果一个图片的宽度是1280px那么换算成尺寸应该是,1280 / 96.0 * 25.4 = 330.2mm

优化3

直到了怎么处理图片像素到尺寸的转换,那么就可以考虑如何处理等比例缩放问题了。 1、计算图片宽带与内容区域宽度的比例 2、计算图片高度与内容区域高度的比例 3、取两个比例之中最小的值,如果比例大于1则图片不缩放保持原来的宽度和高度,否则执行缩放

BufferedImage img = ImageIO.read(image); int width = img.getWidth(); int height = img.getHeight(); // image size double mw = width / 96.0 * 25.4; double mh = height / 96.0 * 25.4; // content size double cw = 210 - (2*31.8); double ch = 297 - (2*25.4); double scaling = 1.0; if (mw > cw) { scaling = cw / mw; } if (mh > ch) { if (ch / mh cw) { scaling = cw / mw; } if (mh > ch) { if (ch / mh org.apache.poi poi-ooxml 5.1.0 org.apache.poi poi-ooxml-schemas 4.1.2 2023 1 2022 3 2020 2 2019 2 2018 5 2023 More on @crxjs/vite-plugin

3 minute read

Back to Top ↑ 2022 Java 8 CompletableFuture Suggestion

3 minute read

重新组织一下个人网站

less than 1 minute read

将图片自适应大小插入到word文档

3 minute read

Back to Top ↑ 2020 关于聚合和多线程的处理套路

1 minute read

正确地使用CompletableFuture

3 minute read

Back to Top ↑ 2019 Transactional是怎么工作的

1 minute read

再造一次FTP连接池轮子

less than 1 minute read

Back to Top ↑ 2018 Use Bouncy Castle Provider implementation to do RSA

5 minute read

使用Mockito修改Bean的依赖

2 minute read

学习Java Collection Framework的Iterator实现

6 minute read

一些我认为有用有趣的JDK方法

3 minute read

说一说java.util.Arrays$ArrayList

1 minute read

Back to Top ↑


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭