Table text gets extra spacing after running through word automation services


I'm generating a document from a bunch of template documents and in addition I'm adding some tables as html altChunks (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 altChunks) 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?

<strong>Edit</strong>: 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:

<pre class="snippet-code-css lang-css prettyprint-override"> 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; } <pre class="snippet-code-html lang-html prettyprint-override"><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 and padding attached to various elements using css, I decided to do something completely counter-intuitive and try wrapping the contents of the cells in a p tag. I was thinking that I might be able to explicitly set the margins on the p inside the td that way. So this:

<td class="col0">4</td>

Becomes this:

<td class="col0">



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 <em>adding</em> more space around the text in the cells because of the default margin on a p 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.


  • sqlalchemy force all connections to close mysql
  • identifying ``method code too large`` origin
  • How can I force an AngluarJS form to realize required fields have been filled through DOM manipulati
  • Parsing Java String to date
  • jQuery.fn.load() is deprecated?
  • JAXB 2 in an Oracle 10g Webapp
  • regex, multiline extract in R
  • Inno Setup Search for specifc file on a CD, retrieve exact filepath and return value to [Files]-Sect
  • dc.js: Reducing rows in data table
  • Extract data between rows r
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • How to add System.Windows dll to Visual Studio 2010 express?
  • CSS: How to fix overlapping divs
  • Visual studio alerts workspace already exists
  • Cross platform UI spacing/padding
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Outlines on links in IE9 remains when focus is changed
  • Django model inheritance, filtering models
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Android full screen on only one activity?
  • Using $this when not in object context
  • Splitting given String into two variables - php
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Fetching methods from BroadcastReceiver to update UI
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Accessing IRQ description array within a module and displaying action names
  • Perl system calls when running as another user using sudo
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • GridView Sorting works once only
  • 0x202A in filename: Why?
  • How to disable jQuery.jplayer autoplay?
  • WPF Applying a trigger on binding failure
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • How to push additional view controllers onto NavigationController but keep the TabBar?