Page 1 of 1

Losing styles when converting from docx to xhtml and back

PostPosted: Fri Nov 29, 2013 4:52 pm
by boiler5
Hello. I am trying to convert my document to xhtml and back. On some step all styles are mangled.

Here is the code for converting from xhtml to docx
Code: Select all
    public static void html2docx1(InputStream is, OutputStream os) {
   try {
       WordprocessingMLPackage wmlp = WordprocessingMLPackage
          .createPackage();
       XHTMLImporter x = new XHTMLImporterImpl(wmlp);
       wmlp.getMainDocumentPart().getContent()
          .addAll(x.convert(is, null));
       File f = File.createTempFile("doc", "docx");
       wmlp.save(f);
       FileInputStream fis = new FileInputStream(f);
       int n = 0;
       byte buff[] = new byte[1024];
       while (n >= 0) {
      n = fis.read(buff);
      if (n > 0)
          os.write(buff, 0, n);
       }
       fis.close();
       f.delete();
   } catch (Exception e) {

   }
    }


And here is the code for converting from docx to xhtml
Code: Select all
    public static void docx2html1(InputStream is, OutputStream os)
       throws Exception {
   WordprocessingMLPackage wmlp = Docx4J.load(is);
   Docx4J.toHTML(wmlp, null, null, os);
    }


There are dociuments I ve attached. Please help. What am I doing wrong?

Re: Losing styles when converting from docx to xhtml and bac

PostPosted: Thu Dec 05, 2013 8:54 pm
by jason
Try using your original docx (or at least its styles part) as a template (wmlp) for the target of the XHTML import.

That way, if @class in the XHTML matches a style defined in the docx, then that style will be used.