I need to find exact word which I want to replace from word document using Open XML in C#. the purpose of replacing the personal details of user with some special character so that its not visible to reader.
For an example, the user has address mentioned in his form, which is stored in database he also has one word document uploaded, the word document also contain following type of string which matches his address. my purpose is to match the address with ###
sign so that other users cant see the address. e.g.
"422, Plot no. 1000/A, The Moon Residency II, Shree Nagrik Co. Op. Society, Sardarnagar, Ahmedabad.
Looking for an opportunity that surpasses in making me a personality that influences the masses and that too effectively. Organizationally, I would strive to work at a single
place with no professional switches being made and would love to work in an environment that demands constant evolution with variable domains incorporated to deal
with."
I want to replace "Co", "Op" with "#" sign. My output would be this:
"422, Plot no. 1000/A, The Moon Residency II, Shree Nagrik #. #. Society, Sardarnagar, Ahmedabad.
Looking for an opportunity that surpasses in making me a personality that influences the masses and that too effectively. Organizationally, I would strive to work at a single
place with no professional switches being made and would love to work in an environment that demands constant evolution with variable domains incorporated to deal
with. "
Now i have several questions 1. How can i search for whole word, right now my code replaces opportunity word with ##portunity since this word has Op. Same with Constant it replaces with ##nstant. I need to replace if the whole word matches.
- how can i match the whole line in the word or may be the whole address, the address should be replace as whole, if not possible, it should replace 70-80%.
Currently my code is as bellow to replace word into word file.
MemoryStream m = new System.IO.MemoryStream();
//strResumeName contain my word file url
m = objBlob.GetResumeFile(strResumeName);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(m, true))
{
body = wordDoc.MainDocumentPart.Document.Body;
colT = body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>();
foreach (DocumentFormat.OpenXml.Wordprocessing.Text c in colT)
{
if (c.InnerText.Trim() != String.Empty)
{
sb.Append(c.InnerText.Trim() + " ");
}
}
string[] strParts = sb.ToString().Split(' ');
HyperLinkList = HyperLinksList(wordDoc);
redactionTags = GetReductionstrings(strParts);
}
using (Novacode.DocX document = Novacode.DocX.Load(m))
{
//objCandidateLogin.Address contain my address
if (!String.IsNullOrEmpty(objCandidateLogin.Address))
{
string[] strParts = objCandidateLogin.Address.Replace(",", " ").Split(' ');
for (int I = 0; I <= strParts.Length - 1; I++)
{
if (strParts[I].Trim().Length > 1)
{
document.ReplaceText(strParts[I].Trim(), "#############", false, RegexOptions.IgnoreCase);
}
}
}
}