替换 OpenXML 中的内容控件

发布于 2024-09-14 01:25:53 字数 671 浏览 5 评论 0原文

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

然后,我考虑将 CustomXML 添加到 .docx,但由于 i4i 诉讼而放弃了这一做法。

然后我决定通过 OpenXML SDK 2.0 简单地更改内容控件的文本,但即使它如此标记,内容控件也不会消失。我猜它不知道文本发生了变化,除非它发生在单词内部。

我也许可以删除 CC 并放置文本,但我担心它可能带来格式和样式问题,而且它也会违背内容控制的目的。

然后我开始想知道是否可以定义自己的 Word 可以识别的占位符。也许通过构建块。它不需要做任何事情,除了使用 OpenXML 很容易找到并且可以以某种方式标记之外,这样我就知道用什么来替换它。我不太确定 Building Blocks 能做什么,但我希望它是可行的。

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

a)易于放置在模板中的东西,也许是预定义的内容控制占位符,您可以将其放置在您不需要的位置并根据您的喜好设置样式。

b) 添加数据后,它会删除所有占位符,并且不会再次修改。它保留占位符中定义的样式/格式。

回顾一下,我需要回答

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

-或者-

我可以为 Word 文档定义自己的自定义 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.

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

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.

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.

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)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)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.

TO RECAP, I need answer to either

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

-OR-

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放血 2024-09-21 01:25:53

也许这可以帮助你,

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

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();
    }
可是我不能没有你 2024-09-21 01:25:53

看一下使用 Field。邮件合并字段正是为此目的而设计的。

Take a look at using a Field. The mail merge fields are designed for exactly this purpose.

厌倦 2024-09-21 01:25:53

从您的问题中我不明白您是否只对使用 OpenXML SDK 修改 ContentControl/SDT 时自动删除 ContentControl/SDT 的解决方案感兴趣,或者您是否希望它在以编程方式或用户修改内容后消失。

如果是前者,我认为您必须按照 Bilel 的建议自行删除它。如果是后者,您应该查看此属性: ContentControl.Temporary(“当用户在控件中键入内容时,或者以编程方式更改控件中的文本时,ContentControl 会自动删除。当 ContentControl 从文档中自动删除时,控件中的文本控制权仍保留在文档中。”)

I don't understand from your question if you are only interested in a solution that automatically removes the ContentControl/SDT when you modify it using the OpenXML SDK, or whether you want it to disappear after the content is modifed programmatically or by a user.

If the former, I think you'll have to remove it yourself as Bilel suggested. If the latter, you should look at this property: ContentControl.Temporary ("the ContentControl is automatically deleted when the user types in the control, or when the text in the control is changed programmatically. When the ContentControl is automatically deleted from the document, the text in the control remains in the document.")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文