|
5 | 5 | using (FileStream destinationStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open)) |
6 | 6 | { |
7 | 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)) |
| 8 | + using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx)) |
12 | 9 | { |
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; |
| 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 | + using (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 | 18 |
|
19 | | - // Get the index of the last section in the destination document. |
20 | | - int secIndex = destinationDocument.ChildEntities.IndexOf(destinationDocument.LastSection); |
| 19 | + // Get the index of the last section in the destination document. |
| 20 | + int secIndex = destinationDocument.ChildEntities.IndexOf(destinationDocument.LastSection); |
21 | 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); |
| 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 | 24 |
|
25 | | - // Get the style and formatting of the last paragraph for reference. |
26 | | - WParagraph lastPara = destinationDocument.LastParagraph; |
| 25 | + // Get the style and formatting of the last paragraph for reference. |
| 26 | + WParagraph lastPara = destinationDocument.LastParagraph; |
27 | 27 |
|
28 | | - // Import content from the source document into the destination document, using destination styles. |
29 | | - destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles); |
| 28 | + // Import content from the source document into the destination document, using destination styles. |
| 29 | + destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles); |
30 | 30 |
|
31 | | - // Modify the paragraph style for the newly added contents by applying left indentation. |
32 | | - AddLeftIndentation(destinationDocument, secIndex, paraIndex + 1, lastPara.ParagraphFormat.LeftIndent); |
| 31 | + // Modify the paragraph style for the newly added contents by applying left indentation. |
| 32 | + AddLeftIndentation(destinationDocument, secIndex, paraIndex + 1, lastPara.ParagraphFormat.LeftIndent); |
33 | 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 | | -}; |
| 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 | + } |
| 41 | + } |
| 42 | +} |
41 | 43 |
|
42 | 44 | /// <summary> |
43 | 45 | /// Applies left indentation to paragraphs and tables in a specified section and paragraph range of a Word document. |
|
0 commit comments