You have 2 basic options:
1. apply pre-existing Word styles (sounds like this is what you want to do?)
2. rely on the XHTML only
Do you have control over the format of the incoming XHTML?
Treating "2. rely on the XHTML only" first :-
Try setting @list-style-type on the relevant ul|ol; see further See further
https://developer.mozilla.org/en-US/doc ... style-typeThis triggers getLvlTextFromCSSListStyleType or getNumberFormatFromCSSListStyleType at
https://github.com/plutext/docx4j-Impor ... .java#L243
Using java Syntax Highlighting
private String getLvlTextFromCSSListStyleType
(String listStyleType,
int level
) {
if ( listStyleType.
equals("disc")) {
return "";
}
if ( listStyleType.
equals("circle")) {
return "o";
}
if ( listStyleType.
equals("square")) {
return "";
}
return "%"+level
+".";
}
Parsed in 0.015 seconds, using
GeSHi 1.0.8.4
There is scope to enhance this method to support other options.
Turning now to "1. apply pre-existing Word styles ":-
At
https://github.com/plutext/docx4j-Impor ... java#L1276
Using java Syntax Highlighting
} else if (isListItem
(blockBox.
getElement())
/* <li class="ListParagraph Normal DocDefaults " style="display: list-item;text-align: justify;"><span style="">Level 1</span></li>
* <li>
<p>Item 2</p>
DON"T TRIGGER THIS LINE
</li>
*/
&& !(blockBox
instanceof com.
openhtmltopdf.
render.
AnonymousBlockBox)) {
// Paragraph level styling
//P currentP = this.getCurrentParagraph(true);
// You'll get an NPE here if you have li which isn't in ol|ul
listHelper.
peekListItemStateStack().
init();
PPr pPr
= Context.
getWmlObjectFactory().
createPPr();
this.
getCurrentParagraph(true).
setPPr(pPr
);
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.
int pos
= cssClass.
indexOf(" ");
if (pos
>-1
) {
cssClass
= cssClass.
substring(0, pos
);
}
// if the docx contains this stylename, set it
Style s
= this.
stylesByID.
get(cssClass
);
if (s
==null) {
log.
debug("No docx style for @class='" + cssClass
+ "'");
if (paragraphFormatting.
equals(FormattingOption.
CLASS_PLUS_OTHER)) {
listHelper.
addNumbering(this.
getCurrentParagraph(true), blockBox.
getElement(), cssMap
);
addParagraphProperties
(pPr, blockBox, cssMap
);
}
// else, can't number
} else if (s.
getType()!=null && s.
getType().
equals("numbering")) {
log.
debug("Using list style from @class='" + cssClass
+ "'");
/* it should contain something like:
*
* <w:pPr>
<w:numPr>
<w:numId w:val="1"/>
</w:numPr>
</w:pPr>
*
* Use this...
*
* We don't actually set the numbering style on the
* paragraph, because numbering styles aren't used that way
* in Word.
*/
BigInteger numId
= s.
getPPr().
getNumPr().
getNumId().
getVal();
listHelper.
setNumbering(pPr, numId
);
// TODO: @start is ignored in this case (it is handled in addNumbering)
// Note that we just use the numbering it points to;
// we don't follow it to its abstract num (which is in fact
// where the w:styleLink matching our @class should be found).
// TODO: if this list is being used a second time, we should
// restart numbering?? Is it restarted in the HTML?
// OK, we've applied @class
if (paragraphFormatting.
equals(FormattingOption.
CLASS_PLUS_OTHER)) {
// now apply ad hoc formatting
addParagraphProperties
(pPr, blockBox, cssMap
);
}
} else {
log.
debug("For docx style for @class='" + cssClass
+ "', but its not a numbering style ");
if (paragraphFormatting.
equals(FormattingOption.
CLASS_PLUS_OTHER)) {
listHelper.
addNumbering(this.
getCurrentParagraph(true), blockBox.
getElement(), cssMap
);
// SPECIAL CASE
if (Docx4jProperties.
getProperty("docx4j.model.datastorage.BindingTraverser.XHTML.Block.rStyle.Adopt",
false)
&& s.
getType()!=null && s.
getType().
equals("paragraph")) {
log.
debug(".. using " + s.
getStyleId() );
PStyle pStyle
= Context.
getWmlObjectFactory().
createPPrBasePStyle();
pStyle.
setVal(s.
getStyleId());
this.
getCurrentParagraph(false).
getPPr().
setPStyle(pStyle
);
}
addParagraphProperties
(pPr, blockBox, cssMap
);
}
}
}
} else {
// No @class
if (paragraphFormatting.
equals(FormattingOption.
CLASS_PLUS_OTHER)) {
addParagraphProperties
(pPr, blockBox, cssMap
);
}
// else its CLASS_TO_STYLE_ONLY,
// but since we have no @class, do nothing
}
}
Parsed in 0.026 seconds, using
GeSHi 1.0.8.4
So to re-use an existing Word style on a list-item, you need:
1. to trigger isListItem, which using <li> does
2. to set paragraphFormatting to CLASS_TO_STYLE_ONLY or CLASS_PLUS_OTHER prior to starting the importer
3. set an appropriate li/@class in your XHTML <----------------------