Page 1 of 1

Table Autospacing

PostPosted: Tue Aug 23, 2016 1:50 am
by Roil
Hello again,

I am now having another issue that might be too specific, but I figured I would inquire anyway. For reference, I am using docx4j-3.3.0 and docx4j-ImportXHTML-3.3.0.

One of my clients is reporting an issue that revolves around auto spacing and tables after converting to docx. The text that immediately follows a table (any table) is jumping around when he tries to edit it. It seems to only be happening in the first piece of content after the table. I have Word 2013, and I am not having the issue. He uses Word 2010, and I also have a colleague that uses 2010. Both of them are able to reproduce the issue. I have added some screenshots below that I hope will help show what I am referring to.

This is how it SHOULD look:
http://imgur.com/a/pldp8

This is what he is getting when he first opens the document. (Notice the top portion of a line of text that looks cut off on the 5th line below the table)
http://imgur.com/a/VmP5b

This is what it looks like when he tries to make any change in that paragraph:
http://imgur.com/a/e6lom

Here is the relevant code that sets the spacing. I have removed some of the surrounding code for brevity.

Code: Select all
var spacing = factory.createPPrBaseSpacing();
var line = new Packages.java.math.BigInteger(240);
spacing.setAfterAutospacing(true);
spacing.setBeforeAutospacing(true);
spacing.setLine(line);
spacing.setLineRule(Packages.org.docx4j.wml.STLineSpacingRule.AUTO);

var styles = sdp.getJaxbElement();
var docDefaults = styles.getDocDefaults();
var defaultRPr = docDefaults.getRPrDefault();
var defaultPPr = docDefaults.getPPrDefault();
var rPr = defaultRPr.getRPr();
var pPr = defaultPPr.getPPr();

var keep = new Packages.org.docx4j.wml.BooleanDefaultTrue();
keep.setVal(true);

pPr.setSpacing(spacing);
pPr.setKeepLines(keep);
pPr.setKeepNext(keep);


I have already confirmed with that client that if I remove the following two lines of code, the issue does not appear, so it appears that it is related to auto spacing somehow.

Code: Select all
spacing.setAfterAutospacing(true);
spacing.setBeforeAutospacing(true);


Finally, here is the relevant HTML for that table and the paragraph that follows it. (I commented out the BR tags because it was not properly escaping them when posting)

Code: Select all
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<meta charset='UTF-8' />
<title></title>
</head>
<body>
<div class='document'>
<table width="100%" border="1" cellspacing="0"
style="border-collapse: collapse">
<tr>
<td width="15%" style="font-weight: bold; font-style: italic;">
Customer Contact</td>
<td width="35%"></td>
<td width="15%" style="font-weight: bold; font-style: italic;">
Project Code / PT</td>
<td width="35%"></td>
</tr>

<tr>
<td style="font-weight: bold; font-style: italic;">Phone</td>
<td></td>
<td style="font-weight: bold; font-style: italic;">OTN/SSR</td>
<td></td>
</tr>

<tr>
<td style="font-weight: bold; font-style: italic;">Email</td>
<td></td>
<td style="font-weight: bold; font-style: italic;">Quote
Number(s)</td>
<td>&nbsp;</td>
</tr>
</table>

<p>This Statement of Work is subject to the terms and conditions of
the <b style="color: red;">&lt;agreement name&gt;</b> agreement
between <b style="color: red;">&lt;master signing
entity&gt;</b> and <b style="color: red;">&lt;customer signing
entity&gt;</b> executed on <b style="color: red;">&lt;date&gt;</b>
including any amendments (the Agreement). Capitalized terms will
have the meanings given in this document or in the Agreement.
Unless specifically stated otherwise in this SOW, in the event of a
conflict between this SOW and the terms of the Agreement, the order
of precedence specified in the Agreement will apply. If there is no
order of precedence specified, the terms of the Agreement will
control.
<!--br-->
<!--br-->
Customer's signature or electronic authorization acknowledges that
Customer has reviewed and agrees to this SOW. The effective date
is, in the case where manual signature is required, the date on
which the last party signs this SOW, or otherwise the date that
makes available or provides the order confirmation to
Customer (SOW Effective Date).
<!--br-->
<!--br-->
</p>
</div>
</body>
</html>


