Hello, dear friends.
I Have a Word templates with tags (like <table>) this tags I identify it like normal text. So my doubt is: I have to replace this tags with RTF Formated Text from a DataBase. There's some way to do that?
Regards
Document doc = new Document();
doc.LoadFromFile(@"test.docx");
TextSelection[] selections = doc.FindAllString("<tag>", false, true);
foreach (TextSelection selection in selections)
{
TextRange range = selection.GetAsOneRange();
int index = range.OwnerParagraph.ChildObjects.IndexOf(range);
//create a temporary section
Section tempSection = doc.AddSection();
String rtfString = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";
//add a paragraph to append rtf
Paragraph par = tempSection.AddParagraph();
par.AppendRTF(rtfString);
foreach (Paragraph p in tempSection.Paragraphs)
{
foreach (DocumentObject obj in p.ChildObjects)
{
range.OwnerParagraph.ChildObjects.Insert(index, obj.Clone());
}
}
range.OwnerParagraph.ChildObjects.Remove(range);
//remove the temp section
doc.Sections.Remove(tempSection);
}
doc.SaveToFile("result.docx", FileFormat.Docx);