|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Merge_Multiple_Documents |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + using (FileStream sourceStream = new FileStream(Path.GetFullPath("../../../Data/MainDocument.rtf"), FileMode.Open)) |
| 11 | + using (FileStream destinationStream = new FileStream(Path.GetFullPath("../../../Data/Template.rtf"), FileMode.Open)) |
| 12 | + using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Automatic)) |
| 13 | + using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Rtf)) |
| 14 | + { |
| 15 | + //Iterate through each section in the source document |
| 16 | + foreach (WSection section in sourceDocument.Sections) |
| 17 | + { |
| 18 | + destinationDocument.Sections.Add(section.Clone()); |
| 19 | + } |
| 20 | + |
| 21 | + // Copy header from other document |
| 22 | + //If document already have an header clear the header part before copying |
| 23 | + using (FileStream headerStream = new FileStream(Path.GetFullPath("../../../Data/header.rtf"), FileMode.Open)) |
| 24 | + { |
| 25 | + WordDocument headerDoc = new WordDocument(headerStream, FormatType.Rtf); |
| 26 | + MoveHeader(headerDoc, destinationDocument); |
| 27 | + headerDoc.Close(); |
| 28 | + } |
| 29 | + // Copy footer from other document |
| 30 | + using (FileStream footerStream = new FileStream(Path.GetFullPath("../../../Data/footer.rtf"), FileMode.Open)) |
| 31 | + { |
| 32 | + WordDocument footerDoc = new WordDocument(footerStream, FormatType.Rtf); |
| 33 | + MoveFooter(footerDoc, destinationDocument); |
| 34 | + footerDoc.Close(); |
| 35 | + } |
| 36 | + |
| 37 | + //Saves the Word document to FileStream. |
| 38 | + using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
| 39 | + { |
| 40 | + destinationDocument.Save(outputStream, FormatType.Docx); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + // Method to move header content from the template document to the main document |
| 45 | + static void MoveHeader(WordDocument templateDocument, WordDocument mainDocument) |
| 46 | + { |
| 47 | + //Entity Collection |
| 48 | + EntityCollection header = templateDocument.Sections[0].Body.ChildEntities; |
| 49 | + //Move all the items from one collection to another collection |
| 50 | + for (int i = 0; i < mainDocument.Sections.Count; i++) |
| 51 | + { |
| 52 | + WSection mainDocSection = mainDocument.Sections[i] as WSection; |
| 53 | + // Move the header items from the template to the main document's Header |
| 54 | + MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, header); |
| 55 | + MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, header); |
| 56 | + MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, header); |
| 57 | + } |
| 58 | + } |
| 59 | + // Method to move footer content from the template document to the main document |
| 60 | + static void MoveFooter(WordDocument templateDocument, WordDocument mainDocument) |
| 61 | + { |
| 62 | + EntityCollection footer = templateDocument.Sections[0].Body.ChildEntities; |
| 63 | + //Move all the items from one collection to another collection |
| 64 | + for (int i = 0; i < mainDocument.Sections.Count; i++) |
| 65 | + { |
| 66 | + WSection mainDocSection = mainDocument.Sections[i] as WSection; |
| 67 | + // Move the footer items from the template to the main document's Footer |
| 68 | + MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, footer); |
| 69 | + MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, footer); |
| 70 | + MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, footer); |
| 71 | + } |
| 72 | + } |
| 73 | + // Move the footer items from the template to the main document's header footer |
| 74 | + static void MoveItems(EntityCollection destinationDoc, EntityCollection sourceDoc) |
| 75 | + { |
| 76 | + for (int i = 0; i < sourceDoc.Count; i++) |
| 77 | + { |
| 78 | + destinationDoc.Add(sourceDoc[i].Clone()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments