Hi
I am able to create a word document supplying a specfic template as in below code.
But the special fields like "Check Box form field" are lost in the generated output.
Can anyone help ..?
import java.io.*;
import org.apache.poi.hpsf.CustomProperties;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class CreateWordDoc {
public static void main (
String[] args) throws Exception {
// POI apparently can't create a document from scratch,
// so we need an existing empty dummy document
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("c:\\data\\sample.dot"));
HWPFDocument doc = new HWPFDocument(fs);
// centered paragraph with large font size
Range range = doc.getRange();
Range r1 = doc.getRange();
for ( int i = 0; i < r1.numSections(); ++i ) {
Section s = r1.getSection(i);
for (int x = 0; x < s.numParagraphs(); x++)
{
Paragraph p = s.getParagraph(x);
for (int z = 0; z < p.numCharacterRuns(); z++){
//character run
CharacterRun run = p.getCharacterRun(z);
//character run text
String text = run.text();
if( text.equalsIgnoreCase("CurrDate")) {
String theDate = "28-05-2009";
if ( theDate != null ){
run.replaceWith(theDate);
} }
if( text.equalsIgnoreCase("XYZ")) {
String theapplicantName = "Mr.John";
if ( theapplicantName != null ){
run.replaceWith(theapplicantName);
} } } }
}
// add a custom document property (needs POI 3.5; POI 3.2 doesn't save custom properties)
DocumentSummaryInformation dsi = doc.getDocumentSummaryInformation();
CustomProperties cp = dsi.getCustomProperties();
if (cp == null)
cp = new CustomProperties();
cp.put("myProperty", "foo bar baz");
dsi.setCustomProperties(cp);
doc.write(new FileOutputStream("c:\\data\\new-hwpf-file.doc"));
}
}