writing document (.doc) file from java with format such as font type, font style

Ads
 

writing document (.doc) file from java with format such as font type, font style

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.

View Answers

March 14, 2011 at 4:48 PM

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.


September 22, 2012 at 3:42 PM

its not working correctly..font style is not getting changed. any1 tell me right method

Ads









Related Tutorials/Questions & Answers:
writing document (.doc) file from java with format such as font type, font style
writing document (.doc) file from java with format such as font type, font... with format such as font type and style to a document file? Thanks in advance... FileInputStream(file)); HWPFDocument doc = new HWPFDocument(fs); Range range
The type Font is ambiguous where generating pdf file from jsp page - JSP-Servlet
The type Font is ambiguous where generating pdf file from jsp page  I am geeting ambiguous error .Do u want to add any jar files   Hi Friend, Import the class Font into your JSP page like the following
Advertisements
serch document from .doc and .docx file
serch document from .doc and .docx file  how to find particular word from .doc and .docx file and that date insert into a data base
Explain the parameters of Font class constructor.
the parameters of Font class constructor.   Java Font class: Font(String name,int style,int size): It creates a new Font from the specified name, style.... If name is null, the name of the new Font is set to the name "Default". style
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written... the binary file from another program as follows: m_dis = new DataInputStream... having the format: 00501 40.815400 -73.045600. The entries I get in the binary file
font changing
font changing  I have created a welcome page and when I try to change the font style of the label named lblCName it does not change.Indentify..., placeOrdr; Icon image; Font fnt; public void init(){ fnt=new Font("Verdana
ModuleNotFoundError: No module named 'font-font-awesome'
ModuleNotFoundError: No module named 'font-font-awesome'  Hi, My... named 'font-font-awesome' How to remove the ModuleNotFoundError: No module named 'font-font-awesome' error? Thanks   Hi, In your
HTML font tag.
for designing not for development.</font> <br/> <font style...HTML font tag. Description : It is a HTML tag, which is used for changing the view of your text. Which will display on view page. Syntax : <font
UILabel Font Size
UILabel Font Size  Hi Developers, How to set font size of UILabel? I am creating label programmatically and I have to set the font size programatically. How I can set UILabel Font Size? Thanks   HI, Following
Change font in swing?
Change font in swing?  I want to use marathi language on labels,buttons
How to read the .doc/ .docx file in Java Program
How to read the .doc/ .docx file in Java Program  Hi, I am beginner in Java programming language. Can anybody explain How to read .doc file in Java.... This way you can read the .doc or .docx file in Java programming language. Thanks
doc file - Java Beginners
doc file   i want to read .doc file ?....it is a part of my project plz help. if possible , can anyone give me the code
ModuleNotFoundError: No module named 'font-caladea'
ModuleNotFoundError: No module named 'font-caladea'  Hi, My Python... 'font-caladea' How to remove the ModuleNotFoundError: No module named 'font-caladea' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'font-converter'
ModuleNotFoundError: No module named 'font-converter'  Hi, My... named 'font-converter' How to remove the ModuleNotFoundError: No module named 'font-converter' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'font-roboto'
ModuleNotFoundError: No module named 'font-roboto'  Hi, My Python... 'font-roboto' How to remove the ModuleNotFoundError: No module named 'font-roboto' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'font-line'
ModuleNotFoundError: No module named 'font-line'  Hi, My Python... 'font-line' How to remove the ModuleNotFoundError: No module named 'font... have to install padas library. You can install font-line python with following
ModuleNotFoundError: No module named 'font-unicode'
ModuleNotFoundError: No module named 'font-unicode'  Hi, My Python... 'font-unicode' How to remove the ModuleNotFoundError: No module named 'font-unicode' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'font-v'
ModuleNotFoundError: No module named 'font-v'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'font-v' How to remove the ModuleNotFoundError: No module named 'font-v'
ModuleNotFoundError: No module named 'Font-Sampler'
ModuleNotFoundError: No module named 'Font-Sampler'  Hi, My Python... 'Font-Sampler' How to remove the ModuleNotFoundError: No module named 'Font-Sampler' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'font-ttfa'
ModuleNotFoundError: No module named 'font-ttfa'  Hi, My Python... 'font-ttfa' How to remove the ModuleNotFoundError: No module named 'font... have to install padas library. You can install font-ttfa python with following
ModuleNotFoundError: No module named 'font-unicode'
ModuleNotFoundError: No module named 'font-unicode'  Hi, My Python... 'font-unicode' How to remove the ModuleNotFoundError: No module named 'font-unicode' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'maoyan-font'
ModuleNotFoundError: No module named 'maoyan-font'  Hi, My Python... 'maoyan-font' How to remove the ModuleNotFoundError: No module named 'maoyan-font' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'mpl-font'
ModuleNotFoundError: No module named 'mpl-font'  Hi, My Python...-font' How to remove the ModuleNotFoundError: No module named 'mpl-font... to install padas library. You can install mpl-font python with following
ModuleNotFoundError: No module named 'read-font'
ModuleNotFoundError: No module named 'read-font'  Hi, My Python... 'read-font' How to remove the ModuleNotFoundError: No module named 'read-font' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'font-intuitive'
ModuleNotFoundError: No module named 'font-intuitive'  Hi, My... named 'font-intuitive' How to remove the ModuleNotFoundError: No module named 'font-intuitive' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'font-ttfa'
ModuleNotFoundError: No module named 'font-ttfa'  Hi, My Python... 'font-ttfa' How to remove the ModuleNotFoundError: No module named 'font... have to install padas library. You can install font-ttfa python with following
ModuleNotFoundError: No module named 'font-roboto'
ModuleNotFoundError: No module named 'font-roboto'  Hi, My Python... 'font-roboto' How to remove the ModuleNotFoundError: No module named 'font-roboto' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'font-v'
ModuleNotFoundError: No module named 'font-v'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'font-v' How to remove the ModuleNotFoundError: No module named 'font-v'
How to set Marathi font to Label??
How to set Marathi font to Label??  Hello, I am doing one project in java-Swing Farm Management i want to set Marathi label in that so how i can? plzzzz help me its urgent
how to use marathi font in swing?
how to use marathi font in swing?  I want to use marathi language on labels,buttons
Getting A File's Mime Type In Java
Getting A File's Mime Type In Java  Getting A File's Mime Type In Java

Ads