在通过单词自动化服务运行后,表文本会获得额外的间距 [英] Table text gets extra spacing after running through word automation services

查看:66
本文介绍了在通过单词自动化服务运行后,表文本会获得额外的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

以前有人看过吗?有没有办法使用包含表的HTML altChunk来强制单词自动操作不弄乱我的表?

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?

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

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?

他是这些表格在HTML中的外观的非常简单的示例:

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>

我尝试为td显式设置padding-bottom: 0,但这没有任何区别.它在HTML中的显示方式几乎与它在Word文档中的显示方式相同,但是出于某些原因,Word Automation Services在每行的底部增加了空间.

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.

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

HTMLFragmentsDictionary<string,string>,其中包含要在文档中插入的表的HTML.

Where HTMLFragments is a Dictionary<string,string> containing the HTML of the table to be inserted in the doc.

推荐答案

在尝试使用css附加到各个元素的marginpadding的不同组合之后,我决定做一些完全违反直觉的事情并尝试将单元格的内容包装在p标记中.我当时在想,我可能可以通过这种方式在td内的p上显式设置边距.因此:

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>

成为这个:

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

令人惊讶的是,它实际上有效!没有我,我实际上没有添加任何针对这些p标签的CSS.当然,这是违反直觉的,因为如果使用原始HTML进行操作,由于p标记的默认边距,最终会在单元格中的文本周围添加更多空间,但是在这里它实际上将其删除.

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 a p tag, but here it actually removes it.

这似乎是Word Automation Services解释HTML altChunk与Word正确方式的方式上的错误.

This seems to be a bug in the way Word Automation Services interprets the HTML altChunk versus how Word proper does.

这篇关于在通过单词自动化服务运行后,表文本会获得额外的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