Lock Toggle Field Codes In Word

Is it possible to stop the toggle of field codes (right-click, toggle field codes, or shorctuts) in a word document using ASPOSE.Words, or by editing the document beforehand in some way? I know that if I restrict editing, I can no longer toggle the field codes, but I want users to be able to edit the text within the document.

Or if that is not possible, I would be open to updating the field and then finding a way to remove the field while keeping the result content?

@amattice You can use Document.UnlinkFields method to replace all fields in the document with simple text.
Also, you can unlink only particular types of fields. For example the following code unlinks all field except PAGE fields:

Document doc = new Document(@"C:\Temp\in.docx");

doc.Range.Fields.Where(f => f.Type!=FieldType.FieldPage).ToList()
    .ForEach(f => f.Unlink());

doc.Save(@"C:\Temp\out.docx");

@alexey.noskov could you provide insight as to why I would be getting an InvalidOperationException error when running Unlink? The error provided is “Document structure was changed”

public async Task ReplaceIncludeTextFieldsWithNewContent(Document document, ManifestModel manifest)
{
    foreach (Field field in document.Range.Fields)
    {
        if (field.Type == FieldType.FieldIncludeText)
        {
            FieldIncludeText includeTextField = (FieldIncludeText)field;
            string fieldCode = field.GetFieldCode();

            var placeholderNameMatch = Regex.Match(fieldCode, "\"([^\"\\:]+)\\:.*?\"");
            if (placeholderNameMatch.Success)
            {
                string placeholderName = placeholderNameMatch.Groups[1].Value;
                var placeholder = manifest.Placeholders.FirstOrDefault(p => p.Placeholder == placeholderName);
                if (placeholder != null && !string.IsNullOrWhiteSpace(placeholder.ContentUrl))
                {
                    string tempFilePath = await _fileManagementService.DownloadFileToTempAsync(placeholder.ContentUrl);
                    includeTextField.SourceFullName = tempFilePath;
                    includeTextField.Update();
                    //includeTextField.Unlink();
                }
            }
        }
    }
}

@amattice It looks like a bug.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26802

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

For now you can unlink INCLUDETEXT in a separate loop. Something like this:

Document doc = new Document(@"C:\Temp\in.docx");

foreach(Field f in doc.Range.Fields)
{
    if (f.Type == FieldType.FieldIncludeText)
    {
        FieldIncludeText includeText = (FieldIncludeText)f;
        // Update INCLUDETEXT field source.
        includeText.SourceFullName = @"C:\Temp\src2.txt";
        includeText.Update();
    }
}

doc.Range.Fields.Where(f => f.Type == FieldType.FieldIncludeText).ToList()
    .ForEach(f => f.Unlink());

doc.Save(@"C:\Temp\out.docx");

Thanks for your help! I moved it outside the original loop and it works!

1 Like