Skip to content

Commit

Permalink
Merge pull request #131 from snehasf3509/main
Browse files Browse the repository at this point in the history
Samples for replacing text with chart, hyperlink and TOC
  • Loading branch information
MohanaselvamJothi authored Oct 5, 2023
2 parents 3618159 + 6223553 commit 37eaaa6
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 1 deletion.
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 not shown.
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>
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++;
}
}
}
}
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 not shown.
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>
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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ static void Main(string[] args)
WParagraph paragraph = new WParagraph(document);
FileStream imageStream = new FileStream(Path.GetFullPath(@"../../../Data" + textSelections[i].SelectedText + ".png"), FileMode.Open, FileAccess.ReadWrite);
WPicture picture = paragraph.AppendPicture(imageStream) as WPicture;
TextSelection newSelection = new TextSelection(paragraph, 0, 1);
TextBodyPart bodyPart = new TextBodyPart(document);
bodyPart.BodyItems.Add(paragraph);
document.Replace(textSelections[i].SelectedText, bodyPart, true, true);
Expand Down
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 not shown.
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);
}
}
}
}
}
}
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>

0 comments on commit 37eaaa6

Please sign in to comment.