Page 1 of 1

Docx4j bind and xhtml content

PostPosted: Thu Feb 18, 2016 1:06 pm
by Danmir
Hello!

First, I would like to thank you for this magnificent library, it really solves a lot of problems.

I have a doubt about xml binding. I can bind normal content, and it works well, the problem I have is binding xhtml. Supposedly, it should convert any escaped tags into WordML format, but I keep seeing the tags in the output document.

I've referred to this post http://www.docx4java.org/forums/data-binding-java-f16/how-to-set-the-od-xpath-t1340.html , and I get the correct document, but I can seem to get a correct result.

It didn't changed the html content, as if it was not binded. I managed to change it by adding
Code: Select all
<w:dataBinding w:xpath="/document[1]/data[1]/htmlProperty[1]" w:storeItemID="{C44A086F-0ABF-48CC-ACB0-932E9DBB4B2F}"/>

Here's the original file:

html.docx
Original
(22.86 KiB) Downloaded 451 times


And here's the resulting file:

result.docx
Resultado
(19.55 KiB) Downloaded 395 times


Here's my java code:

Code: Select all
ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            String xml = "<document><data><htmlProperty>&lt;p&gt;it shoud change&lt;/p&gt;</htmlProperty>"
                    + "</data><today>18 Feb 2016</today><currentTime>09:37</currentTime></document>";
           
            WordprocessingMLPackage wordMLPackage = Docx4J.load(file.getInputstream());
            Docx4J.bind(wordMLPackage, xml, Docx4J.FLAG_BIND_INSERT_XML);
            OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
            wordMLPackage = odh.preprocess();
            Docx4J.save(wordMLPackage, bos,  Docx4J.FLAG_BIND_INSERT_XML);
            bos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }


Any help is appreciated, I'm sure it's something silly I'm not seeing :oops:

I forgot, I'm using docx4j-ImportXHTML 3.2.2

Re: Docx4j bind and xhtml content

PostPosted: Sat Feb 20, 2016 5:17 am
by Danmir
I finally made it work, You have to set FLAGS_NONE or Docx4J.FLAG_BIND_INSERT_XML & Docx4J.FLAG_BIND_REMOVE_SDT.

Code: Select all
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML & Docx4J.FLAG_BIND_REMOVE_SDT);


It was a problem with some library conflicting, I still can't make it work on the main project, but I created a new one and it works fine.

Now, a new question has arised. ¿Can the font style and size be kept according to what's defined in word? it always changes the font to times new roman and size to 11, and I would like to keep the document original format, I just want to use <i> and <b>

Thanks

Re: Docx4j bind and xhtml content

PostPosted: Sat Feb 20, 2016 6:55 am
by Danmir
Finally got it!!

To use the style that comes in the document, you have to use <span>, it will change acordding to what was in that section before! the onlye thing that remained was the font, so I defined it in the style, and now my XML looks like this:

Code: Select all
<htmlProperty>&lt;span style="font-family:Calibri"&gt;&lt;i&gt;Coffee&lt;/i&gt;&lt;/span&gt;</htmlProperty>


Now I only have to resolve the library issue, and will be done, but I will do another post for that.

So, in resume:
  • Use FLAG NONE
  • In your XML use span to let the control acquire the style surrounding it
  • Use style="font-family:XXXFONT" to change it (Remember it must be on your docx already

Hope this helps!!!

Re: Docx4j bind and xhtml content

PostPosted: Mon Feb 29, 2016 11:46 am
by jason
Note that there are various ways to fine tune the behaviour, including:

Code: Select all
    /**
    * Map a font family, for example "Century Gothic" in:
    *
    *    font-family:"Century Gothic", Helvetica, Arial, sans-serif;
    *
    * to a w:rFonts object, for example:
    *
    *    <w:rFonts w:ascii="Arial Black" w:hAnsi="Arial Black"/>
    *
    * Assuming style font-family:"Century Gothic", Helvetica, Arial, sans-serif;
    * the first font family for which there is a mapping is the one
    * which will be used.
    *
    * xhtml-renderer's CSSName defaults font-family: serif
    *
    * It is your responsibility to ensure a suitable font is available
    * on the target system (or embedded in the docx package).  If we
    * (eventually) support CSS @font-face, docx4j could do that
    * for you (at least for font formats we can convert to something
    * embeddable).
    *
    * You should set these up once, for all your subsequent
    * imports, since some stuff is cached and currently won't get updated
    * if you add fonts later.
    *
    * @since 3.0
    */
   public static void addFontMapping(String cssFontFamily, RFonts rFonts) {
      FontHandler.addFontMapping(cssFontFamily, rFonts);
   }


and for paragraphs, runs and tables:

Code: Select all
/**
* CLASS_TO_STYLE_ONLY: a Word style matching a class attribute will
* be used, and nothing else
*
* CLASS_PLUS_OTHER: a Word style matching a class attribute will
* be used; other css will be translated to direct formatting
*
* IGNORE_CLASS: css will be translated to direct formatting
*
*/
public enum FormattingOption {

   CLASS_TO_STYLE_ONLY, CLASS_PLUS_OTHER, IGNORE_CLASS;
}