aspose file tools
The moose likes Other Open Source Projects and the fly likes How to create MS Word Doc using HWPF POI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to create MS Word Doc using HWPF POI" Watch "How to create MS Word Doc using HWPF POI" New topic

How to create MS Word Doc using HWPF POI

dipali chaudhari

Joined: Mar 18, 2009
Posts: 5
How to create MS Word Doc using HWPF POI
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
Welcome to JavaRanch.

The steps are basically the following: 1) create a POIFSFileSystem, 2) from that, create an HWPFDocument, 3) obtain the document's Range by calling getRange, 4) insert text by calling one of the insertAfter methods on it (that will return a CharacterRun or Paragraph object that you can use to style the text), 6) call HWPFDocument.write to save the document.
dipali chaudhari

Joined: Mar 18, 2009
Posts: 5
Hi Ulf Dittmer,
After running following code, I found ArrayIndexOutOfBoundsException, How I can solve that, Can please help me.

HWPFDocument doc = new HWPFDocument(new FileInputStream("C:\\temp\\sample1.doc"));
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!!! HAHAHAHAHA I DID IT!!!");
OutputStream out = new FileOutputStream("C:\\temp\\sample1.doc");

thanks a lot for replying.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
Which statement is throwing an exception? Also, you should close the input stream before using the same file for output.
dipali chaudhari

Joined: Mar 18, 2009
Posts: 5
Hi Ulf,

Now, I have successfully created the word document.

But actually, I want to create a word document from predefined template file.

Templates will be consists of fields & static part (both will contain MS Word provided formatting - like letter with alignments, documents with table, header & footer and etc.), at runtime data for field & input template will be provided.
Program should merge it and generate output in word document format.

Please let me know your suggestions, if any.
kiran venkat m

Joined: May 28, 2009
Posts: 4
Hi dipali,

Had any luck on "create a word document from predefined template file" ?

Please post the code if you have one.

Kiran Venkat
Jasmin Ramesh

Joined: Jun 01, 2009
Posts: 3

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 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\\"));
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 ){
} }
if( text.equalsIgnoreCase("XYZ")) {
String theapplicantName = "Mr.John";
if ( theapplicantName != null ){
} } } }

// 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");
doc.write(new FileOutputStream("c:\\data\\new-hwpf-file.doc"));

Martijn Verburg

Joined: Jun 24, 2003
Posts: 3274

Hi Jasmin and welcome to Javaranch!

We like to try and have one question per thread, so if you could start a new thread with your particular question it would be much appreciated! Also, don't forget you can use the "Code" button to highlight your code (makes it easier for us top help you).

Happy ranching!

Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
dev day

Joined: Jul 22, 2009
Posts: 3
[ UD: Deleted. Please don't post unrelated questions into existing topics. That's called "hijacking" and is generally frowned upon. ]
subject: How to create MS Word Doc using HWPF POI