-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathProgram.cs
66 lines (65 loc) · 3.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
//Open the template Word document.
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic))
{
string htmlFilePath = @"Data/File.html";
//Check if the HTML content is valid.
bool isvalidHTML = document.LastSection.Body.IsValidXHTML(htmlFilePath, XHTMLValidationType.None);
if (isvalidHTML)
{
//Define the variable containing the text to search within the paragraph.
string variable = "Youth mountain bike";
//Find the first occurrence of a particular text in the document
TextSelection textSelection = document.Find(variable, true, true);
//Get the found text as single text range
WTextRange textRange = textSelection.GetAsOneRange();
// Get the paragraph containing the found text range
WParagraph paragraph = textRange.OwnerParagraph;
//Get the next sibling element of the current paragraph.
TextBodyItem nextSibling = paragraph.NextSibling as TextBodyItem;
//Get the index of the current paragraph within its parent text body.
int sourceIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
//Clear all child entities within the paragraph.
paragraph.ChildEntities.Clear();
//Get the list style name from the paragraph.
string listStyleName = paragraph.ListFormat.CurrentListStyle.Name;
//Get the current list level number.
int listLevelNum = paragraph.ListFormat.ListLevelNumber;
//Append HTML content from the specified file to the paragraph.
paragraph.AppendHTML(File.ReadAllText(Path.GetFullPath(htmlFilePath)));
//Reapply the original list style to the paragraph.
paragraph.ListFormat.ApplyStyle(listStyleName);
//Reapply the original list level number.
paragraph.ListFormat.ListLevelNumber = listLevelNum;
//Determine the index of the next sibling if it exists.
int nextSiblingIndex = nextSibling != null ? nextSibling.OwnerTextBody.ChildEntities.IndexOf(nextSibling) : -1;
//Apply the same list style to newly added paragraphs from the HTML content.
for (int k = sourceIndex; k < paragraph.OwnerTextBody.Count; k++)
{
//Stop applying the style if the next sibling is reached.
if (nextSiblingIndex != -1 && k == nextSiblingIndex)
{
break;
}
Entity entity = paragraph.OwnerTextBody.ChildEntities[k];
//Apply the list style only if the entity is a paragraph.
if (entity is WParagraph)
{
(entity as WParagraph).ListFormat.ApplyStyle(listStyleName);
}
else
{
break;
}
}
}
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the modified Word document to the output file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}