• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
harish Rudra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to a more appropriate forum
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
harish Rudra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will you please provide aspose api .Please provide me the api .Thanks for reply
thanks in advance
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
means they are asking money to download jar file
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course they do; it's commercial software. You're not proposing to pirate it, are you?
 
harish Rudra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not like that
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic