Aspose.Words.Examples.CSharp.Programming_Documents.Working_with_Fields.FieldsHelper.ConvertFieldsToStaticText C# (CSharp) Method

ConvertFieldsToStaticText() public static method

Converts any fields of the specified type found in the descendants of the node into static text.
public static ConvertFieldsToStaticText ( CompositeNode compositeNode, FieldType targetFieldType ) : void
compositeNode CompositeNode The node in which all descendants of the specified FieldType will be converted to static text.
targetFieldType FieldType The FieldType of the field to convert to static text.
return void
        public static void ConvertFieldsToStaticText(CompositeNode compositeNode, FieldType targetFieldType)
        {
            string originalNodeText = compositeNode.ToString(SaveFormat.Text); // ExSkip
            FieldsHelper helper = new FieldsHelper(targetFieldType);
            compositeNode.Accept(helper);

            Debug.Assert(originalNodeText.Equals(compositeNode.ToString(SaveFormat.Text)), "Error: Text of the node converted differs from the original"); // ExSkip
            foreach (Node node in compositeNode.GetChildNodes(NodeType.Any, true)) // ExSkip
                Debug.Assert(!(node is FieldChar && ((FieldChar)node).FieldType.Equals(targetFieldType)), "Error: A field node that should be removed still remains."); // ExSkip         
        }

Usage Example

        public static void Run()
        {
            //ExStart:ConvertFieldsInDocument
            // The path to the documents directory.
            string   dataDir  = RunExamples.GetDataDir_WorkingWithFields();
            string   fileName = "TestFile.doc";
            Document doc      = new Document(dataDir + fileName);

            // Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to static text.
            FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldIf);

            dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
            // Save the document with fields transformed to disk.
            doc.Save(dataDir);
            //ExEnd:ConvertFieldsInDocument
            Console.WriteLine("\nConverted fields to static text in the document successfully.\nFile saved at " + dataDir);
        }
All Usage Examples Of Aspose.Words.Examples.CSharp.Programming_Documents.Working_with_Fields.FieldsHelper::ConvertFieldsToStaticText