Page 1 of 1

reading word template (a .DOT file)

PostPosted: Mon Dec 13, 2010 4:59 pm
by sudheer.raju
Dear All,
My First question, which is the best API to read/write MS Word files using JAVA.

Are there API's/frameworks to read .DOT files using JAVA.

I have a requirement like this, is this achievable using JAVA.
Can we read a .DOT (a word template file) and convert it to a .DOC (ms word document file) programmatically by filling some values at given places.

Ex: My MS Word template file (.DOT) would have "Attribute Display Name: Attribute Value", like "Emp Id: Emp_Id", "Emp Name: Emp_Name", "Company: Comp_Name", the program should fill these values with the array of String values (ex: sudheer, 123456, mycompany,...) I pass, values should be filled "Emp Id: 123456", "Emp Name: sudheer", "Company: mycompany", etc...and then export/save the file as a MS Word document, (.DOC).

Let me know if such thing is achievable mostly in Java or C/C++.

Thanks in advance.

--
Sudheer

Re: reading word template (a .DOT file)

PostPosted: Mon Dec 13, 2010 6:11 pm
by jason
docx4j handles the ~2007 and later Open XML formats (docx/dotx etc).

If you specifically want to work with the old binary formats (doc/dot), you need to look elsewhere - perhaps automating OpenOffice (or of course Word itself).

Otherwise, docx4j offers several different ways to populate a template as you describe.

Re: reading word template (a .DOT file)

PostPosted: Tue Dec 14, 2010 5:11 pm
by sudheer.raju
Thanks a lot Jason for the reply.

So as you have said, can we use docx4j to read a .dotx (or appropriate 2007 template) file and populate values into that.
If yes then I shall use the .dotx template, I shall covert/create the .dot to dotx and then populate values into a template file.
Let me know how to populate a template as you suggested, using docx4j.
Pls share some code if you already have, it would be of great help to me.

Thanks,
Sudheer

Re: reading word template (a .DOT file)

PostPosted: Tue Dec 14, 2010 5:37 pm
by jason
Please see the Getting Started guide, under headings "Text substitution" and "Text substitution via data bound content controls".

Re: reading word template (a .DOT file)

PostPosted: Fri Feb 10, 2012 12:15 am
by prasathm
package org.FileReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.poifs.filesystem.POIFSDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.employeeDetails.EmployeeDetails;

public class DocReader {

public void readDocFile() {
String filesname = "C:/temp/dotPERFORMANCE_APPRAISAL.dot";
POIFSFileSystem fs = null;

try {
File file = new File(filesname);
fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);

WordExtractor we = new WordExtractor(doc);

String paragraphstr = we.getText();

StringBuilder stringBuilder = new StringBuilder(
paragraphstr.length());
EmployeeDetails details = new EmployeeDetails();
details.setEmployeeId("123456789");
details.setEmployeeName("xxxxxx");
details.setQualification("B.E");
details.setEmployeeJoiningDate("04/01/2012");
details.setEvaluationPeriod("2 years");
details.setCurrentJobTitle("trainee");
details.setDepartment("java");
details.setSupervisor("yyyyy");
details.setEvaluationDate("05/6/2012");

/*
for (int i = 0; i < paragraphstr1.length; i++) { paragraphstr1[i]
= paragraphstr1[i] .replaceAll("<jwEmployeeId>",
details.getEmployeeId()) .replaceAll("<jwEmployeeName>",
details.getEmployeeName()) .replaceAll("<jwQualification>",
details.getQualification()) .replaceAll("<jwEOJDate>",
details.getEmployeeJoiningDate()) .replaceAll("<jwEvalPeriod>",
details.getEvaluationPeriod()) .replaceAll("<jwCurrJob>",
details.getCurrentJobTitle()) .replaceAll("<jwDept>",
details.getDepartment()) .replaceAll("<jwSupervisor>",
details.getSupervisor()) .replaceAll("<jwEvalDate>",
details.getEvaluationDate());
System.out.println(paragraphstr1[i]); }
*/

if (filesname.endsWith(".doc") || filesname.endsWith(".dot")) {
String message = paragraphstr.replaceAll("<jwEmployeeId>",
details.getEmployeeId());
String message1 = message.replaceAll("<jwEmployeeName>",
details.getEmployeeName());
String message2 = message1.replaceAll("<jwQualification>",
details.getQualification());
String message3 = message2.replaceAll("<jwEOJDate>",
details.getEmployeeJoiningDate());
String message4 = message3.replaceAll("<jwEvalPeriod>",
details.getEvaluationPeriod());
String message5 = message4.replaceAll("<jwCurrJob>",
details.getCurrentJobTitle());
String message6 = message5.replaceAll("<jwDept>",
details.getDepartment());
String message7 = message6.replaceAll("<jwSupervisor>",
details.getSupervisor());
String finalMessage = message7.replaceAll("<jwEvalDate>",
details.getEvaluationDate());
finalMessage.trim();
stringBuilder.append(finalMessage);

System.out.println("message" + stringBuilder.toString());
/*String path = file.getCanonicalPath();
System.out.println("abs path" + file.getAbsolutePath());

POIFSDocument event = new POIFSDocument("output.doc",
new FileInputStream(new File(
"c:/temp/output/output1.doc")));
event.getViewableIterator();
doc.getSavedByTable();*/

Range range = doc.getRange();
CharacterRun run = range.insertBefore(finalMessage.toString());
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);

OutputStream out = new FileOutputStream(new File(
"c:/temp/output/output1.doc"));

doc.write(out);
out.flush();
out.close();
}

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
DocReader reader = new DocReader();
reader.readDocFile();
}
}


package org.employeeDetails;
import java.io.Serializable;

public class EmployeeDetails implements Serializable {

private static final long serialVersionUID = -943913338383549645L;
private String employeeId;
private String employeeName;
private String qualification;
private String employeeJoiningDate;
private String evaluationPeriod;
private String currentJobTitle;
private String department;
private String supervisor;
private String evaluationDate;


public final String getEmployeeId() {
return employeeId;
}


public final void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}


public final String getEmployeeName() {
return employeeName;
}

public final void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public final String getQualification() {
return qualification;
}

public final void setQualification(String qualification) {
this.qualification = qualification;
}

public final String getEmployeeJoiningDate() {
return employeeJoiningDate;
}

public final void setEmployeeJoiningDate(String employeeJoiningDate) {
this.employeeJoiningDate= employeeJoiningDate;
}

public final String getEvaluationPeriod() {
return evaluationPeriod;
}


public final void setEvaluationPeriod(String evaluationPeriod) {
this.evaluationPeriod = evaluationPeriod;
}


public final String getCurrentJobTitle() {
return currentJobTitle;
}

public final void setCurrentJobTitle(String currentJobTitle) {
this.currentJobTitle = currentJobTitle;
}

public final String getDepartment() {
return department;
}


public final void setDepartment(String department) {
this.department = department;
}

public final String getSupervisor() {
return supervisor;
}


public final void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}


public final String getEvaluationDate() {
return evaluationDate;
}


public final void setEvaluationDate(String evaluationDate) {
this.evaluationDate= evaluationDate;
}


}



construct your own file using these two codes.
also make sure ur input file location

Re: reading word template (a .DOT file)

PostPosted: Fri Feb 10, 2012 12:18 am
by prasathm
this is the word input file

Re: reading word template (a .DOT file)

PostPosted: Tue Jun 20, 2017 6:47 pm
by mpawar
Hello,
Can you please let me know how to create a template document similar to the "dotPERFORMANCE_APPRAISAL.dot" having fields like <jwEmployeeId>?