none
How to hide content controls on docx file ? RRS feed

  • Question

  • Hi

    I create successfuly programaticaly an word file (docx) based on an template.

    But on generated document i dont want to be visible the outlines and tags of the content controls.
    I use this controls to remark areas that i have dynamicaly change the content.

    see this screenshoot

    I there any way to remove this outlines and tags programatically ? (or some how to make them unvisibly on generated document?)

    Thanks in advance...

    Wednesday, May 16, 2012 11:24 AM

All replies

  • Why don't you just remove them when genarating the document? Do you need them in the generated document but just make them invisible (somehow)? 
    Wednesday, May 16, 2012 8:32 PM
  • Hi Giorgos,

    I assume you have a template with content controls in it and programmaticaly you find them and replace their content with values you want.
    if my assumption is correct, you can replace the whole content control ("sdt") with run ("r") or paragraph ("p") tags with your data in it, base on your need.

    for more info and code sample you can check: http://ericwhite.com/blog/2011/03/29/release-of-v2-of-doc-gen-system-xpath-in-content-controls/

    Thursday, May 17, 2012 6:04 AM
  • Hi !!
    Have you try this that you just describe ? For me it throws errors that the xml is invalid (i have to change more tags and this is very risky) .

    I have see this video .. and this example have the same "problem".

    Thursday, May 17, 2012 8:44 AM
  • how to remove ?

    i use this content controls as flags for my mechanism to know where i must insert my content.
    So after the "work" i need them to be invisible on the final user who reads the generated document.

    Thursday, May 17, 2012 8:50 AM
  • Hi,

    I think this is the by design behavior, as long as the content control has focus, the border and handle of it show up.  You may try to get focus elsewhere.

    best regards,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Thursday, May 17, 2012 9:20 AM
    Moderator
  • Yes, I have been try that, you only need content controls in your template document and not in the generated document. best way is to remove them during generation process or afterwards.

    The code is quite complicated and big for here to describe, what you need to do is download that sample code https://skydrive.live.com/redir.aspx?cid=5e385848af211ba9&resid=5E385848AF211BA9!325&parid=5E385848AF211BA9!237, 11-03-24-Gen-Docs-XPath.zip. open 11-03-24-Gen-Docs-XPath\DocumentGenerator\DocumentGenerator.cs file and look for Transform method.

    you will find this code at the beginning (I add some comments for you):

    static object Transform(XNode node, XElement document) { XElement element = node as XElement; if (element != null) { if (element.Name == w + "sdt") //Check if object is content control { //Here it read its tag, so it can detect which content control is that string tag = element.Elements(w + "sdtPr") .Elements(w + "alias") .Attributes(w + "val") .FirstOrDefault() .Value; if (tag == "SelectValue") { XElement run = element.Element(w + "sdtContent").Element(w + "r"); string valueSelector = GetContentControlContents(element); string newValue = document.XPathSelectElement(valueSelector).Value; // here it will return new run object, which will be replace the whole sdt (content control) return (new XElement(w + "r", run.Elements().Where(e => e.Name != w + "t"), new XElement(w + "t", newValue))); }
    .
    .
    .

    You can change it to cope with your need or get an idea to change your merging process.

    Thursday, May 17, 2012 9:25 AM
  • As I understand, you don't need Content Controls in the generated documents. You only need them in the template as placeholders. The why are you inserting the content into Content controls?

    You should do the following:

    • Insert the content as sibling element of the content control: cc.InsertBefore(contentElement) or cc.InsertAfter(contentElement). This way you will insert content element just before or after the CC (CC and content control will have the same parent element).
    • Delete the CC element

    This is just a high level picture. The problem here is that there is quite a lot of different cases like: CC is wrapping a table cell, CC is wrapped a table cell, CC is wrapping a paragraph (and you need to add one manually), and more. for each one of these cases you will need to implement the logic to insert the content. For example, sometimes you will need to create a paragraph and then inser the content into it, sometimes you will need to swap table cell and CC before deleting the CC, and so on.

    You can also take a look at Docentric toolkit which you can use it to do document generation and templating very easily. It only takes couple of lines of code to generate a document from a template (designed in MS Word) populated from data. It can handle all such situations (stated above).

    Hope this helps,

    Jure

    Friday, May 18, 2012 7:50 AM