Page 1 of 1

relationship between lists and FormattingOptions

PostPosted: Wed Apr 16, 2014 6:40 pm
by supachamp
Hi,

I use the following options to handle the CSS -> Word style mapping during the XHTML import

Code: Select all
xHTMLImporter.setTableFormatting(FormattingOption.CLASS_PLUS_OTHER);
xHTMLImporter.setParagraphFormatting(FormattingOption.CLASS_TO_STYLE_ONLY);
xHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);


But if I use these options, any type of list within the HTML which I pass to the XHTMLImporter is not shown in the Word document anymore. What can I do about it. I tried to define a style for my lists, but that does not work either. For example:

Code: Select all
<ul class="UnorderedList">
<li>text</li>
</ul>


The Word template document which I load using

Code: Select all
this.wordMLPackage = WordprocessingMLPackage.load(new File(DocumentExporter.TEMPLATE_WORD_DOCUMENT));


contains the class "UnorderedList" though.

How can I have Word show the lists with the formatting options which I define above?

Thanks a lot for your help!!!

Best regards.

Re: relationship between lists and FormattingOptions

PostPosted: Wed Apr 30, 2014 7:37 pm
by supachamp
Can someone please help me out on this issue?

Thanks a lot!

Re: relationship between lists and FormattingOptions

PostPosted: Mon May 05, 2014 1:22 pm
by jason
From the current source code for XHTMLImporterImpl

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                .. if (isListItem(blockBox.getElement())) {

                        :
                       
                        if (paragraphFormatting.equals(FormattingOption.IGNORE_CLASS)) {
                               
                                listHelper.addNumbering(this.getCurrentParagraph(true), blockBox.getElement(), cssMap);                        
                                addParagraphProperties(pPr, blockBox, cssMap );
                               
                        } else {
                                // CLASS_TO_STYLE_ONLY or CLASS_PLUS_OTHER
                                if (listHelper.peekListStack().getElement()!=null
                                                && listHelper.peekListStack().getElement().getAttribute("class")!=null) {

                                        // NB Currently, you need to put this @class on the ol|ul at each level of nesting,
                                        // if you want to use the list style.
                                        // If you only put it on some levels, well, new list(s) will be created for the others,
                                        // with imperfect results...
                                       
                                        String cssClass = listHelper.peekListStack().getElement().getAttribute("class").trim();
                                        log.debug(cssClass);
                                        if (cssClass.equals("")) {
                                                // What to do? same thing as if no @class specified
                                                if (paragraphFormatting.equals(FormattingOption.CLASS_PLUS_OTHER)) {
                                                listHelper.addNumbering(this.getCurrentParagraph(true), blockBox.getElement(), cssMap);
                                                        addParagraphProperties(pPr, blockBox, cssMap );
                                                }
                                                // else its CLASS_TO_STYLE_ONLY,
                                                // but since we have no @class, do nothing
                                               
                                        } else {
                                                // Usual case...
                                               
                                                // Our XHTML export gives a space separated list of class names,
                                                // reflecting the style hierarchy.  Here, we just want the first one.
                                                // TODO, replace this with a configurable stylenamehandler.

 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


You can find the above at line 959 at https://github.com/plutext/docx4j-Impor ... rImpl.java

Re: relationship between lists and FormattingOptions

PostPosted: Wed Jun 18, 2014 10:50 pm
by supachamp
I do not get it to work... I have defined a Word style "unorderedList" for the list and used an equal name "unorderedList" for the class of the unordered list in the html <ul class="unorderedList">. This does not work. Then I tried to derive "unorderedList" in Word from the predefined list style. This does not work either.

Is it possible for you to provide me an example Word template file and html to learn how an html list is correctly converting into a list in Word?!

Thank you very much!!!

Best regards.

Re: relationship between lists and FormattingOptions

PostPosted: Wed Jul 09, 2014 1:11 pm
by jason
You need to make sure the Word style you are creating is a "list" style, not a "paragraph" style.

The easiest way to do this is to click the down arrow next to "Multilevel list" on the ribbon, then choose "Define new list style"

Your styles.xml should then contain a style of type "numbering", for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:style w:type="numbering" w:customStyle="1" w:styleId="UnorderedList">
    <w:name w:val="UnorderedList"/>
    <w:uiPriority w:val="99"/>
    <w:rsid w:val="005859A9"/>
    <w:pPr>
      <w:numPr>
        <w:numId w:val="2"/>
      </w:numPr>
    </w:pPr>
  </w:style>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


and of course a corresponding entry in the numbering part.

Then, assuming your docx is called ListStyle, the following works:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    public static void main(String[] args) throws Exception {
       
        String xhtml = "<ul class=\"UnorderedList\">"+
        "  <li> Outer 1 </li>"+
         " <li> Outer 2 </li>"+
         " <li> Outer 3 </li>"+
        "</ul>";
       
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(System.getProperty("user.dir") + "/ListStyle.docx"));

        XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
               
                wordMLPackage.getMainDocumentPart().getContent().addAll(
                                XHTMLImporter.convert( xhtml, null) );
       
        wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_from_XHTML.docx") );
       
  }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: relationship between lists and FormattingOptions

PostPosted: Wed Jul 09, 2014 9:11 pm
by supachamp
I added the format style "UnorderedList" to my Word document as a "numbering" style. In the styles.xml it is listed as

Code: Select all
<w:style w:type="numbering" w:customStyle="1" w:styleId="UnorderedList">
   <w:name w:val="UnorderedList"/>
   <w:uiPriority w:val="99"/>
   <w:rsid w:val="008361AA"/>
   <w:pPr>
      <w:numPr>
         <w:numId w:val="19"/>
      </w:numPr>
   </w:pPr>
</w:style>


However, when I generate the Word document, the debug message shows

Code: Select all
For docx style for @class='UnorderedList', but its not a character style


In the generated document, the list declared via
Code: Select all
<ul class="UnorderedList">...</ul>
is not a Word list.

I further use the following settings:

Code: Select all
xHTMLImporter.setTableFormatting(FormattingOption.CLASS_PLUS_OTHER);
xHTMLImporter.setParagraphFormatting(FormattingOption.CLASS_TO_STYLE_ONLY);
xHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);