|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace Copy_HeaderFooter_from_one_doc_to_another_doc |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + // Open the template document stream for reading. |
| 12 | + using (FileStream templateStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 13 | + { |
| 14 | + // Open the main document stream for reading. |
| 15 | + using (FileStream maindocumentStreamPath = new FileStream(Path.GetFullPath(@"Data/MainDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 16 | + { |
| 17 | + // Load the main Word document. |
| 18 | + using (WordDocument mainDocument = new WordDocument(maindocumentStreamPath, FormatType.Docx)) |
| 19 | + { |
| 20 | + // Load the template Word document. |
| 21 | + using (WordDocument templateDocument = new WordDocument(templateStreamPath, FormatType.Docx)) |
| 22 | + { |
| 23 | + // Copy header and footer from the template document to the main document. |
| 24 | + MoveHeaderFooter(templateDocument, mainDocument); |
| 25 | + |
| 26 | + // Create a file stream for saving the updated document. |
| 27 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 28 | + { |
| 29 | + // Save the modified main document to the output file. |
| 30 | + mainDocument.Save(outputFileStream, FormatType.Docx); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + /// <summary> |
| 38 | + /// Move header and footer from one Word document to another Word document. |
| 39 | + /// </summary> |
| 40 | + /// <param name="templateDocument"></param> |
| 41 | + /// <param name="mainDocument"></param> |
| 42 | + public static void MoveHeaderFooter(WordDocument templateDocument, WordDocument mainDocument) |
| 43 | + { |
| 44 | + //Gets the section in the template word document |
| 45 | + WSection templateDocSection = templateDocument.Sections[0] as WSection; |
| 46 | + //Move all the items from one collection to another collection |
| 47 | + for (int i = 0; i < mainDocument.Sections.Count; i++) |
| 48 | + { |
| 49 | + WSection mainDocSection = mainDocument.Sections[i] as WSection; |
| 50 | + MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, templateDocSection.HeadersFooters.Header.ChildEntities); |
| 51 | + MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, templateDocSection.HeadersFooters.Footer.ChildEntities); |
| 52 | + MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, templateDocSection.HeadersFooters.Header.ChildEntities); |
| 53 | + MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, templateDocSection.HeadersFooters.Footer.ChildEntities); |
| 54 | + MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, templateDocSection.HeadersFooters.Header.ChildEntities); |
| 55 | + MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, templateDocSection.HeadersFooters.Footer.ChildEntities); |
| 56 | + |
| 57 | + //Copy the page setup from template document to main document. |
| 58 | + mainDocSection.PageSetup.DifferentOddAndEvenPages = templateDocSection.PageSetup.DifferentOddAndEvenPages; |
| 59 | + mainDocSection.PageSetup.DifferentFirstPage = templateDocSection.PageSetup.DifferentFirstPage; |
| 60 | + mainDocSection.PageSetup.HeaderDistance = templateDocSection.PageSetup.HeaderDistance; |
| 61 | + mainDocSection.PageSetup.FooterDistance = templateDocSection.PageSetup.FooterDistance; |
| 62 | + mainDocSection.PageSetup.Margins = templateDocSection.PageSetup.Margins; |
| 63 | + } |
| 64 | + } |
| 65 | + /// <summary> |
| 66 | + /// Move all the items from one collection to another collection. |
| 67 | + /// </summary> |
| 68 | + private static void MoveItems(EntityCollection childEntities1, EntityCollection childEntities2) |
| 69 | + { |
| 70 | + for (int i = 0; i < childEntities2.Count; i++) |
| 71 | + childEntities1.Add(childEntities2[i].Clone()); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments