在运行word自动化服务后,表文本会获得额外的间距



我从一堆模板文档中生成一个文档,此外,我还添加了一些表作为html altChunks(因为这比尝试使用OpenXML创建表容易得多)。这一切都很好,给了我想要的结果。但是,由于某些版本的Word(尤其是在Mac上)处理altChunk的方式存在一些缺陷,并且无法自动更新TOC,我们决定将其设置为通过Sharepoint中的Word自动化服务运行我们的报告。因此,我们像以前一样生成文档,然后通过word自动化将altChunk扁平化为单个文档并更新TOC。

这解决了我们所有的兼容性问题,并更新了我们的TOC,但不幸的是,有一个副作用。我们的表格现在在文本上方和下方添加了空间,使表格比以前长得多。这以前不是问题,即使我们打开文档并在Word中重新保存(因此使所有altChunk都变平),所以这似乎是Word自动化特有的问题。

有人见过这个吗?有没有一种方法,使用包含表的HTML altChunk来强制单词自动化,以免弄乱我的表?

编辑:如果我在Word中选择有问题的表,并选择主选项卡"行和段落间距",然后我可以选择"删除段落后的空格"将表折叠回应有的位置。因此,Word Automation Services似乎希望在表中的段落后添加空格,而Word本身则不希望。有人知道如何阻止这种情况吗?

他是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>

我尝试将padding-bottom: 0显式设置为td,但没有任何区别。它在HTML中的外观与在Word文档中的外观基本相同,但由于某些原因,Word Automation Services会在每行的底部添加空间。

插入altChunk的代码如下所示:

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();
}

其中,HTMLFragments是包含要插入文档中的表的HTML的Dictionary<string,string>

在跌跌撞撞地尝试了使用cssmarginpadding连接到各种元素的不同组合后,我决定做一些完全违背直觉的事情,并尝试将单元格的内容包装在p标签中。我在想,我可能能够以这种方式在td内部显式地设置p上的边距。所以这个:

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

变成这样:

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

令人惊讶的是,它确实有效!没有我添加任何针对p标签的CSS。当然,这是违反直觉的,因为如果你对原始HTML这样做,你最终会因为p标签上的默认边距而在单元格中的文本周围添加更多的空间,但在这里它实际上会删除它

这似乎是Word Automation Services解释HTML altChunk的方式与Word本身的方式之间的一个缺陷。

相关内容

最新更新