His actual requirement is inserting images in M.S Word File.
Frankly i even hardly had any chance to achieve this till now.But always have trust for Apache POI.Since POI is still in initial stages of implementation so search made to look for other libary Api's.
Found out that Microsoft supports this by providing a schema and as a developer we need to provide a valid WordProcessingML document.
Some Sample Examples and More about WordProcessingML can be found in Samples
But unfortunately this didnot solve our requirement as we need to insert images in MS Word file and WordProcessingML is not supporting this feature.Tried hard to get it working and left the option to use WordProcessingML.
Then luckly read a Blog article and found iText libary helps to solve our requirement.
This link provides some basic information about iText-RTF library
http://itextdocs.lowagie.com/tutorial/rtf/index.php
Below Steps describe basic steps involed in achieving this.
a) Create an iText Document object.Doument here can be of any type.It can be PDFDocument or a custom subclass of Document Class.
Here in our example it is fine to create a normal Document object class.
b) Create an FileOutputStream object i.e file the destination .doc file.
c) Next create an object of RtfWriter2 class and pass document and outputstream object as parameters.
d) Open the document
e) Create an iText Image object which should ideally be sitting in .rtf file
f) Add the image object to the document object.
g) Finally close the document object.
Below is the code sample:
private String storeContentsInWord(String imagePath) {
File helloWorldRTF=null;
try {
Document document=new Document();
helloWorldRTF=new File("HelloWorld.rtf");
boolean isExists=helloWorldRTF.exists();
if(!isExists) {
helloWorldRTF.createNewFile();
}
FileOutputStream fileOutput=new FileOutputStream(helloWorldRTF);
RtfWriter2.getInstance(document,fileOutput);
document.open();
document.add(new Jpeg(new URL(imagePath)));
document.close();
}catch(Exception ex) {
ex.printStackTrace();
}
return helloWorldRTF.getAbsolutePath();
}
As Geeks often suggest no use of using api's without understanding the working version of the api.Hence tried to peep through code and here are few points to be noted.
a) Seeing code it is clear that iText uses observer pattern heavily.For example each iText document created can have an associate listener.For example PDFWriter or HTMLWriter can be associated as listeners to the Document object which provides many utility methods for example we can write,add elements to any OutputStream specified.
b) So Coming to our code first we created a basic Document class object.
c) Next we used basic IO code to create a new FileOutputStream object.
c) Next we created an instance of RtfWriter2 which allows the creation of rtf documents via the iText system and associated our document object and FileOutputStream object to it.Internally it will associate an RtfWriter2 as a listener to document object and create an instance of RTFDocument object which stores all document related data.Note: As suggested we may not use this class directly.May be making it inner class should have solved this issue?
d) When document.open() is called it tries to iterate all the listeners in our case RtfWriter2 and set the pageSize,margins with default values and calls open method corresponding to the listener.It internally calls the open method of RTFWriter2 class.Open method in RTFWriter2 class will set the "open" flag to true indicating document is open and then calls the associated RTFDocument object open() method which initializes data buffer object for storing actual data.For more info about data object please refer to RtfDataCache class in api.
e) Next we created a iText Element object called JPEG and passed a reference to the add() method of Document class.Internally it again calls the associated add() method in this case it calls RTFWriter2 add() method.
f) add() method in RTFWriter2 tries to create an instance of RTFImage object as in our case we are trying to store an image into word file.It tries to create a specific element object depending on the element type associated.Here in this case it is JPEF.Other types supported are RtfNewPage,RtfPhrase,RtfParagraph,RtfAnchor,RtfImage,RtfTable etc.Please refer to RTFMapper class in api for more details.When new RTFMapper object is created it will try to retrieve necessary attached image information like image contents,width,height etc.Then RTFWriter2 tries to call add() method in the associated RTFDocument object which tries to add content to the corresponding output stream.
g) Finally we are calling close() method of Document class to try to close the document where finally the data stored in the buffer will be retrieved and saved in the corresponding Output Stream and thus associates again a new RTFDocument object.
Well,I think it is very important for everyone to understand what we are doing and i think it is not really sufficient to know how to call API.We surely need to know the internal workings of API which will help to improve our knowledge more.Today i came to know further how good api design can matter afterall!
Please suggest if any.
8 comments:
Prashant, this is a good and simple solution that help in digging more into iText API . thanks for your R & D - Kasi
Kasi,
Thanks a lot for your encouraging comments.
Thanks
Prashant Jalasutram
Hi Prashanth,
This is nagesh, i have some doubts to ask (related to compny ur working for , how u came there , are you on HSMP? etc ) , not technical. It will be very help full if you mail me ur id ...
Iam eagarly waiting for your response
Pls mail me your ID
to nag.esh@hotmail.com
Nagesh
Hi,
Is there a way with which i can add an image to a file at a specified location.
If anyone has any prior experience of this kindly respond.
Thanks
This is a really good post and really good explanation.
I was having an issue in putting an image/text at a particular location, like after a particular word.
If you have come across a similar situation, please post some information.
Thanks
Ashish Joy
Hi
Prasanth,
I want full example for creating a word document using java with poi can u help me out?
and i want where u r working?
if u send me ur mail id its good enough.
bye
sirisha
Hey there
great post. got me started with itext. but i am facing a problem. I need to add/remove text from existing .doc/.rtf files. Is this possible?
Hi prashanth,
I need to create a MS word document by adding images into it.
Please help me
Thanks
Teja
Post a Comment