替换OpenXML中的内容控件 [英] Replacing Content Controls in OpenXML

查看:244
本文介绍了替换OpenXML中的内容控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些占位符.起初,我将内容控制作为解决方案,但是我遇到了一些问题.

I need something as a placeholder. I at first looked to Content Control as a solution but I'm having some problems with it.

然后我考虑将CustomXML添加到.docx,但是由于i4i诉讼而拒绝了.

I then looked into adding CustomXML to the .docx but turned away from that because of the i4i lawsuit.

然后,我决定只通过OpenXML SDK 2.0来简单地更改内容控件的文本,但是即使如此标记,内容控件也不会消失.我猜想除非文本出现在单词中,否则不知道文本是否已更改.

Then I decided I would just plain change the text of the Content Control through OpenXML SDK 2.0 but even if it's so marked the Content Control doesn't go away. I guess that it doesn't know that the text changed unless it happens inside word.

我也许可以只删除CC并放置文本,但是我担心它可能带来的格式和样式问题,而且还违反了内容控件的目的.

I could perhaps just remove the CC and place text instead but I'm afraid of problems with format and styles it could bring and also it would kind of defy the purpose of the Content Control.

然后,我开始怀疑是否可以定义Word可以识别的自己的占位符.通过构建基块.除了使用OpenXML很容易找到并且可以以某种方式标记之外,它不需要执行任何操作,因此我知道用它替换什么.我不太确定可以使用Building Blocks做些什么,但我希望它是可行的.

Then I started wondering if I could define my own placeholders that Word could recognize. Through Building blocks perhaps. It doesn't have to do anything except be easy to find using OpenXML and somehow taggable so I know what to replace it with. I'm not really sure what can be done with Building Blocks but I'm hoping it's do-able.

不确定哪种解决方案最适合我,但我需要的是:

Not sure what solution would be best for me but what I need is:

a)易于放置在模板中的东西,也许是预定义的Content Control占位符,您可以将它们放置在不需要的地方并根据需要设置样式.

a)Something that's easy to place in the template, perhaps predefined Content Control placeholders that you can place where you wan't and style as you like.

b)添加数据后,它将删除所有占位符,不会再次对其进行修改.它将样式/格式保留在占位符中.

b)When the data has been added it removes all placeholders, it won't be modified again. It keeps the style/format defined in the placeholder.

要回顾,我需要回答

如何在OpenXML SDK中编辑内容控件,以便在添加文本后将其删除.

How can I edit Content Controls in OpenXML SDK so they will be removed after text is added.

-或-

我可以为自己的Word文档定义自己的自定义OpenXML标记,然后替换吗?

Can I define my own custom OpenXML tag for a Word Document that I could then replace?

推荐答案

也许这可以为您提供帮助,

Perhaps this can help you,

private void DeleteSdtBlockAndKeepContent(MainDocumentPart mainDocumentPart, string sdtBlockTag)
    {
        List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
        SdtBlock sdtA = null;

        foreach (SdtBlock sdt in sdtList)
        {
            if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == sdtBlockTag)
            {
                sdtA = sdt;
                break;
            }
        }


        OpenXmlElement sdtc = sdtA.GetFirstChild<SdtContentBlock>();
        OpenXmlElement parent = sdtA.Parent;

        OpenXmlElementList elements = sdtc.ChildElements;

        var mySdtc = new SdtContentBlock(sdtc.OuterXml);

        foreach (OpenXmlElement elem in elements)
        {

            string text = parent.FirstChild.InnerText;
            parent.Append((OpenXmlElement)elem.Clone());
        }

        sdtA.Remove();
    }

这篇关于替换OpenXML中的内容控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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