如何保持打开的 xml 文档的样式

2023-12-04

我使用开放 XML(Microsoft Word - .docx)作为文件模板来自动生成其他文档。在模板文档中,我定义了内容控件,并编写了代码来替换这些内容控件中的内容。

内容被替换并生成文档,但我正在努力保持风格。在Word中,在检查内容控件的属性时,我选中了“使用样式将文本格式化为空控件:样式”复选框,并选中了“编辑内容时删除内容控件”。当文档由代码生成时,这似乎没有任何影响。

Here is how I set the properties of the content control in Word This is my code (which a community member here was kind enough to help with) for replacing the data in the content controls. Any ideas what I should do in order to keep the formatting? The formatting is simple text formatting, like size and font. Please advice:

    private static void ReplaceTags(MainDocumentPart mainPart, string tagName, string tagValue)
    {
        //grab all the tag fields
        var tagFields = mainPart.Document.Body.Descendants<SdtBlock>().Where
            (r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName);

        foreach (var field in tagFields)
        {
            //remove all paragraphs from the content block
            field.SdtContentBlock.RemoveAllChildren<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
            //create a new paragraph containing a run and a text element
            var newParagraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
            var newRun = new DocumentFormat.OpenXml.Wordprocessing.Run();
            var newText = new DocumentFormat.OpenXml.Wordprocessing.Text(tagValue);
            newRun.Append(newText);
            newParagraph.Append(newRun);
            //add the new paragraph to the content block
            field.SdtContentBlock.Append(newParagraph);
        }
    }

当您为内容控件分配新的样式时RunProperties元素被添加到下面SdtProperties。例如,如果我分配一个名为的新样式Style1我可以看到生成了以下 XML:

<w:sdt>
    <w:sdtPr>
        <w:rPr>
            <w:rStyle w:val="Style1" />
        </w:rPr>
    <w:alias w:val="LastName" />
    <w:tag w:val="LastName" />
    ....

您需要获取该值并将其分配给新的Paragraph您正在创建,添加ParagraphSdtBlock然后删除SdtBlock这就是当您选择“编辑内容时删除内容控件”选项时 Word 所做的操作。这RunProperties<w:rPr>元素。以下内容应该可以完成您的任务。

