Skip to content

Commit 5977796

Browse files
1006383-Added new files
1 parent 8233c28 commit 5977796

7 files changed

Lines changed: 75 additions & 26 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Find-and-Replace/Find-and-Replace.csproj" />
3+
</Solution>
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Find_and_Replace</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Diagnostics;
4+
5+
namespace Find_and_replace_in_a_worddocument
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
12+
{
13+
//Open the template Word document.
14+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
15+
{
16+
Stopwatch stopwatch = new Stopwatch();
17+
stopwatch.Start();
18+
//Find all occurrences of a misspelled word and replaces with properly spelled word
19+
int replacedCount = document.Replace("document", "DocIO", false, false);
20+
stopwatch.Stop();
21+
Console.WriteLine(replacedCount);
22+
Console.WriteLine($"Time taken for Replace (string):" + stopwatch.Elapsed.TotalSeconds);
23+
//Create file stream
24+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
25+
{
26+
//Save the Word document to file stream
27+
document.Save(outputFileStream, FormatType.Docx);
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


Performance-metrices/Open-and-save/.NET/Open-and-Save-Word-document/Open-and-Save-Word-document/Program.cs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,22 @@ class Program
88
{
99
static void Main()
1010
{
11-
string inputFolder = Path.GetFullPath("../../../Data/");
12-
string outputFolder = Path.GetFullPath("../../../Output/");
1311

14-
Directory.CreateDirectory(outputFolder);
15-
16-
// Get all .docx files in the Data folder
17-
string[] files = Directory.GetFiles(inputFolder, "*.docx");
18-
19-
foreach (string inputPath in files)
12+
Stopwatch stopwatch = Stopwatch.StartNew();
13+
//Open a file as a stream.
14+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
2015
{
21-
string fileName = Path.GetFileName(inputPath);
22-
string outputPath = Path.Combine(outputFolder, fileName);
23-
24-
Stopwatch stopwatch = Stopwatch.StartNew();
25-
26-
try
27-
{
28-
// Load or open Word document
29-
WordDocument document = new WordDocument(inputPath);
30-
31-
// Save the Word document to Output folder
32-
document.Save(outputPath);
33-
stopwatch.Stop();
34-
Console.WriteLine($"{fileName} open and saved in {stopwatch.Elapsed.TotalSeconds} seconds");
35-
document.Close();
36-
}
37-
catch (Exception ex)
16+
//Load the file stream into a Word document.
17+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
3818
{
39-
Console.WriteLine($"Error processing {fileName}: {ex.Message}");
19+
//Create a file stream.
20+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
21+
{
22+
//Save a Markdown file to the file stream.
23+
document.Save(outputFileStream, FormatType.Docx);
24+
stopwatch.Stop();
25+
Console.WriteLine($"Time taken to open and save a 100-page document: {stopwatch.Elapsed.TotalSeconds} seconds");
26+
}
4027
}
4128
}
4229
}

0 commit comments

Comments
 (0)