|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Merge_Documents_by_Page_Orientation |
| 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 | + bool skipSection = false; |
| 19 | + // Check if the section has landscape orientation which contain table |
| 20 | + if (section.PageSetup.Orientation == PageOrientation.Landscape) |
| 21 | + { |
| 22 | + for (int i = 0; i < section.Body.ChildEntities.Count; i++) |
| 23 | + { |
| 24 | + if (section.Body.ChildEntities[i] is WTable) |
| 25 | + { |
| 26 | + skipSection = true; |
| 27 | + break; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + // If the section is not marked to be skipped, clone and add it to the destination document |
| 32 | + if (!skipSection) |
| 33 | + { |
| 34 | + destinationDocument.Sections.Add(section.Clone()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Copy header from other document |
| 39 | + //If document already have an header clear the header part before copying |
| 40 | + using (FileStream headerStream = new FileStream(Path.GetFullPath("../../../Data/header.rtf"), FileMode.Open)) |
| 41 | + { |
| 42 | + WordDocument headerDoc = new WordDocument(headerStream, FormatType.Rtf); |
| 43 | + MoveHeader(headerDoc, destinationDocument); |
| 44 | + headerDoc.Close(); |
| 45 | + } |
| 46 | + // Copy footer from other document |
| 47 | + using (FileStream footerStream = new FileStream(Path.GetFullPath("../../../Data/footer.rtf"), FileMode.Open)) |
| 48 | + { |
| 49 | + WordDocument footerDoc = new WordDocument(footerStream, FormatType.Rtf); |
| 50 | + MoveFooter(footerDoc, destinationDocument); |
| 51 | + footerDoc.Close(); |
| 52 | + } |
| 53 | + |
| 54 | + //Saves the Word document to FileStream. |
| 55 | + using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
| 56 | + { |
| 57 | + destinationDocument.Save(outputStream, FormatType.Docx); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + // Method to move header content from the template document to the main document |
| 62 | + static void MoveHeader(WordDocument templateDocument, WordDocument mainDocument) |
| 63 | + { |
| 64 | + //Entity Collection |
| 65 | + EntityCollection header = templateDocument.Sections[0].Body.ChildEntities; |
| 66 | + //Move all the items from one collection to another collection |
| 67 | + for (int i = 0; i < mainDocument.Sections.Count; i++) |
| 68 | + { |
| 69 | + WSection mainDocSection = mainDocument.Sections[i] as WSection; |
| 70 | + // Move the header items from the template to the main document's Header |
| 71 | + MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, header); |
| 72 | + MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, header); |
| 73 | + MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, header); |
| 74 | + } |
| 75 | + } |
| 76 | + // Method to move footer content from the template document to the main document |
| 77 | + static void MoveFooter(WordDocument templateDocument, WordDocument mainDocument) |
| 78 | + { |
| 79 | + EntityCollection footer = templateDocument.Sections[0].Body.ChildEntities; |
| 80 | + //Move all the items from one collection to another collection |
| 81 | + for (int i = 0; i < mainDocument.Sections.Count; i++) |
| 82 | + { |
| 83 | + WSection mainDocSection = mainDocument.Sections[i] as WSection; |
| 84 | + // Move the footer items from the template to the main document's Footer |
| 85 | + MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, footer); |
| 86 | + MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, footer); |
| 87 | + MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, footer); |
| 88 | + } |
| 89 | + } |
| 90 | + // Move the footer items from the template to the main document's header footer |
| 91 | + static void MoveItems(EntityCollection destinationDoc, EntityCollection sourceDoc) |
| 92 | + { |
| 93 | + for (int i = 0; i < sourceDoc.Count; i++) |
| 94 | + { |
| 95 | + destinationDoc.Add(sourceDoc[i].Clone()); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments