
Can anyone help me how can i write a java code to write a string with format such as font type and style to a document file? Thanks in advance.

import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class WriteInDocument{
public static void main(String[] args)throws IOException{
File file = new File("C:/sample.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!");
run.setFontSize(2 * 18);
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream(new File("C:/new.doc"));
doc.write(out);
out.flush();
out.close();
}
}
For the above code, poi-scratchpad-3.7-20101029.jar is needed.

its not working correctly..font style is not getting changed. any1 tell me right method
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.