Document document = new Document(@"F:\sample.docx");
List<Paragraph> replacement = new List<Paragraph>();
//create a temp section to contains HTML
Section tempSection = document.AddSection();
Paragraph p4 = tempSection.AddParagraph();
p4.AppendHTML("your HTML");
foreach (var par in tempSection.Paragraphs)
{
Paragraph para = par as Paragraph;
replacement.Add(para);
}
TextSelection[] selections = document.FindAllString("{{{wordToReplace}}}", false, true);
List<TextRangeLocation> locations = new List<TextRangeLocation>();
foreach (TextSelection selection in selections)
{
locations.Add(new TextRangeLocation(selection.GetAsOneRange()));
}
locations.Sort();
foreach (TextRangeLocation location in locations)
{
Replace(location, replacement);
}
//remove the temp section
document.Sections.Remove(tempSection);
document.SaveToFile("11052.docx",FileFormat.Docx);
}
private static void Replace(TextRangeLocation location, IList<Paragraph> replacement)
{
//will be replaced
TextRange textRange = location.Text;
//textRange index
int index = location.Index;
//owener paragraph
Paragraph paragraph = location.Owner;
//owner text body
Body sectionBody = paragraph.OwnerTextBody;
//get the index of paragraph in section
int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);
int replacementIndex = -1;
if (index == 0)
{
//remove
paragraph.ChildObjects.RemoveAt(0);
replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
}
else if (index == paragraph.ChildObjects.Count - 1)
{
paragraph.ChildObjects.RemoveAt(index);
replacementIndex = paragraphIndex + 1;
}
else
{
//split owner paragraph
Paragraph paragraph1 = (Paragraph)paragraph.Clone();
while (paragraph.ChildObjects.Count > index)
{
paragraph.ChildObjects.RemoveAt(index);
}
int i = 0;
int count = index + 1;
while (i < count)
{
paragraph1.ChildObjects.RemoveAt(0);
i += 1;
}
sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);
replacementIndex = paragraphIndex + 1;
}
//insert replacement
for (int i = 0; i <= replacement.Count - 1; i++)
{
sectionBody.ChildObjects.Insert(replacementIndex + i, replacement[i].Clone());
}
}
public class TextRangeLocation : IComparable<TextRangeLocation>
{
public TextRangeLocation(TextRange text)
{
this.Text = text;
}
public TextRange Text
{
get { return m_Text; }
set { m_Text = value; }
}
private TextRange m_Text;
public Paragraph Owner
{
get { return this.Text.OwnerParagraph; }
}
public int Index
{
get { return this.Owner.ChildObjects.IndexOf(this.Text); }
}
public int CompareTo(TextRangeLocation other)
{
return -(this.Index - other.Index);
}
}
<table class='widthStyle' style='width:100%;'><tbody><tr><td style='vertical-align: top;'><ul><li>Boat Cover</li><li>Propeller</li><li>Bilge Pump</li><li>Inboard Motors</li></ul></td><td style='vertical-align: top;'><ul style='vertical-align:top;'><li>Canvas Top</li><li>Step Wedges</li><li>Dock Ties</li></ul></td><td style='vertical-align: top;'><ul><li>Fire Extinguisher</li><li>Reboarding Step</li><li>Jet Ski Cover</li></ul></td></tr></tbody></table>
Document document = new Document(@"F:\sample.docx");
//collect document object, it could get paragraph, table and so on.
List<DocumentObject> replacement = new List<DocumentObject>();
Section tempSection = document.AddSection();
Paragraph p4 = tempSection.AddParagraph();
p4.AppendHTML("your HTML");
foreach (var obj in tempSection.Body.ChildObjects)
{
DocumentObject O = obj as DocumentObject;
replacement.Add(O);
}
TextSelection[] selections = document.FindAllString("{{{wordToReplace}}}", false, true);
List<TextRangeLocation> locations = new List<TextRangeLocation>();
foreach (TextSelection selection in selections)
{
locations.Add(new TextRangeLocation(selection.GetAsOneRange()));
}
locations.Sort();
foreach (TextRangeLocation location in locations)
{
ReplaceObj(location, replacement);
}
//remove the temp section
document.Sections.Remove(tempSection);
document.SaveToFile("11052-table.docx", FileFormat.Docx);
}
private static void ReplaceObj(TextRangeLocation location, List<DocumentObject> replacement)
{
//will be replaced
TextRange textRange = location.Text;
//textRange index
int index = location.Index;
//owener paragraph
Paragraph paragraph = location.Owner;
//owner text body
Body sectionBody = paragraph.OwnerTextBody;
//get the index of paragraph in section
int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);
int replacementIndex = -1;
if (index == 0)
{
//remove
paragraph.ChildObjects.RemoveAt(0);
replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
}
else if (index == paragraph.ChildObjects.Count - 1)
{
paragraph.ChildObjects.RemoveAt(index);
replacementIndex = paragraphIndex + 1;
}
else
{
//split owner paragraph
Paragraph paragraph1 = (Paragraph)paragraph.Clone();
while (paragraph.ChildObjects.Count > index)
{
paragraph.ChildObjects.RemoveAt(index);
}
int i = 0;
int count = index + 1;
while (i < count)
{
paragraph1.ChildObjects.RemoveAt(0);
i += 1;
}
sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);
replacementIndex = paragraphIndex + 1;
}
//insert replacement
for (int i = 0; i <= replacement.Count - 1; i++)
{
sectionBody.ChildObjects.Insert(replacementIndex + i, replacement[i].Clone());
}
}
public class TextRangeLocation : IComparable<TextRangeLocation>
{
public TextRangeLocation(TextRange text)
{
this.Text = text;
}
public TextRange Text
{
get { return m_Text; }
set { m_Text = value; }
}
private TextRange m_Text;
public Paragraph Owner
{
get { return this.Text.OwnerParagraph; }
}
public int Index
{
get { return this.Owner.ChildObjects.IndexOf(this.Text); }
}
public int CompareTo(TextRangeLocation other)
{
return -(this.Index - other.Index);
}
}
public class SpireHelper
{
public static Document ReplaceTextWithHtml(Document document, string wordToReplace, string html)
{
List<Paragraph> replacement = new List<Paragraph>();
//create a temp section to contains HTML
Section tempSection = document.AddSection();
Paragraph p4 = tempSection.AddParagraph();
p4.AppendHTML(html);
foreach (var par in tempSection.Paragraphs)
{
Paragraph para = par as Paragraph;
replacement.Add(para);
}
TextSelection[] selections = document.FindAllString("[" + wordToReplace + "]", false, true);
List<TextRangeLocation> locations = new List<TextRangeLocation>();
foreach (TextSelection selection in selections)
{
locations.Add(new TextRangeLocation(selection.GetAsOneRange()));
}
locations.Sort();
foreach (TextRangeLocation location in locations)
{
Replace(location, replacement);
}
//remove the temp section
document.Sections.Remove(tempSection);
return document;
}
private static void Replace(TextRangeLocation location, IList<Paragraph> replacement)
{
//will be replaced
TextRange textRange = location.Text;
//textRange index
int index = location.Index;
//owener paragraph
Paragraph paragraph = location.Owner;
//owner text body
Body sectionBody = paragraph.OwnerTextBody;
//get the index of paragraph in section
int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);
int replacementIndex = -1;
if (index == 0)
{
//remove
paragraph.ChildObjects.RemoveAt(0);
replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
}
else if (index == paragraph.ChildObjects.Count - 1)
{
paragraph.ChildObjects.RemoveAt(index);
replacementIndex = paragraphIndex + 1;
}
else
{
//split owner paragraph
Paragraph paragraph1 = (Paragraph)paragraph.Clone();
while (paragraph.ChildObjects.Count > index)
{
paragraph.ChildObjects.RemoveAt(index);
}
int i = 0;
int count = index + 1;
while (i < count)
{
paragraph1.ChildObjects.RemoveAt(0);
i += 1;
}
sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);
replacementIndex = paragraphIndex + 1;
}
//insert replacement
for (int i = 0; i <= replacement.Count - 1; i++)
{
sectionBody.ChildObjects.Insert(replacementIndex + i, replacement[i].Clone());
}
}
}
public class TextRangeLocation : IComparable<TextRangeLocation>
{
public TextRangeLocation(TextRange text)
{
this.Text = text;
}
public TextRange Text
{
get { return m_Text; }
set { m_Text = value; }
}
private TextRange m_Text;
public Paragraph Owner
{
get { return this.Text.OwnerParagraph; }
}
public int Index
{
get { return this.Owner.ChildObjects.IndexOf(this.Text); }
}
public int CompareTo(TextRangeLocation other)
{
return -(this.Index - other.Index);
}
}
//Html
<div class="ExternalClass6EC8314D7A06441DA9D40EEE1F113548">
<p style="margin-left: 0in; margin-right: 0in;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<u>Kính gửi:</u>         -           BAN LÃNH ĐẠO </strong></span></span></p>
<ul>
<li style="text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<em>We often come across a scenario where we need to merge data to the merge fields which are created by some others<br/></em></span></span></li>
<li style="text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<em>The MailMerge class in Spire.Doc.Reporting namespace exposes the following methods which return a collection of merge field names or group (region) names in a word document<br/></em></span></span></li>
</ul>
<p style="margin-left: 4.5pt; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<p style="margin-left: 4.5pt; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">We often come across a scenario where we need to merge data to the merge fields which are created by some others, and we are not sure about the merge fields’ names. So in order to accomplish the mail merge purpose, first we need to read the names of all the merge fields<br/> </span></span></p>
<p style="margin-left: 4.5pt; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<table align="center" border="1" cellspacing="0" class="Table" style="border-collapse: collapse; border: 1pt solid windowtext; width: 475.4pt;">
<tbody>
<tr>
<td style="height: 23.3pt; width: 50.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">Num<br/></p>
</td>
<td style="height: 23.3pt; width: 76.3pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">Name</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 78.75pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">No.</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 68.45pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">Amount</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 91.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">UP</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 109.7pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">Total</span></strong></span></span></p>
</td>
</tr>
<tr>
<td style="height: 15.4pt; width: 50.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">1</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 76.3pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">Đường thô</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 78.75pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">120015A, 120017A</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 68.45pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">22,500</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 91.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">403.98</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 109.7pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">1,711,800</span></span></span></p>
</td>
</tr>
<tr>
<td style="height: 16.95pt; width: 50.6pt;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></td>
<td style="height: 16.95pt; width: 76.3pt;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></td>
<td style="height: 16.95pt; width: 78.75pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;"></span></span></span></p>
</td>
<td style="height: 16.95pt; width: 68.45pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;"></span></span></span></p>
</td>
<td style="height: 16.95pt; width: 91.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">TỔNG CỘNG</span></strong></span></span></p>
</td>
<td style="height: 16.95pt; width: 109.7pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">1,711,800</span><span style="color: black;"></span></strong></span></span></p>
</td>
</tr>
</tbody>
</table>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">         Kính trình Ban lãnh đạo thuận duyệt chi phí trên để P. KDQT triển khai thực hiện đảm bảo đúng lịch nhập hàng.</span></span></p>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">   Trân trọng kính trình./</span></span></p>
</div>
<br/>
Document doc = new Document("test.docx");
string str = "good";
string html = "your html";
Document docHtml = new Document();
docHtml.AddSection().AddParagraph().AppendHTML(html);
IDocument replaceDoc = docHtml as IDocument;
doc.Replace(str, replaceDoc, false, true);
doc.SaveToFile("result.pdf", FileFormat.PDF);
<div class="ExternalClass6EC8314D7A06441DA9D40EEE1F113548">
<p style="margin-left: 0in; margin-right: 0in;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<u>Kính gửi:</u>         -           BAN LÃNH ĐẠO </strong></span></span></p>
<ul>
<li style="text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<em>We often come across a scenario where we need to merge data to the merge fields which are created by some others<br/></em></span></span></li>
<li style="text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<em>The MailMerge class in Spire.Doc.Reporting namespace exposes the following methods which return a collection of merge field names or group (region) names in a word document<br/></em></span></span></li>
</ul>
<p style="margin-left: 4.5pt; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<p style="margin-left: 4.5pt; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">We often come across a scenario where we need to merge data to the merge fields which are created by some others, and we are not sure about the merge fields’ names. So in order to accomplish the mail merge purpose, first we need to read the names of all the merge fields<br/> </span></span></p>
<p style="margin-left: 4.5pt; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<table align="center" border="1" cellspacing="0" class="Table" style="border-collapse: collapse; border: 1pt solid windowtext; width: 475.4pt;">
<tbody>
<tr>
<td style="height: 23.3pt; width: 50.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">Num<br/></p>
</td>
<td style="height: 23.3pt; width: 76.3pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">Name</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 78.75pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">No.</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 68.45pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">Amount</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 91.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">UP</span></strong></span></span></p>
</td>
<td style="height: 23.3pt; width: 109.7pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">Total</span></strong></span></span></p>
</td>
</tr>
<tr>
<td style="height: 15.4pt; width: 50.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">1</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 76.3pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">Đường thô</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 78.75pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">120015A, 120017A</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 68.45pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">22,500</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 91.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">403.98</span></span></span></p>
</td>
<td style="height: 15.4pt; width: 109.7pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;">1,711,800</span></span></span></p>
</td>
</tr>
<tr>
<td style="height: 16.95pt; width: 50.6pt;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></td>
<td style="height: 16.95pt; width: 76.3pt;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></td>
<td style="height: 16.95pt; width: 78.75pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;"></span></span></span></p>
</td>
<td style="height: 16.95pt; width: 68.45pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<span style="color: black;"></span></span></span></p>
</td>
<td style="height: 16.95pt; width: 91.6pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">TỔNG CỘNG</span></strong></span></span></p>
</td>
<td style="height: 16.95pt; width: 109.7pt;">
<p style="margin-left: 0in; margin-right: 0in; text-align: center;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">
<strong>
<span style="color: black;">1,711,800</span><span style="color: black;"></span></strong></span></span></p>
</td>
</tr>
</tbody>
</table>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">         Kính trình Ban lãnh đạo thuận duyệt chi phí trên để P. KDQT triển khai thực hiện đảm bảo đúng lịch nhập hàng.</span></span></p>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;"></span></span></p>
<p style="margin-left: 0in; margin-right: 0in; text-align: justify;">
<span style="font-size: 14px;">
<span style="font-family: "times new roman", times, serif;">   Trân trọng kính trình./</span></span></p>
</div>
<br/>