Hi,
I have a document, which is produced by OCR software and which contains quite a lot of optional hyphen characters (decimal 31).
How can I programmatically replace all occurrences of this character with a regular hyphen (hard hyphen) or a soft hyphen (decimal 170).
Many thanks for your support
Kind regards
Patrick Vanbrabant
@natasjadegroot
To replace all occurrences of the optional hyphen character (decimal 31) in your document with a regular hyphen or a soft hyphen (decimal 170), you can use the Range.Replace
method provided by Aspose.Words. Here’s a clear solution:
Cause: The optional hyphen character (decimal 31) may appear in your document and needs to be replaced for proper formatting.
Solution: You can utilize the Range.Replace
method in C# to replace the optional hyphen with either a regular hyphen or a soft hyphen.
Next Steps: Use the following code snippet to implement the replacement in your document:
using Aspose.Words;
class Program
{
static void Main()
{
// Load your document
Document doc = new Document("path/to/your/document.docx");
// Replace optional hyphen (decimal 31) with a regular hyphen (-)
doc.Range.Replace("\u001F", "-", new FindReplaceOptions());
// Or replace with a soft hyphen (decimal 170)
// doc.Range.Replace("\u001F", "\u00AD", new FindReplaceOptions());
// Save the modified document
doc.Save("path/to/your/modified_document.docx");
}
}
In this code, "\u001F"
represents the optional hyphen character, and you can replace it with either a regular hyphen "-"
or a soft hyphen "\u00AD"
as needed. Make sure to adjust the file paths accordingly.
If you have any further questions or need additional assistance, feel free to ask!
Do I not need to update the layout of the document after this
@natasjadegroot No it is not required to updating page layout after find/replace operation.
The code does not find and replace the optional hyphens in the cells of a tables. There are multiple tables int he documetn as well. In these tables there are also these optional hyphen characters.
How does the code need to be changed to replace these characters in the tables as well.
Sorry my mistake. It is replaced inside the tables as well
It is working fine. Thanks
1 Like