|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Import_Headers_and_Footers_from_Another_WordDocument |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + using (FileStream sourceStreamPath = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 11 | + { |
| 12 | + using (FileStream destinationStreamPath = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 13 | + { |
| 14 | + //Opens an source document from file system through constructor of WordDocument class |
| 15 | + using (WordDocument document = new WordDocument(sourceStreamPath, FormatType.Automatic)) |
| 16 | + { |
| 17 | + //Opens the destination document |
| 18 | + using (WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Docx)) |
| 19 | + { |
| 20 | + |
| 21 | + WSection section = document.Sections[0] as WSection; |
| 22 | + //Move all the items from one collection to another collection |
| 23 | + for (int i = 0; i < destinationDocument.Sections.Count; i++) |
| 24 | + { |
| 25 | + MoveItems(destinationDocument.Sections[i].HeadersFooters.EvenHeader.ChildEntities, section.HeadersFooters.Header.ChildEntities); |
| 26 | + MoveItems(destinationDocument.Sections[i].HeadersFooters.EvenFooter.ChildEntities, section.HeadersFooters.Footer.ChildEntities); |
| 27 | + MoveItems(destinationDocument.Sections[i].HeadersFooters.OddHeader.ChildEntities, section.HeadersFooters.Header.ChildEntities); |
| 28 | + MoveItems(destinationDocument.Sections[i].HeadersFooters.OddFooter.ChildEntities, section.HeadersFooters.Footer.ChildEntities); |
| 29 | + MoveItems(destinationDocument.Sections[i].HeadersFooters.FirstPageHeader.ChildEntities, section.HeadersFooters.Header.ChildEntities); |
| 30 | + MoveItems(destinationDocument.Sections[i].HeadersFooters.FirstPageFooter.ChildEntities, section.HeadersFooters.Footer.ChildEntities); |
| 31 | + destinationDocument.Sections[i].PageSetup.DifferentOddAndEvenPages = section.PageSetup.DifferentOddAndEvenPages; |
| 32 | + destinationDocument.Sections[i].PageSetup.DifferentFirstPage = section.PageSetup.DifferentFirstPage; |
| 33 | + } |
| 34 | + using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create)) |
| 35 | + { |
| 36 | + destinationDocument.Save(outputStream, FormatType.Docx); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + /// <summary> |
| 44 | + /// Move all the items from one collection to another collection. |
| 45 | + /// </summary> |
| 46 | + /// <param name="childEntities1"></param> |
| 47 | + /// <param name="childEntities2"></param> |
| 48 | + private static void MoveItems(EntityCollection childEntities1, EntityCollection childEntities2) |
| 49 | + { |
| 50 | + for (int i = 0; i < childEntities2.Count; i++) |
| 51 | + childEntities1.Add(childEntities2[i].Clone()); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
0 commit comments