Page 1 of 1

Please help me in adding HTML Content to docx File

PostPosted: Wed Jun 22, 2011 3:51 am
by suncity65
Hi

Please help me or share me the code which i can use to add HTML content example
<html><body><h1>Test1</h1><br/><p>TestTest</p></body></html> to a docx file.

Thanks!

Re: Please help me in adding HTML Content to docx File

PostPosted: Wed Jun 22, 2011 4:30 am
by suncity65
Hi all

I checked sample CreateWordprocessingMLDocument.java in which i find

Code: Select all
// Add an altChunk
       // .. the part
       String html = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>";
       AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html") );
       afiPart.setBinaryData(html.getBytes());
       afiPart.setContentType(new ContentType("text/html"));
       Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
       // .. the bit in document body
       CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
       ac.setId(altChunkRel.getId() );
       wordMLPackage.getMainDocumentPart().addObject(ac);

       // .. content type
       wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
      
      //injectDocPropsCustomPart(wordMLPackage);
      


I feel the document is created but i am not able to open the file it says "There was an error opening file".
Can some one please help me..

Re: Please help me in adding HTML Content to docx File

PostPosted: Wed Jun 22, 2011 1:51 pm
by jason
It should work.

Did you inspect the resulting docx to check that the AlternativeFormatInputPart contained your expected content, and that document.xml correctly references it?

Have you tried stripping out everything irrelevant, to produce a minimal test case?

Re: Please help me in adding HTML Content to docx File

PostPosted: Wed Jun 22, 2011 3:17 pm
by suncity65
Yeah I tried it is working now, but there is one more issue or concern what i have is that after adding the HTML content as altchunk, if i try to compare it with other docx created by docx4j again having HTML content as altchunk.
The com parer says

NOT IMPLEMENTED: support for w:altChunk -

Can you please help me how i can actually compare the 2 docx files which are created by contents from a rich text editor such as NiceEdit.js

Re: Please help me in adding HTML Content to docx File

PostPosted: Wed Jun 22, 2011 5:17 pm
by jason
Presumably that message is coming from one of the XSLT found in the org.docx4j.diff package

You are getting it because docx4j does not actually convert the HTML into WordML; this is left for Word to do when the document is opened.

There is some code in docx4j to help with converting html to WordML if you need to do that - see earlier posts.