Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Find-and-Replace/Find-and-Replace.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Find_and_Replace</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Diagnostics;

namespace Find_and_replace_in_a_worddocument
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open the template Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//Find all occurrences of a misspelled word and replaces with properly spelled word
int replacedCount = document.Replace("document", "DocIO", false, false);
stopwatch.Stop();
Console.WriteLine(replacedCount);
Console.WriteLine($"Time taken for Replace (string):" + stopwatch.Elapsed.TotalSeconds);
//Create file stream
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the Word document to file stream
document.Save(outputFileStream, FormatType.Docx);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,22 @@ class Program
{
static void Main()
{
string inputFolder = Path.GetFullPath("../../../Data/");
string outputFolder = Path.GetFullPath("../../../Output/");

Directory.CreateDirectory(outputFolder);

// Get all .docx files in the Data folder
string[] files = Directory.GetFiles(inputFolder, "*.docx");

foreach (string inputPath in files)
Stopwatch stopwatch = Stopwatch.StartNew();
//Open a file as a stream.
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
string fileName = Path.GetFileName(inputPath);
string outputPath = Path.Combine(outputFolder, fileName);

Stopwatch stopwatch = Stopwatch.StartNew();

try
{
// Load or open Word document
WordDocument document = new WordDocument(inputPath);

// Save the Word document to Output folder
document.Save(outputPath);
stopwatch.Stop();
Console.WriteLine($"{fileName} open and saved in {stopwatch.Elapsed.TotalSeconds} seconds");
document.Close();
}
catch (Exception ex)
//Load the file stream into a Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
Console.WriteLine($"Error processing {fileName}: {ex.Message}");
//Create a file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save a Markdown file to the file stream.
document.Save(outputFileStream, FormatType.Docx);
stopwatch.Stop();
Console.WriteLine($"Time taken to open and save a 100-page document: {stopwatch.Elapsed.TotalSeconds} seconds");
}
}
}
}
Expand Down
Loading