Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing Content Controls in 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?

like image 682
Ingó Vals Avatar asked Aug 10 '10 11:08

Ingó Vals


People also ask

What is content control in asp net?

Content controls provide a way for you to design documents and templates that have these features: A user interface (UI) that has controlled input like a form.

Does OpenXml require office?

It does not require Microsoft Office but does require Microsoft Windows. It can be used as a standalone converter with products that read Office's older binary formats, such as OpenOffice.org. Microsoft Office 2008 for Mac and Microsoft Office for Mac 2011 support the Office Open XML format.

How do I add a reference to DocumentFormat OpenXml packaging?

Go to your Solution Explorer > right click on references and then click Manage NuGet Packages . Then search in tab "Online" for DocumentFormat. OpenXml and install it. After you can use DocumentFormat.

What is run in OpenXml?

A run is represented by an r element, which allows the producer to combine breaks, styles, or formatting properties, applying the same information to all the parts of the run. Just as a paragraph can have properties, so too can a run.


1 Answers

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();
    }
like image 199
DevTun Avatar answered Oct 16 '22 06:10

DevTun