Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

Replace Text Word Document

$
0
0
- I am using Apache POI to try and replace certain instances of text in a word doc with another word. I have MergeFields in the word doc I am scanning the string for. The code executes and replaces the occurences successfully however the mergefields become fubarred.

In the output doc the word ERGEFIELD appears before every mergefield cell without the "M" and includes the name of the MERGEFIELD cell I guess and ALSO the actual text in the cell obviously messing up the entire format of the doc.

Code below.

Code:

{       
      String inputFilename = "C:\\a.doc";

      String outputFilename = "C:\\Users\\Me\\Desktop\\b.doc";
      POIFSFileSystem fs = null;
      FileInputStream fis = new FileInputStream(inputFilename);
      fs = new POIFSFileSystem(fis);

      HWPFDocument doc = new HWPFDocument(fs);

      Range range = doc.getRange();

      range.replaceText("This", "That");

      FileOutputStream fos = new FileOutputStream(outputFilename);
      doc.write(fos);

      fis.close();
      fos.close();
    }


Viewing all articles
Browse latest Browse all 120

Trending Articles