1- using System . ComponentModel ;
2- using System . Diagnostics ;
1+ using System . Diagnostics ;
32using Syncfusion . DocIO ;
43using Syncfusion . DocIO . DLS ;
54
65class 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+ using ( WordDocument mainDoc = new WordDocument ( sourceFileStream , FormatType . Docx ) )
4214 {
43- WSection clonedSection = section . Clone ( ) ;
44- destinationDocument . Sections . Add ( clonedSection ) ;
45- }
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 } ") ;
15+ using ( WordDocument mergeDoc = new WordDocument ( destinationFileStream , FormatType . Docx ) )
16+ {
17+ Stopwatch sw = Stopwatch . StartNew ( ) ;
18+ mainDoc . ImportContent ( mergeDoc , ImportOptions . UseDestinationStyles ) ;
19+ sw . Stop ( ) ;
20+ Console . WriteLine ( "Time taken for Merge Documents:" + sw . Elapsed . TotalSeconds ) ;
21+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/MergedDocument.docx" ) , FileMode . Create ) )
22+ {
23+ mainDoc . Save ( outputFileStream , FormatType . Docx ) ;
24+ }
25+ }
26+ }
5827 }
5928 }
6029 }
61- }
30+ }
0 commit comments