Page 1 of 1

Can't Preserve Space When Convert Docx To XSL-FO

PostPosted: Tue Apr 28, 2015 10:34 am
by qq955166
Hi Guys,
I am using docx4j-3.2.1.jar.

In my test word file, I can see

Code: Select all
<w:p><w:r><w:t xml:space="preserve">1         Example 3a (bold)         1</w:t></w:r></w:p>


however, in my xsl-fo file,

Code: Select all
<block font-size="10.5pt" line-height="100%" space-after="0in" space-before="0in" text-align="justify"><inline><inline font-family="Times New Roman">1         Example 3a (bold)         1</inline></inline></block>


The attribute white-space-treatment="preserve" is not produced. and therefore, my pdf output does not have white space neither.

Does anyone know how to deal with this?

Thank you!

Re: Can't Preserve Space When Convert Docx To XSL-FO

PostPosted: Thu Apr 30, 2015 9:37 pm
by jason
Please note the w:t attribute doesn't quite do what I think you think it does; see http://webapp.docx4java.org/OnlineDemo/ ... dML/t.html

That said, a glance at the code does suggest there is still an issue here.

Assuming you're using XSLT approach, this is related to:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <xsl:template match="w:t[parent::w:r]">
       
                <xsl:variable name="pPrNode" select="../../w:pPr" />   
                <xsl:variable name="rPrNode" select="../w:rPr" />      
                <xsl:variable name="textNode" select="." />    
       
                <xsl:copy-of select="java:org.docx4j.convert.out.common.XsltCommonFunctions.fontSelector(
                                $conversionContext, $pPrNode, $rPrNode, $textNode)" />

                               
        </xsl:template>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Ultimately, it hits:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    public Object fontSelector(PPr pPr, RPr rPr, Text wmlText) {

        String text=null;
        if (wmlText==null) {
                log.debug("Null Text object");
        } else {
                text = wmlText.getValue();
                spacePreserve = (wmlText.getSpace()!=null) && (wmlText.getSpace().equals("preserve"));
        }
       
        return fontSelector( pPr,  rPr,  text);
    }
 
Parsed in 0.026 seconds, using GeSHi 1.0.8.4


and what probably needs to be changed is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    public void setAttribute(Element el, String fontName) {
       
        // could a document fragment contain just a #text node?
       
                if (outputType== RunFontActionType.DISCOVERY) {
                        return;
                } else if (outputType==RunFontActionType.XHTML) {
                if (spacePreserve) {
                /*
                 *      Convert @xml:space='preserve' to style="white-space:pre-wrap;"
                                which is good for FF3, and WebKit; not honoured by IE7 though.
                 */

                        el.setAttribute("style", getCssProperty(fontName) + "white-space:pre-wrap;");
                       
                } else {
                        el.setAttribute("style", getCssProperty(fontName));
                }
        } else if (outputType==RunFontActionType.XSL_FO) {
                String val = getPhysicalFont(fontName);
                if (val==null) {
                        // Avoid @font-family="", which FOP doesn't like
                        el.setAttribute("font-family", fallbackFont );
                } else {       
                        el.setAttribute("font-family", val );
                }
               
                        // Any reason not to always do this?
//                      el.setAttribute( "white-space-collapse", "false");
//                      el.setAttribute( "white-space", "pre");
               
        }
    }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: Can't Preserve Space When Convert Docx To XSL-FO

PostPosted: Fri May 01, 2015 5:37 am
by qq955166
Hi Jason, first of all, I very appreciate your reply, thank you.

I read your link about w:t attribute, but how do I create a significant whitespace?

and I don't get what should be changed in setAttribute(Element el, String fontName), could you advise me more in detail?

Thanks!

Re: Can't Preserve Space When Convert Docx To XSL-FO

PostPosted: Tue May 26, 2015 1:40 pm
by jason