|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 5 | +{ |
| 6 | + // Open the destination Word document. |
| 7 | + using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx)) |
| 8 | + { |
| 9 | + using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 10 | + { |
| 11 | + // Open the source Word document. |
| 12 | + using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx)) |
| 13 | + { |
| 14 | + // Set the break code as no break to prevent page breaks. |
| 15 | + sourceDocument.Sections[0].BreakCode = SectionBreakCode.NoBreak; |
| 16 | + // Initialize a variable to hold the list style from the destination document. |
| 17 | + ListStyle listStyle = null; |
| 18 | + // Iterate through the paragraphs in the last section of the destination document. |
| 19 | + int paragraphCount = destinationDocument.LastSection.Paragraphs.Count; |
| 20 | + for (int i = paragraphCount - 1; i >= 0; i--) |
| 21 | + { |
| 22 | + WParagraph paragraph = destinationDocument.LastSection.Paragraphs[i]; |
| 23 | + // Get the current list style. |
| 24 | + if (paragraph.ListFormat.CurrentListStyle != null) |
| 25 | + listStyle = paragraph.ListFormat.CurrentListStyle; |
| 26 | + else |
| 27 | + { |
| 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 | + } |
| 33 | + // Break the loop if a list style is found. |
| 34 | + if (listStyle != null) |
| 35 | + break; |
| 36 | + } |
| 37 | + // Import the content of the source document at the end of the destination document. |
| 38 | + destinationDocument.ImportContent(sourceDocument, ImportOptions.ListContinueNumbering); |
| 39 | + if (listStyle != null) |
| 40 | + { |
| 41 | + // Apply liststyle to the paragraphs in the destination document to maintain continuous numbering. |
| 42 | + foreach (WParagraph paragraph in destinationDocument.LastSection.Paragraphs) |
| 43 | + { |
| 44 | + if (paragraph.ListFormat.CurrentListStyle != null) |
| 45 | + paragraph.ListFormat.ApplyStyle(listStyle.Name); |
| 46 | + } |
| 47 | + } |
| 48 | + // Save the merged document. |
| 49 | + using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 50 | + { |
| 51 | + destinationDocument.Save(outputStream, FormatType.Docx); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments