Word中的替換功能以查找指定文本然後替換為新的文本,可單個替換或全部替換。以下将要介紹的内容,除常見的以文本替換文本外,還将介紹使用不同對象進行替換的方法,具體可包括:
1. 指定字元串内容替換文本(通過方法replce(matchString, newValue, caseSensitive, wholeWord );直接指定替換的新字元串内容)
2. 擷取文檔内容替換文本(通過方法replace(String matchString, TextSelection textSelection, boolean caseSensitive, boolean wholeWord);替換指定文本)
3. 圖檔替換文本
4. 圖檔替換圖檔
使用工具及jar導入:
需要使用 Free Spire.Doc for Java 的jar包,可手動下載下傳并解壓導入Spire.Doc.jar檔案到Java程式,也可以通過maven倉庫下載下傳導入。
【示例1】指定字元串内容替換文本
import com.spire.doc.*;
public class ReplaceTextWithText {
public static void main(String[] args) {
//加載文檔
Document doc = new Document();
doc.loadFromFile("test.docx");
//要替換第一個出現的指定文本,隻需在替換前調用setReplaceFirst方法來指定隻替換第一個出現的指定文本
//doc.setReplaceFirst(true);
//調用方法用新文本替換原文本内容
doc.replace("系統測試", "System Testing", false, true);
//儲存文檔
doc.saveToFile("ReplaceAllText.docx",FileFormat.Docx_2013);
doc.dispose();
}
}
【示例2】擷取文檔内容替換文本
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中的指定内容
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中查找到的内容替換文檔1中的指定字元串
doc1.replace("System Test, ST",textSelection,false,true);
//儲存文檔1
doc1.saveToFile("ReplaceTextWithDocument.docx",FileFormat.Docx_2013);
doc1.dispose();
}
}
兩個用于測試的文檔如下,将文檔2中的文本内容替換文檔1中的指定文本内容:
替換結果:
【示例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("系統測試",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");
}
}*/
//儲存結果文檔
doc.saveToFile("ReplaceWithImage.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
(完)