poi 替换word并设置字体工具类方法

一、工具类内容

     * 替换文本 (每种字体有特定的字体字号)
     * @param params key被替换字符,value content为替换内容 font为替换字体 fontSize为替换字号
     * @param path 文件路径
     * @param outPath 输出文件路径
     */
    public static void replaceTextCustomization(Map<String, HashMap<String, Object>> params, String path, String outPath) throws FileNotFoundException {
        XWPFDocument doc = openDocx(path);
        for (XWPFParagraph paragraph : doc.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(0);
                for (Map.Entry<String, HashMap<String, Object>> entry : params.entrySet()) {
                    if (text != null && text.contains(entry.getKey())) {
                        text = text.replaceAll(entry.getKey(), entry.getValue().get("content").toString());
                        if (entry.getValue().get("font") != null) {
                            CTFonts font = run.getCTR().addNewRPr().addNewRFonts();
                            font.setEastAsia(entry.getValue().get("font").toString());
                        }
                        if (entry.getValue().get("fontSize") != null) {
                            run.setFontSize(Integer.parseInt(entry.getValue().get("fontSize").toString()));
                        }
                        run.setText(text, 0);
                    }
                }
            }
        }
        writeDocx(doc, new FileOutputStream(outPath));
    }

    /**
     * 替换文本(统一设置字体字号)
     * @param params 替换和被替换内容
     * @param font  字体
     * @param fontSize 字号
     * @param path 文件路径
     * @param outPath 输出文件路径
     * @throws FileNotFoundException 错误
     */
    public static void replaceText(Map<String, String> params, String font, int fontSize, String path, String outPath) throws FileNotFoundException {
        XWPFDocument doc = openDocx(path);
        for (XWPFParagraph paragraph : doc.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(0);
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    if (text != null && text.contains(entry.getKey())) {
                        text = text.replaceAll(entry.getKey(), entry.getValue());
                        if (font != null && !"".equals(font)) {
                            CTFonts fontSet = run.getCTR().addNewRPr().addNewRFonts();
                            fontSet.setEastAsia(font);
                        }
                        if (fontSize != 0) {
                            run.setFontSize(fontSize);
                        }
                        run.setText(text, 0);
                    }
                }
            }
        }
        writeDocx(doc, new FileOutputStream(outPath));
    }
       /**
     * @param url             获取文档对象的路径
     * @return XWPFDocument   返回文档对象
     * */
    public static XWPFDocument openDocx(String url) {
        XWPFDocument doc = null;
        try (InputStream input = new FileInputStream(url)) {
            doc = new XWPFDocument(input);
            return doc;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }

    /**
     * @param outDoc    输出的文档对象
     * @param out       输出内容
     * @return void
     * */
    public static void writeDocx(XWPFDocument outDoc, OutputStream out) {
        try {
            outDoc.write(out);
            out.flush();
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
要用POI替换Word文档中的图片,可以使用如下的代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFPicture; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.util.IOUtils; public class ReplaceImageInWord { public static void main(String[] args) throws Exception { // 读入原始 Word 文档 FileInputStream fis = new FileInputStream(new File("original.docx")); XWPFDocument document = new XWPFDocument(fis); fis.close(); // 获取所有段落 for (XWPFParagraph paragraph : document.getParagraphs()) { // 获取段落中所有的图片 for (XWPFPicture picture : paragraph.getCTP().getRArray(0).getDrawingArray(0).getInlineArray()) { // 获取图片二进制数据 byte[] data = picture.getCTPicture().getBlipFill().getBlip().getEmbeddedDocument().getByteArray(); // 替换图片 FileInputStream fis2 = new FileInputStream(new File("new-image.png")); byte[] newData = IOUtils.toByteArray(fis2); fis2.close(); picture.getCTPicture().getBlipFill().getBlip().setEmbeddedDocument(newData); } } // 保存 Word 文档 FileOutputStream fos = new FileOutputStream(new File("new.docx")); document.write(fos); fos.close(); document.close(); } } ``` 这段代码中,我们读入原始 Word 文档,然后遍历文档中的所有段落,再遍历每个段落中的所有图片。我们可以使用 `getCTP().getRArray(0).getDrawingArray(0).getInlineArray()` 获取每个段落中的图片。然后,我们可以使用 `getCTPicture().getBlipFill().getBlip().getEmbeddedDocument().getByteArray()` 获取图片的二进制数据,使用 `setEmbeddedDocument()` 方法替换图片。最后,我们保存修改后的 Word 文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值