I'm generating a document from a bunch of template documents and in addition I'm adding some tables as html altChunk
s (because it's a lot easier than trying to create a table with OpenXML). This all worked fine and gave me the result I wanted. But, because of some deficiencies with the way altChunk
were being processed by some versions of Word (especially on the Mac) and the inability to automatically update the TOC, we decided to set it up to run our reports through word automation services in Sharepoint. So we generate our doc as before and then pass it through word automation to flatten out the altChunk
into a single document and update the TOC.
This solves all our compatibility problems and updates our TOC, but unfortunately, there is a side-effect. Our tables now have added space above and below the text that makes the tables much longer than they were before. This wasn't an issue before, even if we opened our document and resaved in Word (hence flattening all the altChunk
s) so it seems to be a problem specific to word automation.
Has anybody seen this before? Is there a way, using an HTML altChunk
containing a table to force word automation not to mess up my tables?
Edit: If I select the offending table in Word and select the home tab, "Line and Paragraph spacing", I can then select "Remove Space After Paragraph" to collapse the table back to what it should be. So it appears to be that Word Automation Services wants to add space after paragraphs in the table while Word itself does not. Does anybody have any idea how to stop that?
He's a very simple example of what these tables look like in HTML:
table {
font-size: 0.8em;
border-collapse:collapse;
font-family: calibri, sans-serif
}
caption {
font-size: 1.05em;
font-weight: bold;
background-color: transparent;
}
tbody td {
border: solid #909090 1px;
font-size: 0.9em;
}
thead th {
border-width: 0 1px 0 1px;
border-style: solid;
border-color: #909090;
}
.tableNotes {
font-size: 6pt;
font-family: arial, sans-serif;
}
tbody td {
text-align: center;
}
thead th {
background-color: #98a5b5;
}
.sectionHeader {
font-weight: bold;
text-align: left;
background-color: #becfe3;
}
<body>
<table width="100%">
<thead>
<tr>
<th class="col0">col0</th>
<th class="col1">col1</th>
<th class="col2">col2</th>
<th class="col3">col3</th>
<th class="col4">col4</th>
<th class="col5">col5</th>
<th class="col6">col6</th>
<th class="col7">col7</th>
</tr>
</thead>
<tbody>
<tr>
<td class="sectionHeader" colspan="8">Section 1</td>
</tr>
<tr>
<td class="col0">4</td>
<td class="col1">4</td>
<td class="col2">4</td>
<td class="col3">4</td>
<td class="col4">4</td>
<td class="col5">4</td>
<td class="col6">4</td>
<td class="col7">2</td>
</tr>
</tbody>
</table>
</body>
I tried explicitly setting padding-bottom: 0
for td
, but that didn't make any difference. The way it looks in HTML is pretty much how it should look in the Word document, but Word Automation Services adds space to the bottom of each row for some reason.
The code to insert the altChunk
looks like this:
Paragraph p = placeholder.Ancestors<Paragraph>().FirstOrDefault();
if (p != null)
{
var chunk = WordDoc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);
var id = WordDoc.MainDocumentPart.GetIdOfPart(chunk);
// populate the chunk
using (var stream = chunk.GetStream())
{
using (StreamWriter tw = new StreamWriter(stream))
{
tw.Write(HTMLFragments[tableName]);
}
}
// Now we need an altChunk to insert into the parent
AltChunk altChunk = new AltChunk()
{
Id = id,
AltChunkProperties = new AltChunkProperties()
{
MatchSource = new MatchSource()
{
Val = true
}
}
};
p.InsertAfterSelf(altChunk);
p.Remove();
}
Where HTMLFragments
is a Dictionary<string,string>
containing the HTML of the table to be inserted in the doc.
After stumbling around and trying different combinations of
margin
andpadding
attached to various elements usingcss
, I decided to do something completely counter-intuitive and try wrapping the contents of the cells in ap
tag. I was thinking that I might be able to explicitly set the margins on thep
inside thetd
that way. So this:Becomes this:
And amazingly, it actually works! Without me actually adding any CSS targeting those
p
tags. It's counter-intuitive, of course, because if you do that with the original HTML you will end up adding more space around the text in the cells because of the default margin on ap
tag, but here it actually removes it.This seems to be a bug in the way Word Automation Services interprets the HTML
altChunk
versus how Word proper does.