0

I have been trying to convert html content to docx using their library and I do get a docx file created after running my app but it has blank content whereas the html does have some content in it. Please check the code below and I have included all necessary libraries from AndroidDocxtoHTML example on git.

Code:

// HTML Code
String html = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>";

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
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");

I do not understand what's missing in the code that I get blank document. I found this code for java which i modified for android. Some people suggested using nightly build jar for xhtml conversions. Do I need to use that?

Abhishek Singh
  • 415
  • 1
  • 12
  • 24

1 Answers1

0

AlternativeFormatInputPart doesn't actually convert your HTML to normal docx content.

It is up to the application displaying the docx to do that (and most can't).

Instead, consider using docx4j-ImportXHTML to do the conversion.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • But can that convert html back to docx or does it convert XHTML to docx? – Abhishek Singh Jul 06 '15 at 12:09
  • You'll need to make your html into well formed XHTML. And you may or may not have issues to overcome to get it running on Android. – JasonPlutext Jul 06 '15 at 22:03
  • Hi I tried the code on https://github.com/plutext/docx4j-ImportXHTML/blob/master/src/samples/java/org/docx4j/samples/XhtmlToDocxAndBack.java but unfortunately I am getting an error that xhtml importer cannot be accessed as it uses a deprecated API. – Abhishek Singh Jul 17 '15 at 23:22
  • That issue is fixed and was because of other reasons. error: cannot access XHTMLImporter class file for org.docx4j.convert.in.xhtml.XHTMLImporter not found is another error i am getting I added xhtmlrenderer and docx4jimportxhtml jar files – Abhishek Singh Jul 17 '15 at 23:35
  • @JasonPlustext It would be great of you if you could answer my queries regarding docx4j at http://stackoverflow.com/questions/31548563/android-html-to-docx-docx4j-issues – Abhishek Singh Jul 21 '15 at 20:24