Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Other Open Source Projects and the fly likes Inserting table and image  into a doc file using hwpf(poi) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Inserting table and image  into a doc file using hwpf(poi)" Watch "Inserting table and image  into a doc file using hwpf(poi)" New topic
Author

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

harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.


Ping & DNS - my free Android networking tools app
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
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);
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Moving to a more appropriate forum


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
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
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
Do you know any other alternative for the problem(Inserting table and image into a doc file using hwpf(poi)) ie jar file
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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?
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
Will you please provide aspose api .Please provide me the api .Thanks for reply
thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
means they are asking money to download jar file
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Of course they do; it's commercial software. You're not proposing to pirate it, are you?
harish Rudra
Greenhorn

Joined: Oct 14, 2009
Posts: 9
not like that
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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
siva Tamil
Greenhorn

Joined: Nov 18, 2013
Posts: 3
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting table and image into a doc file using hwpf(poi)