Java實(shí)現(xiàn)替換Word中文本和圖片功能
前言
Word中的替換功能以查找指定文本然后替換為新的文本,可單個(gè)替換或全部替換。以下將要介紹的內(nèi)容,除常見的以文本替換文本外,還將介紹使用不同對象進(jìn)行替換的方法,具體可包括:
1. 指定字符串內(nèi)容替換文本(通過方法replce(matchString, newValue, caseSensitive, wholeWord );直接指定替換的新字符串內(nèi)容)
2. 獲取文檔內(nèi)容替換文本(通過方法replace(String matchString, TextSelection textSelection, boolean caseSensitive, boolean wholeWord);替換指定文本)
3. 圖片替換文本
4. 圖片替換圖片
使用工具及jar導(dǎo)入:
需要使用 Free Spire.Doc for Java 的jar包,可手動下載并解壓導(dǎo)入Spire.Doc.jar文件到Java程序,也可以通過maven倉庫下載導(dǎo)入。
1.指定字符串內(nèi)容替換文本
import com.spire.doc.*;
public class ReplaceTextWithText {
public static void main(String[] args) {
//加載文檔
Document doc = new Document();
doc.loadFromFile("test.docx");
//要替換第一個(gè)出現(xiàn)的指定文本,只需在替換前調(diào)用setReplaceFirst方法來指定只替換第一個(gè)出現(xiàn)的指定文本
//doc.setReplaceFirst(true);
//調(diào)用方法用新文本替換原文本內(nèi)容
doc.replace("系統(tǒng)測試", "System Testing", false, true);
//保存文檔
doc.saveToFile("ReplaceAllText.docx",FileFormat.Docx_2013);
doc.dispose();
}
}
2.獲取文檔內(nèi)容替換文本
import com.spire.doc.*;
import com.spire.doc.documents.TextSelection;
public class ReplaceTextWithDocument {
public static void main(String[] args) {
//加載文檔1
Document doc1 = new Document();
doc1.loadFromFile("test.docx");
//加載文檔2
Document doc2 = new Document();
doc2.loadFromFile("TargetFile.docx");
//查找文檔2中的指定內(nèi)容
TextSelection textSelection = doc2.findString("Falling under the scope of black box testing, " +
"system testing is a phase in the software " +
"testing cycle where a total and integrated" +
" application /system is tested.",false,false);
//用文檔2中查找到的內(nèi)容替換文檔1中的指定字符串
doc1.replace("System Test, ST",textSelection,false,true);
//保存文檔1
doc1.saveToFile("ReplaceTextWithDocument.docx",FileFormat.Docx_2013);
doc1.dispose();
}
}兩個(gè)用于測試的文檔如下,將文檔2中的文本內(nèi)容替換文檔1中的指定文本內(nèi)容:

替換結(jié)果:

3.圖片替換文本
import com.spire.doc.*;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
public class ReplaceTextWithImg {
public static void main(String[] args) {
//加載文檔
Document doc = new Document("test.docx");
//查找需要替換的字符串
TextSelection[] textSelection = doc.findAllString("系統(tǒng)測試",true,false);
int index ;
//加載圖片替換文本字符串
for (Object obj : textSelection) {
TextSelection Selection = (TextSelection)obj;
DocPicture pic = new DocPicture(doc);
pic.loadImage("tp.png");
TextRange range = Selection.getAsOneRange();
index = range.getOwnerParagraph().getChildObjects().indexOf(range);
range.getOwnerParagraph().getChildObjects().insert(index,pic);
range.getOwnerParagraph().getChildObjects().remove(range);
}
//保存文檔
doc.saveToFile("ReplaceTextWithImage.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
4.圖片替換圖片
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;
public class ReplacePictureWithPicture {
public static void main(String[] args) {
//加載Word文檔
Document doc = new Document();
doc.loadFromFile("sample.docx");
//獲取文檔中的指定段落
Section section = doc.getSections().get(0);
Paragraph para = section.getParagraphs().get(0);
//替換段落中的第一張圖片
Object obj = para.getChildObjects().get(0);
if(obj instanceof DocPicture){
DocPicture pic = (DocPicture)obj;
pic.loadImage("tp.png");
}
/*//批量替換圖片
for(int i =0;i < section.getParagraphs().getCount();i++){
Object obj = section.getParagraphs().get(i).getChildObjects();
if(obj instanceof DocPicture){
DocPicture pic = (DocPicture)obj;
pic.loadImage("tp.png");
}
}*/
//保存結(jié)果文檔
doc.saveToFile("ReplaceWithImage.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
到此這篇關(guān)于Java實(shí)現(xiàn)替換Word中文本和圖片功能的文章就介紹到這了,更多相關(guān)Java替換Word文本 圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用listIterator逆序arraylist示例分享
對于列表而言,除了Iterator,還提供了一個(gè)功能更加強(qiáng)大的ListIterator。它可以實(shí)現(xiàn)逆序遍歷列表中的元素。本示例將使用其逆序遍歷ArrayList2014-02-02
SpringBoot中配置多數(shù)據(jù)源的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot中配置多數(shù)據(jù)源的方法的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
Spring基于常用AspectJ切點(diǎn)表達(dá)式使用介紹
AspectJ是一個(gè)基于Java語言的AOP框架,使用AspectJ需要導(dǎo)入Spring?AOP和AspectJ相關(guān)jar包,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
SpringBoot+MyBatisPlus對Map中Date格式轉(zhuǎn)換處理的方法詳解
在?SpringBoot?項(xiàng)目中,?如何統(tǒng)一?JSON?格式化中的日期格式。本文將為大家介紹一種方法:利用MyBatisPlus實(shí)現(xiàn)對Map中Date格式轉(zhuǎn)換處理,需要的可以參考一下2022-10-10
Java幾個(gè)實(shí)例帶你進(jìn)階升華下篇
與其明天開始,不如現(xiàn)在行動,本文為你帶來幾個(gè)Java書寫的實(shí)際案例,對鞏固編程的基礎(chǔ)能力很有幫助,快來一起往下看看吧2022-03-03
java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Java實(shí)現(xiàn)Excel數(shù)據(jù)驗(yàn)證功能
在Java中,開發(fā)者可以使用一些開源的庫(如Apache POI)來添加、修改和處理Excel中的數(shù)據(jù),下面我們就來看看如何使用Java實(shí)現(xiàn)添加,修改和刪除Excel數(shù)據(jù)驗(yàn)證吧2023-10-10
JAVA集成Freemarker生成靜態(tài)html過程解析
這篇文章主要介紹了JAVA集成Freemarker生成靜態(tài)html過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

