Java Word中的文本、圖片替換功能

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

 

(完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章