Hi Tahir,
I tried implementing based on the given code, but still i am not able to generate the file. Instead the code is not returning any generated file and the code is iterating.
Attached my template. Can you help me what is the issue in this.Below is the code
var MngLetter_Issues = Server.MapPath("~/App_Data/Template/Management Letter.docx");
Document doc_MngLetter = new Document(MngLetter_Issues);
doc_MngLetter.Range.Replace("[Matters Inclusion]", "Summary for year ended " + DateTime.Now.ToShortDateString() + ".", false, false);
int issueCounter = 0;
foreach (var issue in selectedFundIssues)
{
doc_MngLetter.Range.Replace("[Section/Regulation number] – [Description]", issue.Issue.SISSection.SISSection + "-" + issue.Issue.IssueName, false, false);
doc_MngLetter.Range.Replace("[Insert succinct summary of matter]", issue.Issue.ManagementDescription, false, false);
// rule set to Insert conclusion of impact on audit opinion from below list:
doc_MngLetter.Range.Replace("[Insert conclusion of impact on audit opinion from below list:]", "This event has not resulted in a qualified audit opinion and is not reportable under the ATO reporting guidelines.", false, false);
// rule set to Insert Action required from option below
if (issueCounter < (selectedFundIssues.Count - 1))
{
String[] newcontents = { issue.Issue.Mgmt_ActionRequired, "[Section/Regulation number] – [Description]", "[Insert succinct summary of matter]", "[Insert conclusion of impact on audit opinion from below list:]", "Action Required", "[Insert Action required from option below]" };
Regex regex = new Regex("[Insert Action required from option below]", RegexOptions.IgnoreCase);
doc_MngLetter.Range.Replace(regex, new ReplaceEvaluator(newcontents), false);
}
}
else
{
if (issue.IsManagement && (issue.IsAuditOpinion_PartA || issue.IsAuditOpinion_PartB || (issue.IsAuditOpinion_PartA && issue.IsAuditOpinion_PartB)) && !issue.IsRestricted)
doc_MngLetter.Range.Replace("[Insert Action required from option below]", issue.Issue.Mgmt_ActionRequired, false, false);
}
issueCounter++;
}
}
else
{
// doc_MngLetter.Range.Replace("This sentence only applies if there are no issues. Default.", "", false, false);
doc_MngLetter.Range.Replace("[Matters Inclusion]", "End" + DateTime.Now.ToShortDateString() + ".", false, false);
doc_MngLetter.Range.Replace("[Section/Regulation number] – [Description]", string.Empty, false, false);
doc_MngLetter.Range.Replace("[Insert succinct summary of matter]", string.Empty, false, false);
// rule 1
doc_MngLetter.Range.Replace("[Insert conclusion of impact on audit opinion from below list:]", string.Empty, false, false);
doc_MngLetter.Range.Replace("Action Required", string.Empty, false, false);
doc_MngLetter.Range.Replace("[Insert from option below]", string.Empty, false, false);
}
MemoryStream outStream = new MemoryStream();
doc_MngLetter.Save(outStream, SaveFormat.Docx);
public class ReplaceEvaluator : IReplacingCallback
{
String[] newcontents;
public ReplaceEvaluator(String[] contents)
{
newcontents = contents;
}
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = remainingLength - currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
}
while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
SplitRun((Run)currentNode, remainingLength);
runs.Add(currentNode);
}
// Create Document Buidler
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
builder.MoveTo((Run)runs[runs.Count - 1]);
builder.Write(newcontents[0]);
builder.Font.Bold = true;
builder.Write(ControlChar.LineBreak + ControlChar.LineBreak + newcontents[1]);
builder.Font.Bold = false;
builder.Writeln(newcontents[2]);
builder.Writeln(newcontents[3]);
builder.Font.Italic = true;
builder.Writeln(newcontents[4]);
builder.Font.Italic = false;
builder.Writeln(newcontents[5]);
foreach (Run run in runs)
run.Remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}