We are using following code for this requirements.
protected void btn_Click(object sender, EventArgs e)
{
string File = “C:\FooterIssue.Docx”;
Aspose.Words.Document doc = new Aspose.Words.Document(File);
List<string> Tags = new List<string>();
Dictionary<string, string> aProductCost = new Dictionary<string, string>();
aProductCost.Add("PL_PRODUCT_COST", "$2,500");
aProductCost.Add("PL_ORDER_COST", "$2,000");
aProductCost.Add("PL_QUANTITY", "3");
aProductCost.Add("PL_TOTAL_COST", "$7,500");
List<Aspose.Words.Markup.StructuredDocumentTag> sdtNodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<Aspose.Words.Markup.StructuredDocumentTag>().ToList<Aspose.Words.Markup.StructuredDocumentTag>();
string Value = string.Empty;
foreach (Aspose.Words.Markup.StructuredDocumentTag cc in sdtNodes)
{
Value = aProductCost[cc.Title];
if (Value != null) //when found, apply the value.
{
mergeDataToContentControl(cc, Value);
}
}
foreach (Section sect in doc.Sections)
{
ProcessFooter(sect, HeaderFooterType.FooterFirst, "$");
ProcessFooter(sect, HeaderFooterType.FooterPrimary, "$");
ProcessFooter(sect, HeaderFooterType.FooterEven, "$");
ProcessFooter(sect, HeaderFooterType.FooterFirst, ",");
ProcessFooter(sect, HeaderFooterType.FooterPrimary, ",");
ProcessFooter(sect, HeaderFooterType.FooterEven, ",");
}
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
doc.Save(outStream, Aspose.Words.SaveFormat.Docx);
byte[] docBytes = null;
docBytes = outStream.ToArray();
FileStream output = new FileStream("C:\\FooterIssue1.Docx", FileMode.Create,
FileAccess.Write);
// Write byte array contents in the output file stream**
output.Write(docBytes, 0, docBytes.Length);
// Close output file
output.Close();
}
private static void ProcessFooter(Section sect, HeaderFooterType headerType, string pattern)
{
HeaderFooter footer = sect.HeadersFooters[headerType];
if (footer != null)
{
// Replace
footer.Range.Replace(pattern, "", false, false);
}
}
private void mergeDataToContentControl(Aspose.Words.Markup.StructuredDocumentTag tag, string value)
{
var paragraphs = tag.GetChildNodes(NodeType.Paragraph, true).ToArray();
if (paragraphs != null && paragraphs.Length > 0)
{
Run run = null;
Paragraph par = (Paragraph)paragraphs[0];
if (par.Runs != null && par.Runs.Count > 0)
{
run = (Run)par.Runs[0].Clone(true);
run.Text = value;
}
else
{
run = new Run(tag.Document, value);
}
par.Runs.Clear();
par.Runs.Add(run);
}
else //case: when there is no paragraph as part of the Content Control.
{
var runs = tag.GetChildNodes(NodeType.Run, true).ToArray();
if (runs != null && runs.Length > 0)
{
Run r = (Run)runs[0];
r.Text = string.Empty;
r.Text = value;
}
else
{
tag.AppendChild(new Run(tag.Document, value));
}
}
}