Replace text in equations

hello, I need to replace certain values in formulas.
The problem is that the Replace method does not change the value.

Code:

int main() {
  auto doc = System::MakeObject<Document>(u"input.docx");
  System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
  for (int i = 0; i < doc->GetChildNodes(NodeType::OfficeMath, true)->get_Count(); i++) {

    auto officeMath = System::ExplicitCast<OfficeMath>(doc->GetChildNodes(NodeType::OfficeMath, true)->idx_get(i));
    
    // Does not work
    officeMath->get_Range()->Replace(u"pogoda", u"ya", options);

    // Outputs equations (on screenshot)
    std::cout << officeMath->GetText() << std::endl;
  }

  doc->Save(u"output.docx");
  return 0;
}

input.docx (13,2 КБ)

@danyapogoda

To replace text in equations using Aspose.Words, you need to ensure that you are correctly accessing and modifying the OfficeMath objects. The Replace method should work, but it may not be functioning as expected due to the way the text is structured within the OfficeMath object.

Here’s a refined approach to replace text in equations:

  1. Access the OfficeMath Nodes: You are already doing this correctly by iterating through the OfficeMath nodes in your document.

  2. Use the Correct Replace Method: Ensure that the FindReplaceOptions is set up correctly. You might want to check if the options object is configured to handle case sensitivity or whole word matching, depending on your needs.

  3. Check the Text Structure: Sometimes, the text in equations may not be a simple string. You may need to explore the structure of the OfficeMath object to find the specific text elements you want to replace.

Here’s an example of how you might modify your code:

int main() {
    auto doc = System::MakeObject<Document>(u"input.docx");
    System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();

    for (int i = 0; i < doc->GetChildNodes(NodeType::OfficeMath, true)->get_Count(); i++) {
        auto officeMath = System::ExplicitCast<OfficeMath>(doc->GetChildNodes(NodeType::OfficeMath, true)->idx_get(i));

        // Ensure the options are set correctly if needed
        options->set_IgnoreCase(true); // Example option

        // Attempt to replace the text
        officeMath->get_Range()->Replace(u"pogoda", u"ya", options);

        // Output the modified equation text
        std::cout << officeMath->GetText() << std::endl;
    }

    doc->Save(u"output.docx");
    return 0;
}

Make sure to check the output after running this code to see if the text replacement is successful. If it still does not work, you may need to inspect the specific structure of the OfficeMath content to ensure that the text you are trying to replace is accessible in the way you expect.

If you continue to face issues, consider reviewing the examples provided in the Aspose documentation for working with OfficeMath, as they may offer additional insights or methods for handling text replacements effectively.

@danyapogoda
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-28249

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.