|
3 | 3 |
|
4 | 4 | using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
5 | 5 | { |
6 | | - using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 6 | + // Open the destination Word document. |
| 7 | + using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx)) |
7 | 8 | { |
8 | | - // Open the destination Word document. |
9 | | - using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx)) |
| 9 | + using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite)) |
10 | 10 | { |
11 | 11 | // Open the source Word document. |
12 | 12 | using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx)) |
13 | 13 | { |
14 | | - // Set the break code of the source document's first section to prevent page breaks. |
| 14 | + // Set the break code as no break to prevent page breaks. |
15 | 15 | sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak; |
16 | 16 | // Initialize a variable to hold the list style from the destination document. |
17 | 17 | ListStyle listStyle = null; |
18 | 18 | // Iterate through the paragraphs in the last section of the destination document. |
19 | 19 | foreach (WParagraph paragraph in destinationDocument.LastSection.Paragraphs) |
20 | 20 | { |
21 | | - // Store the current list style if available. |
22 | 21 | if (paragraph.ListFormat.CurrentListStyle != null) |
23 | 22 | { |
24 | | - listStyle = paragraph.ListFormat.CurrentListStyle; |
| 23 | + // Get the current list style. |
| 24 | + listStyle = paragraph.ListFormat.CurrentListStyle; |
25 | 25 | } |
26 | 26 | else |
27 | 27 | { |
28 | | - // If no current list style, check the paragraph style for a list style and store it. |
29 | | - WParagraphStyle style1 = destinationDocument.Styles.FindByName(paragraph.StyleName) as WParagraphStyle; |
30 | | - if (style1 != null) |
31 | | - listStyle = style1.ListFormat.CurrentListStyle; |
| 28 | + // Check the paragraph style for a list style and store it. |
| 29 | + WParagraphStyle style = destinationDocument.Styles.FindByName(paragraph.StyleName) as WParagraphStyle; |
| 30 | + if (style != null) |
| 31 | + listStyle = style.ListFormat.CurrentListStyle; |
32 | 32 | } |
33 | 33 | // Break the loop if a list style is found. |
34 | 34 | if (listStyle != null) |
35 | 35 | break; |
36 | 36 | } |
37 | 37 | // Import the content of the source document at the end of the destination document. |
38 | 38 | destinationDocument.ImportContent(sourceDocument, ImportOptions.ListContinueNumbering); |
39 | | - // If a list style is found, apply it to the paragraphs in the destination document to maintain continuous numbering. |
40 | 39 | if (listStyle != null) |
41 | 40 | { |
| 41 | + // Apply liststyle to the paragraphs in the destination document to maintain continuous numbering. |
42 | 42 | foreach (WParagraph paragraph in destinationDocument.LastSection.Paragraphs) |
43 | 43 | { |
44 | | - // Apply the found list style. |
45 | 44 | if (paragraph.ListFormat.CurrentListStyle != null) |
46 | | - paragraph.ListFormat.ApplyStyle(listStyle.Name); |
| 45 | + paragraph.ListFormat.ApplyStyle(listStyle.Name); |
47 | 46 | } |
48 | 47 | } |
49 | 48 | // Save the merged document. |
|
0 commit comments