您的位置 首页 java

Java 用新文本/图片替换Word文档中的指定文本

在修改Word文档中的文本内容时,有时我们需要替换文中某个字或词,如果我们一个一个去删除编辑,这样会很浪费时间,而且也有可能会漏修改。这时,我们就可以借助代码一次性将所有指定的文本用新内容进行替换,既节省时间提高效率 ,同时也能把握准确度。

本文将在一款名为 Free Spire.Doc for Java 控件的帮助下,用Java代码实现对Word文本进行替换操作。其中,我将从以下三个方面来进行代码演示:

  • 用新文本替换 Word文档中的所有指定文本
  • 用新文本替换Word文档中 第一个出现的指定文本
  • 用图片替换Word文档中的所有指定文本

Jar包导入

在运行代码前,我们需要创建运行环境。首先下载安装配置JDK 1.8.0和Intellij IDEA 2019,接着需将控件里的Jar包导入 IDEA 。导入方式有两种:其一,在 E-iceblue中文官网 上获取产品包,解压后在lib文件夹下找到Spire.Doc.jar,最后手动导入IDEA。其二,在IDEA中创建Maven项目,然后在pom.xml文件下 配置Maven仓库路径及指定Free Spire.Doc for Java的Maven依赖。

 <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>  

代码示例

示例一 用新文本替换Word文档中的所有指定文本

 import com.spire.doc.*;

public class ReplaceAllText {
    public static void main(String[] args) {
        //加载Word文档
        Document document = new Document("C:\Users\Test1\Desktop\Sample.docx");

        //使用新文本替换文档中的指定文本
        document.replace("圣诞节", "Christmas", false, true);

        //保存文档
        document.saveToFile("output/ReplaceAllMatchedText.docx", FileFormat.Docx_2013);
    }
}  

替换效果

示例二 用新文本替换Word文档中第一个出现的指定文本

 import com.spire.doc.*;
public class ReplaceFirstText {
    public static void main(String[] args) {
        //加载Word文档
        Document document = new Document("C:\Users\Test1\Desktop\Sample.docx");

        //设置只替换第一个出现的指定文本
        document.setReplaceFirst(true);

        //使用新文本替换第一个出现的指定文本
        document.replace("圣诞节", "Christmas", false, true);

        //保存文档
        document.saveToFile("output/ReplaceFirstMatchedText.docx", FileFormat.Docx_2013);
    }
}  

替换效果

示例三 用图片替换Word文档中的所有指定文本

 import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

public class ReplaceTextWithImage {
    public static void main(String[] args) {
        //加载示例文档
        Document document = new Document();
        document.loadFromFile("C:\Users\Test1\Desktop\Sample.docx");

        //查找文档中的字符串“成都冰蓝科技有限公司”
        TextSelection[] selections = document.findAllString("圣诞节", true, true);

        //用图片替换文字
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {

            TextSelection textSelection = (TextSelection)obj;
            DocPicture pic = new DocPicture(document);
            pic.loadImage("C:\Users\Test1\Desktop\image.png");
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }

        //保存文档
        document.saveToFile("output/ReplaceTextWithImage.docx", FileFormat.Docx_2013);
    }
}  

替换效果

文章来源:智云一二三科技

文章标题:Java 用新文本/图片替换Word文档中的指定文本

文章地址:https://www.zhihuclub.com/186677.shtml

关于作者: 智云科技

热门文章

网站地图