void Main()
{
InitCoreComponent();
var doc = new Aspose.Words.Document(@"C:\Users\admin\Desktop\demo.docx");
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
Range paraRange = para.Range;
paraRange.Replace("房地产评估规划测绘咨询有限公司", "test");
}
doc.Save(@"C:\Users\admin\Desktop\result.docx");
}
It appears that you are experiencing difficulties with replacing specific content in a Word document using Aspose.Words for .NET. The issue may stem from the structure of the document, particularly if the text you want to replace is embedded within complex elements such as tables, headers, or footers.
Cause: The problem could be due to hidden text, formatting issues, or the text being located in non-standard areas of the document.
Solution: To effectively replace the text, follow these steps:
Check for Hidden Text: Verify that the text you want to replace is not hidden or formatted in a way that makes it unrecognizable to the replace function.
Use Regular Expressions: If the text has variations (like different cases or extra spaces), utilize regular expressions for a more flexible search. The Regex class can be employed in your replace method.
Iterate Through All Nodes: Instead of limiting your search to paragraphs, iterate through all nodes in the document. This ensures that you check every possible location where the text might exist.
Use FindReplaceOptions: Customize your find and replace operation with the FindReplaceOptions class. This allows you to specify options such as case sensitivity and whether to match whole words.
Here’s a modified version of your code that incorporates these suggestions:
void Main()
{
InitCoreComponent();
var doc = new Aspose.Words.Document(@"C:\Users\admin\Desktop\demo.docx");
// Create FindReplaceOptions to customize the replace operation
FindReplaceOptions options = new FindReplaceOptions
{
MatchCase = false, // Set to true if case sensitivity is required
FindWholeWordsOnly = true // Set to true if you want to match whole words only
};
// Replace text throughout the entire document
doc.Range.Replace("房地产评估规划测绘咨询有限公司", "test", options);
doc.Save(@"C:\Users\admin\Desktop\result.docx");
}
This code will search for the specified text throughout the entire document and replace it with “test”. Adjust the MatchCase and FindWholeWordsOnly properties according to your requirements.
Next Steps: Implement the provided code and test it with your document. If you encounter any further issues or have additional questions, please feel free to reach out for further assistance.
@sullivan
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-28545
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.
@sullivan We have managed to reproduce the problem on our side. The issue has been logged as WORDSNET-28545. We will keep you updated and let you know once it is resolved or we have more information for you.
@sullivan We have completed analyzing the issue. The problem occurs because there are shapes among the words of the problematic text located after 房, 地 and 产:
In order for such text to be replaced correctly, these images within the text must be ignored.
You can use the appropriate option to achieve desired result:
FindReplaceOptions options = new FindReplaceOptions { IgnoreShapes = true };
int matches = doc.Range.Replace("房地产评估规划测绘咨询有限公司", "test", options);