Skip to content

Commit 4e4eb94

Browse files
Modified sample
1 parent b88ae2e commit 4e4eb94

3 files changed

Lines changed: 26 additions & 48 deletions

File tree

Performance-metrices/Clone-and-merge/.NET/Clone-and-merge-word-document/Clone-and-merge-word-document/Clone-and-merge-word-document.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,15 @@
1111
<ItemGroup>
1212
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
14-
14+
<ItemGroup>
15+
<None Update="Data\SourceDocument\Document-100.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\DestinationDocument\Document-100.docx">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Output\.gitkeep">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
1525
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,28 @@
1-
using System.ComponentModel;
2-
using System.Diagnostics;
1+
using System.Diagnostics;
32
using Syncfusion.DocIO;
43
using Syncfusion.DocIO.DLS;
54

65
class Program
76
{
87
static void Main()
98
{
10-
string sourceFolder = Path.GetFullPath("../../../Data/SourceDocument/");
11-
string destinationFolder = Path.GetFullPath("../../../Data/DestinationDocument/");
12-
string outputFolder = Path.GetFullPath("../../../Output/");
13-
14-
Directory.CreateDirectory(outputFolder);
15-
16-
// Get all source files
17-
string[] sourceFiles = Directory.GetFiles(sourceFolder, "*.docx");
18-
19-
foreach (string sourcePath in sourceFiles)
9+
using (FileStream sourceFileStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument/Document-100.docx"), FileMode.Open, FileAccess.Read))
2010
{
21-
string fileName = Path.GetFileName(sourcePath);
22-
string destinationPath = Path.Combine(destinationFolder, fileName);
23-
24-
if (!File.Exists(destinationPath))
25-
{
26-
Console.WriteLine($"Skipping {fileName} - No matching destination file found.");
27-
continue;
28-
}
29-
30-
string outputPath = Path.Combine(outputFolder, fileName);
31-
32-
Stopwatch stopwatch = Stopwatch.StartNew();
33-
34-
try
11+
using (FileStream destinationFileStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument/Document-100.docx"), FileMode.Open, FileAccess.Read))
3512
{
36-
// Open source and destination document
37-
WordDocument sourceDocument = new WordDocument(sourcePath);
38-
WordDocument destinationDocument = new WordDocument(destinationPath);
39-
40-
// Clone and merge all slides
41-
foreach (WSection section in sourceDocument.Sections)
13+
WordDocument mainDoc = new WordDocument(sourceFileStream, FormatType.Docx);
14+
WordDocument mergeDoc = new WordDocument(destinationFileStream, FormatType.Docx);
15+
Stopwatch sw = Stopwatch.StartNew();
16+
mainDoc.ImportContent(mergeDoc, ImportOptions.UseDestinationStyles);
17+
sw.Stop();
18+
Console.WriteLine("Time taken for Merge Documents:" + sw.Elapsed.TotalSeconds);
19+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/MergedDocument.docx"), FileMode.Create))
4220
{
43-
WSection clonedSection = section.Clone();
44-
destinationDocument.Sections.Add(clonedSection);
21+
mainDoc.Save(outputFileStream, FormatType.Docx);
4522
}
46-
47-
// Save the merged document.
48-
destinationDocument.Save(outputPath);
49-
50-
stopwatch.Stop();
51-
Console.WriteLine($"{fileName} is cloned and merged in {stopwatch.Elapsed.TotalSeconds} seconds.");
52-
sourceDocument.Close();
53-
destinationDocument.Close();
54-
}
55-
catch (Exception ex)
56-
{
57-
Console.WriteLine($"Error processing {fileName}: {ex.Message}");
23+
mainDoc.Close();
24+
mergeDoc.Close();
5825
}
5926
}
6027
}
61-
}
28+
}

0 commit comments

Comments
 (0)