The objective is to replace the specified tags in a word document(.docx) using OpenXml.
Below is a sample of the word document structure
1) Document content:
2) Footer:
The code sample is as follows:
using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\Users\dev5setup\Desktop\DEPT_TEST.docx", true))
{
Dictionary<string, string> tagMappings = GetTagMappingsForTemplate();
IEnumerable<Paragraph> paras = doc.MainDocumentPart.Document.Descendants<Paragraph>();
foreach (Paragraph para in paras)
{
// get all runs under the paragraph and convert to be array
Run[] runArr = para.Descendants<Run>().ToArray();
// foreach each run
foreach (Run run in runArr)
{
string modifiedString = "";
int count = 0;
string innerText = run.InnerText;
#region replace tags
foreach (KeyValuePair<string, string> mapping in tagMappings)
{
if (count == 0)
{
modifiedString = run.InnerText.Replace(mapping.Key, mapping.Value);
}
else
{
modifiedString = modifiedString.Replace(mapping.Key, mapping.Value);
}
count++;
}
#endregion
// if the InnerText doesn't modify
if (modifiedString != run.InnerText)
{
Text t = new Text(modifiedString);
run.RemoveAllChildren<Text>();
run.AppendChild<Text>(t);
}
}
}
doc.MainDocumentPart.Document.Save();
//Update the word document footer section
FooterPart pp = doc.MainDocumentPart.FooterParts.ElementAt(0);
IEnumerable<Paragraph> footerParas = pp.Footer.Descendants<Paragraph>();
foreach (Paragraph footerPara in footerParas)
{
// get all runs under the paragraph and convert to an array
Run[] runArr = footerPara.Descendants<Run>().ToArray();
// foreach each run
foreach (Run run in runArr)
{
string modifiedString = "";
int count = 0;
string innerText = run.InnerText;
#region replace tags
foreach (KeyValuePair<string, string> mapping in tagMappings)
{
if (count == 0)
{
modifiedString = run.InnerText.Replace(mapping.Key, mapping.Value);
}
else
{
modifiedString = modifiedString.Replace(mapping.Key, mapping.Value);
}
count++;
}
#endregion
// if the InnerText doesn't modify
if (modifiedString != run.InnerText)
{
Text t = new Text(modifiedString);
run.RemoveAllChildren<Text>();
run.AppendChild<Text>(t);
}
}
}
}
Below is a sample of the word document structure
1) Document content:
2) Footer:
The code sample is as follows:
using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\Users\dev5setup\Desktop\DEPT_TEST.docx", true))
{
Dictionary<string, string> tagMappings = GetTagMappingsForTemplate();
IEnumerable<Paragraph> paras = doc.MainDocumentPart.Document.Descendants<Paragraph>();
foreach (Paragraph para in paras)
{
// get all runs under the paragraph and convert to be array
Run[] runArr = para.Descendants<Run>().ToArray();
// foreach each run
foreach (Run run in runArr)
{
string modifiedString = "";
int count = 0;
string innerText = run.InnerText;
#region replace tags
foreach (KeyValuePair<string, string> mapping in tagMappings)
{
if (count == 0)
{
modifiedString = run.InnerText.Replace(mapping.Key, mapping.Value);
}
else
{
modifiedString = modifiedString.Replace(mapping.Key, mapping.Value);
}
count++;
}
#endregion
// if the InnerText doesn't modify
if (modifiedString != run.InnerText)
{
Text t = new Text(modifiedString);
run.RemoveAllChildren<Text>();
run.AppendChild<Text>(t);
}
}
}
doc.MainDocumentPart.Document.Save();
//Update the word document footer section
FooterPart pp = doc.MainDocumentPart.FooterParts.ElementAt(0);
IEnumerable<Paragraph> footerParas = pp.Footer.Descendants<Paragraph>();
foreach (Paragraph footerPara in footerParas)
{
// get all runs under the paragraph and convert to an array
Run[] runArr = footerPara.Descendants<Run>().ToArray();
// foreach each run
foreach (Run run in runArr)
{
string modifiedString = "";
int count = 0;
string innerText = run.InnerText;
#region replace tags
foreach (KeyValuePair<string, string> mapping in tagMappings)
{
if (count == 0)
{
modifiedString = run.InnerText.Replace(mapping.Key, mapping.Value);
}
else
{
modifiedString = modifiedString.Replace(mapping.Key, mapping.Value);
}
count++;
}
#endregion
// if the InnerText doesn't modify
if (modifiedString != run.InnerText)
{
Text t = new Text(modifiedString);
run.RemoveAllChildren<Text>();
run.AppendChild<Text>(t);
}
}
}
}
No comments:
Post a Comment