Java中word文档操作:把图片和文字插入word文档中!!!

达到的效果:把服务器上的图片插入word文档中,并且生成一个word文件。

其他包括:文字换行、图片大小适应一个word文档页等。

直接上代码:

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>4.1.2</version>
</dependency>
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class InsertMultipleImagesToWordDocument {
    public static void main(String[] args) {
        // 加载Word文档
        try {
            List<String> imagePaths = new ArrayList<>();
            imagePaths.add("C:\\Users\\tuyua\\Desktop\\空白图片空白图片\\111.png");
            imagePaths.add("C:\\Users\\tuyua\\Desktop\\空白图片空白图片\\222.png");
            imagePaths.add("C:\\Users\\tuyua\\Desktop\\空白图片空白图片\\333.png");
            XWPFDocument doc = new XWPFDocument();
            // 插入图片
            for (String imagePath : imagePaths) {
                XWPFParagraph paragraph = doc.createParagraph();
                paragraph.setAlignment(ParagraphAlignment.CENTER); // 设置对齐方式为居中
                XWPFRun run = paragraph.createRun();
                run.setFontSize(12); // 设置字体大小
                run.setText("这是你要输入的文字!!!!!");//输入文字,比如可以图片的说明文字等
                run.addBreak();//换行
                // 读取原始图片
                BufferedImage originalImage = ImageIO.read(new File(imagePath));
                // 计算宽高比
                double aspectRatio = (double) originalImage.getWidth() / originalImage.getHeight();
                // 计算新的宽度和高度
                int newWidth = 400;//高度固定在word一页内
                int newHeight = 650;//高度固定在word一页内
                if (aspectRatio == 1.00) {
                    newHeight = newWidth;
                } else if (aspectRatio > 1.00) {
                    //宽度固定,高度按照比例缩小
                    newHeight = (int) (newWidth / aspectRatio);
                } else {
                    //高度固定,宽度按照比例缩小(如果算下来宽度超过400,那么宽度固定400,再缩小高度)
                    newWidth = (int) (newHeight * aspectRatio);
                    if (newWidth > 400) {
                        newWidth = 400;
                        newHeight = (int) (newWidth / aspectRatio);
                    }
                }
                FileInputStream is = new FileInputStream(imagePath);
                run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, imagePath, Units.toEMU(newWidth), Units.toEMU(newHeight));
                is.close();
            }
            // word文件要存放的路径
            String fileDir = "C:\\Users\\tuyua\\Desktop\\空白图片空白图片\\" + UUidUtils.getUuid();
            File dir = new File(fileDir);
            // 使用mkdirs()方法创建文件夹,包括任何必要的父文件夹
            dir.mkdirs();
            FileOutputStream out = new FileOutputStream(fileDir + "/" + "testInsertImage.docx");
            doc.write(out);
            out.close();
            doc.close();
            System.out.println("success");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 效果如下:

success!!!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库实现将数学公式转换到 Word 文档。具体步骤如下: 1. 首先,需要在 pom.xml 文件添加 Apache POI 的依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个 Word 文档,并添加一个段落: ``` XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); ``` 3. 创建一个数学公式对象,并设置公式内容: ``` XWPFMath math = paragraph.createRun().getCTR().addNewOMath(); CTOMath ctomath = CTOMath.Factory.parse("数学公式内容"); math.setOMath(ctomath); ``` 其,"数学公式内容" 是你要插入的数学公式的内容,可以使用 LaTeX 语法编写。 4. 将 Word 文档保存到本地: ``` FileOutputStream out = new FileOutputStream("文件路径"); document.write(out); out.close(); ``` 完整代码示例: ``` import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath; public class MathFormulaToWord { public static void main(String[] args) throws Exception { XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFMath math = paragraph.createRun().getCTR().addNewOMath(); CTOMath ctomath = CTOMath.Factory.parse("数学公式内容"); math.setOMath(ctomath); FileOutputStream out = new FileOutputStream("文件路径"); document.write(out); out.close(); } } ``` 注意:在将 LaTeX 语法转换为 MathML 语法时,需要借助一些工具,例如 MathJax、LaTeX2MathML 等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值