Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -11,5 +11,15 @@
<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\SourceDocument\Document-100.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\DestinationDocument\Document-100.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
@@ -1,61 +1,28 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

class Program
{
static void Main()
{
string sourceFolder = Path.GetFullPath("../../../Data/SourceDocument/");
string destinationFolder = Path.GetFullPath("../../../Data/DestinationDocument/");
string outputFolder = Path.GetFullPath("../../../Output/");

Directory.CreateDirectory(outputFolder);

// Get all source files
string[] sourceFiles = Directory.GetFiles(sourceFolder, "*.docx");

foreach (string sourcePath in sourceFiles)
using (FileStream sourceFileStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument/Document-100.docx"), FileMode.Open, FileAccess.Read))
{
string fileName = Path.GetFileName(sourcePath);
string destinationPath = Path.Combine(destinationFolder, fileName);

if (!File.Exists(destinationPath))
{
Console.WriteLine($"Skipping {fileName} - No matching destination file found.");
continue;
}

string outputPath = Path.Combine(outputFolder, fileName);

Stopwatch stopwatch = Stopwatch.StartNew();

try
using (FileStream destinationFileStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument/Document-100.docx"), FileMode.Open, FileAccess.Read))
{
// Open source and destination document
WordDocument sourceDocument = new WordDocument(sourcePath);
WordDocument destinationDocument = new WordDocument(destinationPath);

// Clone and merge all slides
foreach (WSection section in sourceDocument.Sections)
WordDocument mainDoc = new WordDocument(sourceFileStream, FormatType.Docx);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add using statement

WordDocument mergeDoc = new WordDocument(destinationFileStream, FormatType.Docx);
Stopwatch sw = Stopwatch.StartNew();
mainDoc.ImportContent(mergeDoc, ImportOptions.UseDestinationStyles);
sw.Stop();
Console.WriteLine("Time taken for Merge Documents:" + sw.Elapsed.TotalSeconds);
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/MergedDocument.docx"), FileMode.Create))
{
WSection clonedSection = section.Clone();
destinationDocument.Sections.Add(clonedSection);
mainDoc.Save(outputFileStream, FormatType.Docx);
}

// Save the merged document.
destinationDocument.Save(outputPath);

stopwatch.Stop();
Console.WriteLine($"{fileName} is cloned and merged in {stopwatch.Elapsed.TotalSeconds} seconds.");
sourceDocument.Close();
destinationDocument.Close();
}
catch (Exception ex)
{
Console.WriteLine($"Error processing {fileName}: {ex.Message}");
mainDoc.Close();
mergeDoc.Close();
}
}
}
}
}
Loading