They are working around the issue by manually changing the setKeepLines and setKeepNext within Word for now, but I would like to make a fix on the back end. As you can see in the code above, even when I apply that fix to the docDefaults on my side, they still have the issue.

Since I am sending the entire document over in one giant XHTML string, it makes it difficult for me to split that up. Any fixes I can make would have to be able to be applied to the entire document using docx4j or modified in the corresponding HTML.

Do you have any ides or suggestions about how I can work around this issue, or if it is a bug? I understand that using Word 2010 makes it harder to test and resolve, but as they are a large company, they cannot easily roll out an Office upgrade to solve it that way.

Any help or suggestions you can provide would be greatly appreciated. Thanks!

Re: Table Autospacing

PostPosted: Tue Aug 23, 2016 8:41 am
by jason
I have Word 2010 available for testing.

Are you able to attach/post a short docx which exhibits the issue? (You can delete everything in it which isn't required to demo the glitch)

Re: Table Autospacing

PostPosted: Thu Aug 25, 2016 5:36 am
by Roil
See attached. I had my colleague create it, and it was still showing the error in her Word document when she saved it. The issue is happening with the paragraph right after the table. When she went to make an edit to that paragraph, it would quickly realign the spacing or show double wording. Hopefully, you can recreate the issue. I don't know a really good way to classify what is going on with it.

Re: Table Autospacing

PostPosted: Tue Sep 06, 2016 9:19 pm
by jason
Sorry for the delay in replying.

Short answer: layout glitch with HTML Paragraph auto spacing in Word 2010

For the benefit of others, we are talking about:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:spacing w:beforeAutospacing="1" w:afterAutospacing="1"/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


See further http://webapp.docx4java.org/OnlineDemo/ ... ing_1.html

Playing with your document, the same behaviour occurs in Word 2010 even without the table.

Looks like this may be a glitch in Word 2010's layout model. See further http://answers.microsoft.com/en-us/offi ... 852?auth=1

Looks like you should avoid paragraph autospacing if you want the docx to behave properly in Word 2010.

How is this setting getting into your docx? It isn't being added by docx4j's XHTML Import is it?

As per the link above, Word 2010 has an option: File > Options > Advanced > (scroll to bottom) Layout Options > Don't use HTML Paragraph auto spacing, which when checked, seems to make the document perform OK

For more, you can Google: https://www.google.com/search?q=HTML+Pa ... +Word+2010

Re: Table Autospacing

PostPosted: Wed Sep 14, 2016 9:37 pm
by Roil
Thank you for the information. I just got back from a vacation today, so I will speak with my client about our options.

I am manually setting the Before and After auto spacing in my code before the XHTML is converted. Then I set the spacing of the docDefaults. See the lines below from my original post.

Code: Select all
spacing.setAfterAutospacing(true);
spacing.setBeforeAutospacing(true);

var styles = sdp.getJaxbElement();
var docDefaults = styles.getDocDefaults();
var defaultRPr = docDefaults.getRPrDefault();
var defaultPPr = docDefaults.getPPrDefault();
var rPr = defaultRPr.getRPr();
var pPr = defaultPPr.getPPr();

pPr.setSpacing(spacing);

Re: Table Autospacing

PostPosted: Thu Apr 27, 2017 2:36 am
by Roil
For anyone else struggling with this, I was able to set this in the code as opposed to having the user do it on the client side. Here is a snippet of the code.

Code: Select all
var r = pkg.getMainDocumentPart().getRelationshipsPart().getRelationshipByType(Packages.org.docx4j.openpackaging.parts.relationships.Namespaces.SETTINGS);
var dsp = pkg.getMainDocumentPart().getRelationshipsPart().getPart(r);
var compat = dsp.getJaxbElement().getCompat();
if(compat != null) {
  var noHTMLAutoSpacing = new Packages.org.docx4j.wml.BooleanDefaultTrue();
  noHTMLAutoSpacing.setVal(true);
  compat.setDoNotUseHTMLParagraphAutoSpacing(noHTMLAutoSpacing);
}