How to add a content control to a Word 2007 document using OpenXML

I want to create a Word 2007 document without using the object model. Therefore, I would prefer to create it using the open xml format. So far I have managed to create a document. Now I want to add a content control to it and map it to xml. Can anyone guide me on the same ???

+5
source share
2 answers

Anoop,

You said you could create a document using OpenXmlSdk. With this in mind, you can use the following code to create a content control to add your document to the Wordprocessing.Body element.

//praragraph to be added to the rich text content control
Run run = new Run(new Text("Insert any text Here") { Space = StaticTextConstants.Preserve });
Paragraph paragraph = new Paragraph(run);

SdtProperties sdtPr = new SdtProperties(
        new Alias { Val = "MyContentCotrol" },
        new Tag { Val = "_myContentControl" });
SdtContentBlock sdtCBlock = new SdtContentBlock(paragraph);
SdtBlock sdtBlock = new SdtBlock(sdtPr, sdtCBlock);

//add this content control to the body of the word document
WordprocessingDocument wDoc = WordprocessingDocument.Open(path, true); //path is where your word 2007 file is
Body mBody = wDoc.MainDocumentPart.Document.Body;
mBody.AppendChild(sdtBlock);

wDoc.MainDocumentPart.Document.Save();
wDoc.Dispose();

, . , , " XML". , CustomXmlBlock ContentControl ?

+7

Word www.codeplex.com.

, , .

  • Word. Office (Round thingy) " Word" . , .

  • , Content, . " " ( ).

  • .

  • , . , xml .

  • xml , .

  • openxml sdk 1.0 2.0 ( ctp), Word XML , .

, Word xml. , , "a.zip". zip , . "document.xml". XML- customXml "item1.xml".

, .

+3

All Articles