private static void ReplaceTags(MainDocumentPart mainPart, string tagName, string tagValue)
{
    //grab all the tag fields
    IEnumerable<SdtBlock> tagFields = mainPart.Document.Body.Descendants<SdtBlock>().Where
        (r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName);

    foreach (var field in tagFields)
    {
        //grab the RunProperties from the SdtBlcok
        RunProperties runProp = field.SdtProperties.GetFirstChild<RunProperties>();

        //create a new paragraph containing a run and a text element
        Paragraph newParagraph = new Paragraph();
        Run newRun = new Run();
        if (runProp != null)
        {
            //assign the RunProperties to our new run
            newRun.Append(runProp.CloneNode(true));
        }
        Text newText = new Text(tagValue);
        newRun.Append(newText);
        newParagraph.Append(newRun);
        //insert the new paragraph before the field we're going to remove
        field.Parent.InsertBefore(newParagraph, field);

        //remove the SdtBlock to mimic the Remove content control when contents are edited option
        field.Remove();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何保持打开的 xml 文档的样式 的相关文章

随机推荐

  • 如何从 UIImage 中 NSLog 像素 RGB?

    我只想 1 复制像素数据 2 迭代并修改每个像素 仅向我展示如何将 ARGB 值 NSLog 为 255 3 从新的像素数据创建 UIImage 如果有人能告诉我如何将像素的 RGBA 值 NSLog 为 255 我就可以弄清楚血淋淋的细节
  • 从路径读取事件日志文件

    我的问题与这个非常相似如何以编程方式打开事件日志 除了我正在记录任何东西 我需要从多台未连接的机器创建日志条目数据库 我收到 evtx 文件 然后尝试处理它们 现在我正在从导出的 xml 文件中执行此操作 但我想跳过到 xml 转换部分 我
  • ng-src 不适用于 youtube 嵌入视频

    我对 YouTube 嵌入代码有一个小问题 在我的控制器中 scope emedUrl https www youtube com embed
  • Python 中的多态性

    class File object def init self filename if os path isfile filename self filename filename self file open filename rb se
  • 手动响应鼠标悬停事件

    有没有办法触发 React 的 mouseover 和 mouseenter 事件 可以开火 ReactDOM findDOMNode someNode focus ReactDOM findDOMNode someNode click 有
  • PHP 找不到保存处理程序内存缓存

    我正在为这个问题绞尽脑汁 它应该很简单 但似乎找不到解决方案 所以希望你们中的一个人可以帮助我 我正在尝试使用 php 的 memcache 扩展来存储会话 我正在运行 MAMP 并已正确安装了扩展 我认为 它在我执行 phpinfo 时显
  • JPA中NamedQuery注解有什么好处?

    刚才我写了一个NamedQuery对于 JPA 实体 我们对此感到非常高兴 这里是 NamedQuery name Panties RED PANTIES QRY query SELECT p FROM Panties p WHERE p
  • 使用 iTextSharp 在 VB.NET 中读取 PDF 书签

    我正在制作一个工具 可以扫描 PDF 文件并搜索 PDF 书签和正文中的文本 我正在使用带有 VB NET 和 iTextSharp 的 Visual Studio 2008 如何从现有 PDF 文件加载书签列表 这取决于您所说的 书签 时
  • 如何找到采样边界内的最大圆?

    给定一组二维点 这些点是不规则形状的边界 该形状可能不是凸的并且可能有内孔 是否有一种算法可以找到适合边界的最大圆 我已经做了很多搜索 并且确实找到了接近的算法 例如最大的空圆问题 但到目前为止我发现没有一个与我所拥有的约束相匹配 动机 由
  • 将两行文本传递到InputBox中

    我的代码中有以下行要求用户输入 strFind InputBox Please enter the text to look for Replace Text in Files 我需要用户输入由 LF 或 CR 或 CRLF 换行符 分隔的
  • 为什么协议中的仅获取属性要求不能通过符合的属性来满足?

    为什么下面的代码会产生错误 protocol ProtocolA var someProperty ProtocolB get protocol ProtocolB class ConformsToB ProtocolB class Som
  • 在 WP7 中反序列化 JSON

    我有这个 JSON 我正在尝试在 Windows Phone 上读取它 我一直在玩DataContractJsonSerializer和 Json NET 但运气不太好 尤其是阅读每个 条目 lastUpdated 16 12 filter
  • 如何用SQL-Server 2000查询层次信息?

    我有一张桌子Folders包含有关文件夹的分层信息 FolderID FolderName ParentID 1 Folder1 0 2 Folder2 1 3 Folder3 2 4 Folder4 3 For Folder4我想获取以下
  • Android MapView 无法删除标记

    我正在使用 locationManager 和 ItemizedOverlay 来绘制 我的位置 标记 问题是当触发 onLocationChanged 时 我正在绘制新标记 而不是最后一个移动到新位置的标记 这是我的 onLocation
  • 如何正确清理 Excel 互操作对象?

    我在 C 中使用 Excel 互操作 ApplicationClass 并将以下代码放入我的finally子句中 while System Runtime InteropServices Marshal ReleaseComObject e
  • jQuery 按需加载图像

    我正在尝试按需加载图像 以防止下载不需要的图像 我正在使用 jQuery 到目前为止 这就是我想出的 region image attr src images e key jpg load function this fadeIn e ke
  • 如何避免在接口中重复使用相同的实现代码?

    首先 我为 又一个界面问题 道歉 不过 我认为这个问题可能值得一问 因为这是一个奇怪的问题 我正在使用的项目使用 Actionscript 3 但这更多是一个一般的 OOP 问题 情况是这样的 我有一个已经从基类继承的类 它是电子游戏中的一
  • Spring Data JDBC:DataRetrievalFailureException:无法将 [oracle.sql.ROWID] 转换为 [java.lang.Number]

    我是 Spring Data JDBC 的新手 我正在努力创建一个简单的 Dto 并将其持久保存在数据库上 我正在使用 Spring Boot 2 1 1 RELEASE 和 Oracle 12 数据库 UserDto Table valu
  • 如何使 std::istream_iterator 只读直到行尾?

    有以下代码 std vector
  • 如何保持打开的 xml 文档的样式

    我使用开放 XML Microsoft Word docx 作为文件模板来自动生成其他文档 在模板文档中 我定义了内容控件 并编写了代码来替换这些内容控件中的内容 内容被替换并生成文档 但我正在努力保持风格 在Word中 在检查内容控件的属