Welcome to OpenXML Developer Sign in | Join | Help

How to find all content conrols on dotx doc and replace them with text

Last post 08-20-2008, 6:26 AM by knst. 1 replies.
Sort Posts: Previous Next
  •  08-19-2008, 4:56 PM 3585

    How to find all content conrols on dotx doc and replace them with text

    Purpose : take docx put text from some outer datasorce to content controls (dictionary  CCtag, value to keep it simple). And remove CC from document keeping all text and formating.

    Spent 3 day already. The only result I achieved - I can find and delete content controls from docx. But any try to add text lead to document corruption. Tried to insert <w:t>bla-bla</w:t>
    Probably cause of error is these attributes
    w:rsidR="006D4366" w:rsidRPr="00E71775" w:rsidRDefault="006D4366" w:rsidP="006D4366" which are usually present in tags. Have no idea how to generate them.

    Besides the aproach with replacing CC with text is not the best one. In this case all  formatting is lost. Will be very thankful for any help.



  •  08-20-2008, 6:26 AM 3590 in reply to 3585

    Re: How to find all content conrols on dotx doc and replace them with text

    found such solution

     PackagePart documentPart = null;
                const string documentRelationshipType =
                    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
                foreach (PackageRelationship relationship in pckg.GetRelationshipsByType(documentRelationshipType))
                {
                    documentPart =
                        pckg.GetPart(PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri));
                    break;
                }

                XmlDocument doc = new XmlDocument();
                doc.Load(documentPart.GetStream());

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

                foreach (XmlNode sdtNode in doc.SelectNodes(@"./w:document/w:body//w:sdt", nsmgr))
                {
                    XmlNode propNode = sdtNode.SelectSingleNode(@"./w:sdtPr/w:alias", nsmgr);
                    if (propNode != null)
                    {
                        foreach (XmlAttribute attrNode in propNode.Attributes)
                            Console.WriteLine(attrNode.Value);
                        XmlNode node = sdtNode.SelectSingleNode("//w:sdtContent/w:p", nsmgr);
                        if (node == null)
                            node = sdtNode.SelectSingleNode("//w:sdtContent/w:r", nsmgr);
                        XmlNode clone = node.Clone();
                        XmlNode wt = clone.SelectSingleNode("//w:t", nsmgr);
                        wt.InnerText = "bla-bla";
                        sdtNode.ParentNode.ReplaceChild(clone, sdtNode);
                    }
                }
                documentPart.GetStream().Seek(0, SeekOrigin.Begin); //reset the stream-pointer
                documentPart.GetStream().SetLength(0);
                doc.Save(documentPart.GetStream());


View as RSS news feed in XML