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 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
Simple Font Paint Example
. A font is a specific style of displaying characters. It describes the design, size... is an element of writing and displaying characters in a style. Class GlyphVector... = "Java is an Object Oriented Programming Language.";         Font font = new
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
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
Java get available Font
Java get available Font   ... font. In order to retrieve all the available fonts, we have used the AWT... the available GraphicsDevices and Font objects. getAllFonts()- This is the method
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
Font Selection
have to decide the specific font name for the specific text from the pdf content and the font name has to be mentioned in the java code during...(String input) is use to process the font on the text .Its return type
Draw Font Using Canvas Example
, int style, int size) Font getHeight() int...;styles [style], sizes [size]);   g.setFont(font);  ... Draw Font Using Canvas Example     
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
Font Derivation
(int style) creates a new font object, applying a new style... Font Derivation       In this section, you will studied about the font derivation. 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
Maven dependency for org.webjars - font-awesome version 5.15.2 is released. Learn to use font-awesome version 5.15.2 in Maven based Java projects
this version ( org.webjars - font-awesome version 5.15.2 ) in their Java project... and includes  org.webjars - font-awesome version 5.15.2 java library in your project...Maven dependency for  org.webjars  - Version 5.15.2 of font-awesome
Maven dependency for org.webjars - font-awesome version 5.15.3 is released. Learn to use font-awesome version 5.15.3 in Maven based Java projects
this version ( org.webjars - font-awesome version 5.15.3 ) in their Java project... and includes  org.webjars - font-awesome version 5.15.3 java library in your project...Maven dependency for  org.webjars  - Version 5.15.3 of font-awesome
Maven dependency for org.webjars - font-awesome version 4.6.3 is released. Learn to use font-awesome version 4.6.3 in Maven based Java projects
this version ( org.webjars - font-awesome version 4.6.3 ) in their Java project...; org.webjars - font-awesome version 4.6.3 java library in your project. Now you... dependency of org.webjars - font-awesome version 4.6.3 in your pom.xml file you
Maven dependency for org.webjars - font-awesome version 5.6.1 is released. Learn to use font-awesome version 5.6.1 in Maven based Java projects
this version ( org.webjars - font-awesome version 5.6.1 ) in their Java project...; org.webjars - font-awesome version 5.6.1 java library in your project. Now you... dependency of org.webjars - font-awesome version 5.6.1 in your pom.xml file you
Maven dependency for org.webjars - font-awesome version 5.15.4 is released. Learn to use font-awesome version 5.15.4 in Maven based Java projects
this version ( org.webjars - font-awesome version 5.15.4 ) in their Java project... and includes  org.webjars - font-awesome version 5.15.4 java library in your project...Maven dependency for  org.webjars  - Version 5.15.4 of font-awesome
How to display the font in center
How to display the font in center   ... the font in center. To display the font in center, the method paint() is defined. The Font class defines the font 'Monotype 'Corsiva'. The class
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-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-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-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 '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-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-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

Ads