none
Lock content after programmatically replaced Content Control with content RRS feed

  • Question

  • Hi,

    I have developed an application that does a lot of merging of documents using OpenXML, AltChunk and Content Controls. My Content Controls have the "Remove content control when contents are edited" checkbox checked.

    What I want to do next, is to lock the content that has replaced a Content Control. In other words, when the final document is ready, users will not be able to edit the content.
    Important note: I don't want to lock the whole document, just parts of it (where selected Content Controls are replaced with content).

    Is this possible?

    I have tried checking the "Contents cannot be edited" property on the Content Controls, both with and without the "Remove content control when contents are edited" checkbox checked. Doesn't work.

    Is there a way to do this using code?

    Thanks!

    Edit: Here's some of my code:

    using (MemoryStream mem = new MemoryStream())
    {
    mem.Write(byteArray, 0, (int) byteArray.Length);
    mem.Seek(0, SeekOrigin.Begin);
    chunk.FeedData(mem);
    }

    Word.AltChunk altChunk = new Word.AltChunk();
    altChunk.Id = altChunkId;

    OpenXmlElement parent = sdt.Parent;
    parent.InsertAfter(altChunk, sdt);
    sdt.Remove();


    Edit 2:
    I guess I now realize that my code doesn't really make use of the Content Controls other than using them as "bookmarks". What I need is to insert my content in the actual Content Controls.

    The content I'm trying to insert is content from other Word documents.

    Can anyone please advise me on how to do this?

    Friday, January 24, 2014 8:43 AM

All replies

  • Could you please restate your request as with your second edit I'm quite confused as to what the desired end-result is. Please list the steps as you see them, such as

    Open a document using the Open XML SDK
    ...

    Please also include any observed behavior that has occurred during your tests, including any errors. "Doesn't work" is not descriptive enough, we have to understand HOW something does not work.

    Please note that, if you use AltChunk the actual content of the file being inserted will only be integrated into the "target" once Word opens the document. Although it's not something I've ever tested, it's possible that Word is unable to perform the integration if the AltChunk has been assigned to a "protected" range (non-editable content control or section protected as a form). If this should be the case then you will be unable to use the AltChunk approach to integrate content from other sources.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 24, 2014 7:02 PM
    Moderator
  • Hi,

    Ok, I understand why my post is confusing. I will try to write a more complete and understandable description here. My scenario/challenge has changed a bit since my original post though.

    Scenario:

    I have a Word document (from now called Doc1) with various Content Controls. I also have a few other Word documents (from now called Content Documents) with various content (formatted in Word). What I need to do, is to populate each Content Control in Doc1 with content from the various Content Documents. I have logic to map each Content Control to one of the Content Documents.
    Also, it's important that I can set specific formatting and locking on each of the Content Controls.

    Where am I at right now?

    Everything works, except (a pretty important part) for:
    I'm not able to fetch the content of my Content Documents with formatting. I found an open source library that fetches the content in plain text, but that's simply not good enough in this case. I also need the formatting.

    So, I'm able to populate my Content Controls with content, but the content fetched from my Content Documents are in plain text, without any formatting, including line breaks.

    The code I use to populate the Content Controls is as follows: 

    var obj = sdt.Descendants<Word.Text>().FirstOrDefault();
    if (obj != null)
    {

        sdt.Descendants<Word.Text>().FirstOrDefault().Text = <plain text from other documents>;
    }

    So, what I'm looking for now, is how to fetch the content of my Content Documents as is, in other words, with all the formatting.

    Am I on the right track? I mentioned AltChunk in my original post. I'm currently not using it as I found an article on the internet that used the code I stated above.

    Like I said, I'm not getting any errors now, but I'm just not able to keep the formatting of the document that I'm fetching content from.

    Is my problem more clear and understandable now?

    Monday, January 27, 2014 12:24 PM
  • Hi Relapse

    The problem with any approach is that the information contained in document.xml may not carry (all) formatting with it. STYLE formattting is managed in styles.xml (The StylesDefinitions Part) and referred to via an rId (Word looks up the style id in the styles.xml file and applies the formatting it finds there).

    Any formatting applied directly to the text will come across, but NOT if you request only the TEXT nodes. You need the <w:p> (Paragraph) nodes WITH all child nodes in order to get formatting information. And if any of the formatting is controlled by a style, you'd need to

    1. Look up the style
    2. Determine whether the style is already present in your target doc
    3. If it is, decide whether to overwrite that formatting, or add the additional formatting
    4. If adding the additional formatting, the style's ID and Name must be altered so as not to conflict with the existing style
    5. The id information must be changed in the paragraph you're briniging in
    6. Both style and content must be properly appended in the target document

    This is work that Word does in the background when something is inserted into a document. When working with the Open XML SDK you don't have any of these automatisms added to the application interface, to make life easy for the user.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, January 27, 2014 5:00 PM
    Moderator
  • Hi,

    Thank you for your reply.

    I'm going to choose to forget about the formatting for now and rather just focus on copying the content from a Word document into a Content Control of a different Word document. I'm aiming at using the approach you mentioned, using the Paragraphs and their child elements.

    Could you provide some code on how to do this?

    Thanks!

    Tuesday, January 28, 2014 8:34 AM