|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace Replace_list_text_with_paragraphs |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + using (FileStream destDocStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 12 | + { |
| 13 | + //Open the destination Word document. |
| 14 | + using (WordDocument destDocument = new WordDocument(destDocStream, FormatType.Docx)) |
| 15 | + { |
| 16 | + using (FileStream sourceDocStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 17 | + { |
| 18 | + //Open the source Word document. |
| 19 | + using (WordDocument sourceDocument = new WordDocument(sourceDocStream, FormatType.Docx)) |
| 20 | + { |
| 21 | + //Find the text "[finder]" in the destination document. |
| 22 | + TextSelection textSelection = destDocument.Find("[finder]", false, false); |
| 23 | + if (textSelection != null) |
| 24 | + { |
| 25 | + //Get the paragraph containing the found text in destintaion document. |
| 26 | + WParagraph destOwnerParagraph = textSelection.GetAsOneRange().OwnerParagraph; |
| 27 | + //Get the owner section. |
| 28 | + WSection destOwnerSection = destOwnerParagraph.OwnerTextBody.Owner as WSection; |
| 29 | + //Get the owner paragraph index. |
| 30 | + int destOwnerParaIndex = destOwnerSection.Paragraphs.IndexOf(destOwnerParagraph); |
| 31 | + //Retrieve the first section of the source document. |
| 32 | + WSection section = sourceDocument.Sections[0]; |
| 33 | + //Iterate through the each paragraph of the source document. |
| 34 | + for (int i = 0; i < section.Paragraphs.Count; i++) |
| 35 | + { |
| 36 | + WParagraph sourcePara = section.Paragraphs[i]; |
| 37 | + //Replace the found text with the first paragraph text from the source document. |
| 38 | + if (i == 0) |
| 39 | + destOwnerParagraph.Replace(textSelection.SelectedText, sourcePara.Text, false, false); |
| 40 | + //For remaining paragraphs in the source document. |
| 41 | + else |
| 42 | + { |
| 43 | + //Clone the found destination paragraph. |
| 44 | + WParagraph paraToInsert = (WParagraph)destOwnerParagraph.Clone(); |
| 45 | + //Change the paragraph text to the source paragraph text. |
| 46 | + paraToInsert.Text = sourcePara.Text; |
| 47 | + //Insert the cloned paragraph as the next item of the found destination paragraph. |
| 48 | + destOwnerSection.Body.ChildEntities.Insert(destOwnerParaIndex, paraToInsert); |
| 49 | + } |
| 50 | + //Increase the paragraph index to insert the next cloned paragraph in the correct position. |
| 51 | + destOwnerParaIndex++; |
| 52 | + } |
| 53 | + } |
| 54 | + //Save the destination Word document. |
| 55 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 56 | + { |
| 57 | + //Saves the Word document to file stream. |
| 58 | + destDocument.Save(outputFileStream, FormatType.Docx); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments