Page 1 of 1

how to embed image with html <img tag.

PostPosted: Wed Jul 16, 2014 9:34 am
by subnot303640
I am creating a word document using doc4j api. I am constructing a html message with html table and img tags in it and saving the document. The html string has an image tag in it like this
<img src="c:\\smiley.jpg" alt="Smiley face" height="42" width="42">.
Upon opening the document, the image appears fine. However, when I open the same file in other machine, I don't see the image, since the image is in c drive in a different machine.
I tried giving relative path, but it didn't work. How to solve this problem.

Thanks.

Re: how to embed image with html <img tag.

PostPosted: Wed Jul 16, 2014 8:28 pm
by jason
What code are you using to add the html?

Re: how to embed image with html <img tag.

PostPosted: Thu Jul 17, 2014 1:54 am
by subnot303640
I am using addAltChunk aPI. I have a huge html string, but just simplified for this case.

Code: Select all
try {
String html = "<html><head></head><body><div><img src='c:\\smiley.jpg' alt='Smiley face' height='42' width='42'/></div></body></html>";

      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Html, html.getBytes());
      File file = new File("c://mydocument.docx");
      wordMLPackage.save(file);


}catch(Exception e) {
e.printStackTrace();
}

Re: how to embed image with html <img tag.

PostPosted: Thu Jul 17, 2014 7:47 am
by jason
That code relies on Word to convert the altChunk to real/native docx content.

If you have docx4j do the conversion, your issue should go away.

You can either replace addAltChunk with direct invokations of docx4j-ImportXHTML, or call convertAltChunks (which does the same thing):-

https://github.com/plutext/docx4j/blob/ ... kHost.java