Forums Register Login

Inserting table and image into a doc file using hwpf(poi)

+Pie Number of slices to send: Send
I want to do an application in which I want to insert table and an image into msword 2003 doc file . If any one know please help me with a sample code.
+Pie Number of slices to send: Send
Welcome to JavaRanch.

There's a library called Apache POI that can work with Word documents (you can find a simple example of how to create a DOC file using it at http://faq.javaranch.com/java/CreateWordDocument), but I think it will be a frustrating experience to use it for changes to complex documents (and I'm not even sure that it would ultimately be successful).

Are you creating a new file, or do you want to modify an existing file? If the former, you might have better luck creating an RTF file - which is a much simpler file format to generate, and which also supports tables and images.
+Pie Number of slices to send: Send
I am already using this Apache POI and I am going to try to modify the existing doc file with the insertion of table and image .
When I inserted a table into doc file with following code for insertion its inserting but with out borders so it is not visible doc file.If you solved this problem I am greatly thanks to you.Thanku in advance

File f=new File("/home/harish/Desktop/TextModify/test.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
HWPFDocument hwpf=new HWPFDocument(fs);
TableProperties tp=new TableProperties(4);
BorderCode br=new BorderCode();
tp.setBrcBottom(br);
tp.setBrcLeft(br);
tp.setBrcRight(br);
tp.setBrcTop(br);
tp.setDyaRowHeight(5);
tp.setFTableHeader(true);
tp.setDxaGapHalf(3);
tp.setTlp(10);
hwpf.getRange().getSection(0).getParagraph(1).insertBefore(tp,4);
+Pie Number of slices to send: Send
Moving to a more appropriate forum
+Pie Number of slices to send: Send
It's neither free nor open source, but there's a really awesome product called Aspose that allows complete manipulation of the MS Word object model. I've used it a fair amount, and it's pretty impressive.

http://www.aspose.com/categories/java-components/aspose.words-for-java/default.aspx
+Pie Number of slices to send: Send
Thank you for replying whether Aspose which you told is an open source. If it is opensource i am pretty happy .Because I am doing an application in which complex doc operation should be done.thanks in advance
+Pie Number of slices to send: Send
Do you know any other alternative for the problem(Inserting table and image into a doc file using hwpf(poi)) ie jar file
+Pie Number of slices to send: Send
The http://faq.javaranch.com/java/AccessingFileFormats page links to WordApi.exe. I'm not sure if it does what you're asking, but it seems worth checking out.

Is the RTF approach I outlined in my first post not feasible?
+Pie Number of slices to send: Send
How can we generate rtf file I didn't know much more about RTF If you have any method please provide me.
Thanks in advance
+Pie Number of slices to send: Send
I'd advise to create a Word document that looks like the one you want to create, and then save it as RTF. Then you can study the RTF that gets created (which is text, and not too hard to understand). Now you can either a) create an entire RTF file from scratch that contains your content, or b) use an existing RTF file as template which contains placeholders for the content you want to add (something like "$image$" or "$table$"); these can then be replaced with the actual content using string operations.
+Pie Number of slices to send: Send
Will you please provide aspose api .Please provide me the api .Thanks for reply
thanks in advance
+Pie Number of slices to send: Send
What do you mean, "provide"? The API documentation is available on the Aspose web site, along with examples and other stuff. What else are you looking for?

It seems that you are looking for an easy solution in which you need to invest neither effort nor money; such a solution does not exist.
+Pie Number of slices to send: Send
means they are asking money to download jar file
+Pie Number of slices to send: Send
Of course they do; it's commercial software. You're not proposing to pirate it, are you?
+Pie Number of slices to send: Send
not like that
+Pie Number of slices to send: Send
 

harish Rudra wrote:Thank you for replying whether Aspose which you told is an open source. If it is opensource i am pretty happy .Because I am doing an application in which complex doc operation should be done.thanks in advance



No, Aspose is not an open source technology. In fact, from some stack traces that I've seen they employ an obfuscator to protect their product -- a logical decision if you ask me!

They do allow a free trial where Aspose simply applies a watermark to all generated documents indicating it is unlicensed.

We did a lot of searching and quite frankly this is the best solution available -- but it has its price. If you're working on an open source (i.e. not for profit) project, this is probably not the solution for you.

As Ulf pointed out, working with RTF may be an option for you -- but realize that the RTF specification is... extensive. You can download the entire spec here: http://tinyurl.com/yhsgqb5
+Pie Number of slices to send: Send
 

harish Rudra wrote:I am already using this Apache POI and I am going to try to modify the existing doc file with the insertion of table and image .
When I inserted a table into doc file with following code for insertion its inserting but with out borders so it is not visible doc file.If you solved this problem I am greatly thanks to you.Thanku in advance

File f=new File("/home/harish/Desktop/TextModify/test.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
HWPFDocument hwpf=new HWPFDocument(fs);
TableProperties tp=new TableProperties(4);
BorderCode br=new BorderCode();
tp.setBrcBottom(br);
tp.setBrcLeft(br);
tp.setBrcRight(br);
tp.setBrcTop(br);
tp.setDyaRowHeight(5);
tp.setFTableHeader(true);
tp.setDxaGapHalf(3);
tp.setTlp(10);
hwpf.getRange().getSection(0).getParagraph(1).insertBefore(tp,4);



hi harish rudra, i need the java code for copying a png image and pasting it in ms word document. It would be really helpful for me if you give me the code for it. thanks in advance.
+Pie Number of slices to send: Send
Apart from what I said before about using RTF: POI does not support adding images to .doc files (using the HWPF classes), it only supports it for .docx files (using the XWPF classes). This should get you started: http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleImages.java
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 18706 times.
Similar Threads
HWPF POI
HWPF in POI
HWPF in POI
HWPF Mail Merging
Problem regarding insertion of Buttons into doc using POI from Apache HWPF
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:59:02.