-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from snehasf3509/main
Samples for replacing text with chart, hyperlink and TOC
- Loading branch information
Showing
13 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...d-Replace/Find-and-replace-text-with-TOC/.NET standard/Find-and-replace-text-with-TOC.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.4.33213.308 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-replace-text-with-TOC", "Find-and-replace-text-with-TOC\Find-and-replace-text-with-TOC.csproj", "{23ABDEA3-F208-4355-B4A4-D17CBC963A16}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{23ABDEA3-F208-4355-B4A4-D17CBC963A16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{23ABDEA3-F208-4355-B4A4-D17CBC963A16}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{23ABDEA3-F208-4355-B4A4-D17CBC963A16}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{23ABDEA3-F208-4355-B4A4-D17CBC963A16}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {632BD348-C0F4-4909-B134-1554F75A6F1B} | ||
EndGlobalSection | ||
EndGlobal |
Binary file added
BIN
+12 KB
...nd-and-replace-text-with-TOC/.NET standard/Find-and-replace-text-with-TOC/Data/Input.docx
Binary file not shown.
13 changes: 13 additions & 0 deletions
13
...th-TOC/.NET standard/Find-and-replace-text-with-TOC/Find-and-replace-text-with-TOC.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>Find_and_replace_text_with_TOC</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
57 changes: 57 additions & 0 deletions
57
...ce/Find-and-replace-text-with-TOC/.NET standard/Find-and-replace-text-with-TOC/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Syncfusion.DocIO; | ||
using Syncfusion.DocIO.DLS; | ||
using Syncfusion.DocIORenderer; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Find_and_replace_text_with_TOC | ||
{ | ||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
//Opens an existing Word document. | ||
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) | ||
{ | ||
TextSelection[] selections = document.FindAll("[Insert TOC]", true, true); | ||
WTextRange textrange = selections[0].GetAsOneRange(); | ||
WParagraph paragraph = textrange.OwnerParagraph; | ||
int index = paragraph.ChildEntities.IndexOf(textrange); | ||
//Remove the existing text | ||
paragraph.ChildEntities.Remove(textrange); | ||
//Insert the TOC | ||
InsertTOC(document, paragraph, index); | ||
//Update the TOC | ||
document.UpdateTableOfContents(); | ||
//Creates file stream. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) | ||
{ | ||
//Saves the Word document to file stream. | ||
document.Save(outputFileStream, FormatType.Docx); | ||
} | ||
} | ||
} | ||
} | ||
/// <summary> | ||
/// Insert the TOC in the index of given paragraph | ||
/// </summary> | ||
/// <param name="document"></param> | ||
/// <param name="ownerPara"></param> | ||
/// <param name="index"></param> | ||
private static void InsertTOC(WordDocument document, WParagraph ownerPara, int index) | ||
{ | ||
//Create a new paragraph | ||
WParagraph newPara = new WParagraph(document); | ||
//Append TOC to the new paragraph | ||
newPara.AppendTOC(1, 3); | ||
//Insert the child entities of new paragraph to the owner paragraph at the given index. | ||
for (int i = 0; i < newPara.ChildEntities.Count;) | ||
{ | ||
ownerPara.ChildEntities.Insert(index, newPara.ChildEntities[i]); | ||
index++; | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...place/Find-and-replace-text-with-chart/.NET Standard/Find-and-replace-text-with-chart.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.4.33213.308 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-replace-text-with-chart", "Find-and-replace-text-with-chart\Find-and-replace-text-with-chart.csproj", "{6DC22CF0-0561-4C68-9D83-5C0971490748}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{6DC22CF0-0561-4C68-9D83-5C0971490748}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{6DC22CF0-0561-4C68-9D83-5C0971490748}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{6DC22CF0-0561-4C68-9D83-5C0971490748}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{6DC22CF0-0561-4C68-9D83-5C0971490748}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {D05EB822-AE02-4DFA-B932-C32E99F9BFA9} | ||
EndGlobalSection | ||
EndGlobal |
Binary file added
BIN
+16.6 KB
...nd-replace-text-with-chart/.NET Standard/Find-and-replace-text-with-chart/Data/Input.docx
Binary file not shown.
13 changes: 13 additions & 0 deletions
13
...rt/.NET Standard/Find-and-replace-text-with-chart/Find-and-replace-text-with-chart.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>Find_and_replace_text_with_chart</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
102 changes: 102 additions & 0 deletions
102
...ind-and-replace-text-with-chart/.NET Standard/Find-and-replace-text-with-chart/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Syncfusion.DocIO; | ||
using Syncfusion.DocIO.DLS; | ||
using Syncfusion.OfficeChart; | ||
using System; | ||
using System.ComponentModel; | ||
using System.IO; | ||
|
||
namespace Find_and_replace_text_with_chart | ||
{ | ||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
//Opens an existing Word document. | ||
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) | ||
{ | ||
TextBodyPart bodyPart = new TextBodyPart(document); | ||
//Create new paragraph | ||
WParagraph paragraph= CreateBarChart(document); | ||
bodyPart.BodyItems.Add(paragraph); | ||
//Replaces the placeholder text with a new chart. | ||
document.Replace("[Purchase details]", bodyPart, true, true, true); | ||
//Creates file stream. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) | ||
{ | ||
//Saves the Word document to file stream. | ||
document.Save(outputFileStream, FormatType.Docx); | ||
} | ||
} | ||
} | ||
} | ||
/// <summary> | ||
/// Create a bar chart inside a new paragraph. | ||
/// </summary> | ||
/// <param name="document"></param> | ||
/// <returns></returns> | ||
private static WParagraph CreateBarChart(WordDocument document) | ||
{ | ||
//Create new paragraph | ||
WParagraph paragraph = new WParagraph(document); | ||
//Append new chart | ||
WChart barchart = paragraph.AppendChart(400, 300); | ||
//Set type as bar chart | ||
barchart.ChartType = OfficeChartType.Bar_Clustered; | ||
//Add data for the bar chart | ||
AddBarChartData(barchart); | ||
|
||
//Set the other parts of the chart. | ||
barchart.ChartTitle = "Purchase Details"; | ||
barchart.ChartTitleArea.FontName = "Calibri"; | ||
barchart.ChartTitleArea.Size = 14; | ||
IOfficeChartSerie serie1 = barchart.Series.Add("Sum of Future Expenses"); | ||
serie1.Values = barchart.ChartData[2, 2, 6, 2]; | ||
IOfficeChartSerie serie2 = barchart.Series.Add("Sum of Purchases"); | ||
serie2.Values = barchart.ChartData[2, 3, 6, 3]; | ||
|
||
barchart.HasDataTable = true; | ||
barchart.DataTable.HasBorders = true; | ||
barchart.DataTable.HasHorzBorder = true; | ||
barchart.DataTable.HasVertBorder = true; | ||
barchart.DataTable.ShowSeriesKeys = true; | ||
barchart.HasLegend = false; | ||
|
||
barchart.PlotArea.Border.LinePattern = OfficeChartLinePattern.Solid; | ||
barchart.ChartArea.Border.LinePattern = OfficeChartLinePattern.Solid; | ||
barchart.PrimaryCategoryAxis.CategoryLabels = barchart.ChartData[2, 1, 6, 1]; | ||
barchart.PrimaryCategoryAxis.Font.Size = 12; | ||
barchart.PrimaryCategoryAxis.MajorTickMark = OfficeTickMark.TickMark_None; | ||
barchart.PrimaryValueAxis.MajorTickMark = OfficeTickMark.TickMark_None; | ||
|
||
//Return the new paragraph that has bar chart in it. | ||
return paragraph; | ||
} | ||
/// <summary> | ||
/// Add the data for bar chart | ||
/// </summary> | ||
/// <param name="chart"></param> | ||
private static void AddBarChartData(WChart chart) | ||
{ | ||
chart.ChartData.SetValue(1, 2, "Sum of Future Expenses"); | ||
chart.ChartData.SetValue(1, 3, "Sum of Purchases"); | ||
chart.ChartData.SetValue(2, 1, "Nancy Davalio"); | ||
chart.ChartData.SetValue(2, 2, 1300); | ||
chart.ChartData.SetValue(2, 3, 600); | ||
chart.ChartData.SetValue(3, 1, "Andrew Fuller"); | ||
chart.ChartData.SetValue(3, 2, 680); | ||
chart.ChartData.SetValue(3, 3, 1000); | ||
chart.ChartData.SetValue(4, 1, "Janet Leverling"); | ||
chart.ChartData.SetValue(4, 2, 1280); | ||
chart.ChartData.SetValue(4, 3, 800); | ||
chart.ChartData.SetValue(5, 1, "Margaret Peacock"); | ||
chart.ChartData.SetValue(5, 2, 2000); | ||
chart.ChartData.SetValue(5, 3, 400); | ||
chart.ChartData.SetValue(6, 1, "Steven Buchanan"); | ||
chart.ChartData.SetValue(6, 2, 2660); | ||
chart.ChartData.SetValue(6, 3, 731); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Find-and-Replace/Replace-text-with-hyperlink/.NET Standard/Replace-text-with-hyperlink.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.4.33213.308 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-hyperlink", "Replace-text-with-hyperlink\Replace-text-with-hyperlink.csproj", "{0E4D096D-C506-497E-8DE1-A71DECFBBB8C}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{0E4D096D-C506-497E-8DE1-A71DECFBBB8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{0E4D096D-C506-497E-8DE1-A71DECFBBB8C}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{0E4D096D-C506-497E-8DE1-A71DECFBBB8C}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{0E4D096D-C506-497E-8DE1-A71DECFBBB8C}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {FC6DAA82-E7CA-4AB9-A2A0-A54A52989B25} | ||
EndGlobalSection | ||
EndGlobal |
Binary file added
BIN
+12.5 KB
...ace/Replace-text-with-hyperlink/.NET Standard/Replace-text-with-hyperlink/Data/Input.docx
Binary file not shown.
40 changes: 40 additions & 0 deletions
40
...-Replace/Replace-text-with-hyperlink/.NET Standard/Replace-text-with-hyperlink/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Syncfusion.DocIO; | ||
using Syncfusion.DocIO.DLS; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Replace_text_with_hyperlink | ||
{ | ||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
//Opens an existing Word document. | ||
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) | ||
{ | ||
string textToReplace = "Syncfusion"; | ||
//Initialize new text body part | ||
TextBodyPart textBodyPart = new TextBodyPart(document); | ||
//Initialize new paragraph | ||
WParagraph paragraph = new WParagraph(document); | ||
//Adds the paragraph into the text body part | ||
textBodyPart.BodyItems.Add(paragraph); | ||
//Appends web hyperlink to the paragraph | ||
paragraph.AppendHyperlink("http://www.syncfusion.com", textToReplace, HyperlinkType.WebLink); | ||
//Replaces all entries of given string in the document with text body part | ||
document.Replace(textToReplace, textBodyPart, false, true); | ||
//Clears the text body part | ||
textBodyPart.Clear(); | ||
//Creates file stream. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) | ||
{ | ||
//Saves the Word document to file stream. | ||
document.Save(outputFileStream, FormatType.Docx); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...th-hyperlink/.NET Standard/Replace-text-with-hyperlink/Replace-text-with-hyperlink.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>Replace_text_with_hyperlink</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
</Project> |