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(); } }

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/175701.html
標籤:Java
上一篇:微服務平臺技術架構
