Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to create MS Word Doc using HWPF POI

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to create MS Word Doc using HWPF POI
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!");
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream("C:\\temp\\sample1.doc");
doc.write(out);
out.flush();
out.close();

thanks a lot for replying.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which statement is throwing an exception? Also, you should close the input stream before using the same file for output.
 
dipali chaudhari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dipali,

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

Please post the code if you have one.

Thanks
Kiran Venkat
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"));
}
}

 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[ UD: Deleted. Please don't post unrelated questions into existing topics. That's called "hijacking" and is generally frowned upon. ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic