4

I am trying to read a word document template and then replace the variables from the template, with user given data.without changing the heading or style as on the tempate.I'm not sure that what I am doing is correct way or not but this is the way I started:

'XWPFDocument docx = new XWPFDocument(
            new FileInputStream(
                    "D://TestDocumentPrep/src/XXXXX_TestReport_URL_Document.docx"));
    XWPFWordExtractor we = new XWPFWordExtractor(docx);
    String textData = we.getText();
    String newTestData=textData.replace("$var_source_code$", list.get(1))
            .replace("$var_rsvp_code$", list.get(2))
            .replace("$var_ssn$", list.get(3))
            .replace("$var_zip_code$", list.get(4))
            .replace("$var_point_for_business$",
                    anotherData.getPointForBusiness())
            .replace("$var_E1_url$", anotherData.getE1url())
            .replace("$var_E2_url$", anotherData.getE2url())
            .replace("$var_E3_url$", anotherData.getE3url());
    System.out.println(newTestData);'

This is what I have done.But Its reading the content of the word document as a string and replacing the variables.Now how to put the replaced string in word document in the template format?

Here I found something but Not exactly my solution

Here also I found something but not exact solution

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Sayak Choudhury
  • 167
  • 1
  • 1
  • 13

1 Answers1

4

Hi I am able to find the solution

Here is the code I have used for editing my word document and it's fine with both .doc and .docx format of the file which i want to edit and generate an edited new word document without changing the base template.

public void wordDocProcessor(AnotherVO anotherData, ArrayList<String> list,
        String sourse, String destination) throws IOException,
        InvalidFormatException {
    XWPFDocument doc = new XWPFDocument(OPCPackage.open(sourse
            + "XXXXX_TestReport_URL_Document.doc"));
    for (XWPFTable tbl : doc.getTables()) {
        for (XWPFTableRow row : tbl.getRows()) {
            for (XWPFTableCell cell : row.getTableCells()) {
                for (XWPFParagraph p : cell.getParagraphs()) {
                    for (XWPFRun r : p.getRuns()) {
                        String text = r.getText(0);
                        if (text != null
                                && text.contains("var_source_code")) {
                            text = text.replace("var_source_code",
                                    list.get(1));
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_rsvp_code")) {
                            text = text.replace("var_rsvp_code",
                                    list.get(2));
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_ssn")) {
                            text = text.replace("var_ssn", list.get(3));
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_zip_code")) {
                            text = text
                                    .replace("var_zip_code", list.get(4));
                            r.setText(text, 0);
                        }
                        if (text != null
                                && text.contains("var_point_for_business")) {
                            text = text.replace("var_point_for_business",
                                    anotherData.getPointForBusiness());
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_E1_url")) {
                            text = text.replace("var_E1_url",
                                    anotherData.getE1url());
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_E2_url")) {
                            text = text.replace("var_E2_url",
                                    anotherData.getE2url());
                            r.setText(text, 0);
                        }
                        if (text != null && text.contains("var_E3_url")) {
                            text = text.replace("var_E3_url",
                                    anotherData.getE3url());
                            r.setText(text, 0);
                        }
                    }
                }
            }
        }
    }
    doc.write(new FileOutputStream(destination + list.get(0)
            + "_TestReport_URL_Document.doc"));
}
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
Sayak Choudhury
  • 167
  • 1
  • 1
  • 13