Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.spire.doc.Document;
- import com.spire.doc.FileFormat;
- import com.spire.doc.documents.TextSelection;
- import java.awt.*;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class Word{
- public static void main(String[] args) {
- String input = "/Users/rifky/Desktop/replaceWithText.docx";
- String output = "/Users/rifky/Desktop/out/replaceWithText.docx";
- String findText = "then";
- String replacementWord = "kemudian";
- //load Word document
- Document document = new Document();
- document.loadFromFile(input, FileFormat.Docx);
- //replace the text
- document.replace(findText, replacementWord, false, true);
- //highlight the replaced text
- TextSelection[] textSelections = document.findAllString(replacementWord, false, true);
- //set highlight
- for (TextSelection selection : textSelections) {
- selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.WHITE);
- }
- //save the document
- document.saveToFile(output,FileFormat.Docx);
- }
- public static String readTextFromFile(String fileName) throws IOException{
- StringBuffer sb = new StringBuffer();
- BufferedReader br = new BufferedReader(new FileReader(fileName));
- String content = null;
- while ((content = br.readLine()) != null) {
- sb.append(content);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment