11using Syncfusion . DocIO ;
22using Syncfusion . DocIO . DLS ;
33
4- //Open the existing main document.
5- FileStream fileStream1 = new FileStream ( Path . GetFullPath ( @"Data/SourceDocument.docx" ) , FileMode . Open ) ;
6- WordDocument mainDocument = new WordDocument ( fileStream1 , FormatType . Docx ) ;
7- //Open the existing temporary document.
8- FileStream fileStream = new FileStream ( Path . GetFullPath ( @"Data/DestinationDocument.docx" ) , FileMode . Open ) ;
9- WordDocument tempDoc = new WordDocument ( fileStream , FormatType . Docx ) ;
10- //Set the first section break.
11- tempDoc . Sections [ 0 ] . BreakCode = SectionBreakCode . NoBreak ;
12-
13- //Get the last section index of main document.
14- int secIndex = mainDocument . ChildEntities . IndexOf ( mainDocument . LastSection ) ;
15- //Get the last paragraph index of main document.
16- int paraIndex = mainDocument . LastSection . Body . ChildEntities . IndexOf ( mainDocument . LastParagraph ) ;
17- //Get the last paragraph style.
18- WParagraph lastPara = mainDocument . LastParagraph ;
19-
20- //Import the temporary document content to the main document.
21- mainDocument . ImportContent ( tempDoc , ImportOptions . UseDestinationStyles ) ;
22-
23- //Modify the paragraph style for the added contents.
24- AddLeftIndentation ( mainDocument , secIndex , paraIndex + 1 , lastPara . ParagraphFormat . LeftIndent ) ;
25-
26- //Save the main document.
27- FileStream outputStream = new FileStream ( Path . GetFullPath ( @"Output/Output.docx" ) , FileMode . Create , FileAccess . Write ) ;
28- mainDocument . Save ( outputStream , FormatType . Docx ) ;
29-
30- //Dispose the streams.
31- fileStream1 . Dispose ( ) ;
32- fileStream . Dispose ( ) ;
4+ // Open the existing main document from a file stream.
5+ using ( FileStream destinationStream = new FileStream ( Path . GetFullPath ( @"Data/DestinationDocument.docx" ) , FileMode . Open ) )
6+ {
7+ // Load the main Word document.
8+ WordDocument destinationDocument = new WordDocument ( destinationStream , FormatType . Docx ) ;
9+
10+ // Open the existing source document from a file stream.
11+ using ( FileStream sourceStream = new FileStream ( Path . GetFullPath ( @"Data/SourceDocument.docx" ) , FileMode . Open ) )
12+ {
13+ // Load the source Word document.
14+ WordDocument sourceDocument = new WordDocument ( sourceStream , FormatType . Docx ) ;
15+
16+ // Set the first section break in the source document to "NoBreak" for seamless merging.
17+ sourceDocument . Sections [ 0 ] . BreakCode = SectionBreakCode . NoBreak ;
18+
19+ // Get the index of the last section in the destination document.
20+ int secIndex = destinationDocument . ChildEntities . IndexOf ( destinationDocument . LastSection ) ;
21+
22+ // Get the index of the last paragraph in the last section of the destination document.
23+ int paraIndex = destinationDocument . LastSection . Body . ChildEntities . IndexOf ( destinationDocument . LastParagraph ) ;
24+
25+ // Get the style and formatting of the last paragraph for reference.
26+ WParagraph lastPara = destinationDocument . LastParagraph ;
27+
28+ // Import content from the source document into the destination document, using destination styles.
29+ destinationDocument . ImportContent ( sourceDocument , ImportOptions . UseDestinationStyles ) ;
30+
31+ // Modify the paragraph style for the newly added contents by applying left indentation.
32+ AddLeftIndentation ( destinationDocument , secIndex , paraIndex + 1 , lastPara . ParagraphFormat . LeftIndent ) ;
33+
34+ // Save the updated destination document to a new file.
35+ using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"Output/Output.docx" ) , FileMode . Create , FileAccess . Write ) )
36+ {
37+ destinationDocument . Save ( outputStream , FormatType . Docx ) ;
38+ } ;
39+ } ;
40+ } ;
3341
3442/// <summary>
3543/// Applies left indentation to paragraphs and tables in a specified section and paragraph range of a Word document.
3644/// </summary>
37- void AddLeftIndentation ( WordDocument document1 , int secIndex , int paraIndex , float leftIndent )
45+ void AddLeftIndentation ( WordDocument document , int secIndex , int paraIndex , float leftIndent )
3846{
39- //Iterate through the sections added from the temporary document.
40- for ( int i = secIndex ; i < document1 . ChildEntities . IndexOf ( document1 . LastSection ) + 1 ; i ++ )
47+ // Iterate through the sections added from the source document, starting from secIndex .
48+ for ( int i = secIndex ; i < document . ChildEntities . IndexOf ( document . LastSection ) + 1 ; i ++ )
4149 {
42- //Iterate through the child entities added from the temporary document.
43- for ( int j = paraIndex ; j < document1 . Sections [ i ] . Body . ChildEntities . Count ; j ++ )
50+ // Iterate through the child entities (paragraphs/tables) added from the source document.
51+ for ( int j = paraIndex ; j < document . Sections [ i ] . Body . ChildEntities . Count ; j ++ )
4452 {
45- //If the child entity is a paragraph then apply the previously taken para left indent .
46- if ( document1 . Sections [ i ] . Body . ChildEntities [ j ] is WParagraph )
53+ // If the child entity is a paragraph, apply the left indent from the last paragraph .
54+ if ( document . Sections [ i ] . Body . ChildEntities [ j ] is WParagraph )
4755 {
48- WParagraph para = document1 . Sections [ i ] . Body . ChildEntities [ j ] as WParagraph ;
49- //Set the left indentation
56+ WParagraph para = document . Sections [ i ] . Body . ChildEntities [ j ] as WParagraph ;
57+ // Set the left indentation for the paragraph
5058 para . ParagraphFormat . LeftIndent = leftIndent ;
5159 }
52- //If the child entity is a table then apply the previously taken para left indent.
53- else if ( document1 . Sections [ i ] . Body . ChildEntities [ j ] is WTable )
60+ // If the child entity is a table, apply the same left indent to the table .
61+ else if ( document . Sections [ i ] . Body . ChildEntities [ j ] is WTable )
5462 {
55- WTable table = document1 . Sections [ i ] . Body . ChildEntities [ j ] as WTable ;
56- //Set the left indentation
63+ WTable table = document . Sections [ i ] . Body . ChildEntities [ j ] as WTable ;
64+ // Set the left indentation for the table.
5765 table . TableFormat . LeftIndent = leftIndent ;
5866 }
5967 }
60- //Reset the index for next section.
68+ // Reset the paragraph index for the next section to start from the beginning .
6169 paraIndex = 0 ;
6270 }
6371}
0 commit comments