|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) |
| 5 | +{ |
| 6 | + // Load the Word document from the file stream |
| 7 | + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) |
| 8 | + { |
| 9 | + // Add header and footer to the document |
| 10 | + AddHeaderFooter(document); |
| 11 | + // Save the modified document to an output file |
| 12 | + using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) |
| 13 | + { |
| 14 | + document.Save(outputStream1, FormatType.Docx); |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Adds an image to the header paragraph. |
| 21 | +/// </summary> |
| 22 | +/// <param name="headerParagraph">The paragraph where the image will be added.</param> |
| 23 | +static void GetHeaderContent(IWParagraph headerParagraph) |
| 24 | +{ |
| 25 | + // Open the image file stream |
| 26 | + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Picture.png"), FileMode.Open, FileAccess.Read); |
| 27 | + |
| 28 | + // Append the image to the header paragraph |
| 29 | + IWPicture picture = headerParagraph.AppendPicture(imageStream); |
| 30 | + |
| 31 | + // Set image positioning and formatting properties |
| 32 | + picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText; |
| 33 | + picture.VerticalOrigin = VerticalOrigin.Margin; |
| 34 | + picture.VerticalPosition = -45; // Adjust vertical position |
| 35 | + picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 36 | + picture.HorizontalPosition = 30f; // Adjust horizontal position |
| 37 | + picture.WidthScale = 50; // Scale image width |
| 38 | + picture.HeightScale = 40; // Scale image height |
| 39 | +} |
| 40 | + |
| 41 | +/// <summary> |
| 42 | +/// Adds a styled footer with a background color and page number. |
| 43 | +/// </summary> |
| 44 | +static void GetFooterContent(IWParagraph footerParagraph) |
| 45 | +{ |
| 46 | + // Create a rectangle shape in the footer for background styling |
| 47 | + Shape rectangleShape = footerParagraph.AppendShape(AutoShapeType.Rectangle, 700, 50); |
| 48 | + // Set rectangle shape properties (position, alignment, wrapping, color) |
| 49 | + rectangleShape.WrapFormat.TextWrappingStyle = TextWrappingStyle.InFrontOfText; |
| 50 | + rectangleShape.HorizontalAlignment = ShapeHorizontalAlignment.Left; |
| 51 | + rectangleShape.VerticalAlignment = ShapeVerticalAlignment.Bottom; |
| 52 | + rectangleShape.HorizontalOrigin = HorizontalOrigin.Page; |
| 53 | + rectangleShape.VerticalOrigin = VerticalOrigin.Page; |
| 54 | + rectangleShape.FillFormat.Color = Syncfusion.Drawing.Color.LightBlue; // Set background color |
| 55 | + // Add a paragraph inside the rectangle for text |
| 56 | + footerParagraph = rectangleShape.TextBody.AddParagraph(); |
| 57 | + footerParagraph.AppendText("Adventure Works Cycles"); |
| 58 | + footerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; |
| 59 | + // Add a right-aligned tab stop for page number formatting |
| 60 | + footerParagraph.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader); |
| 61 | + // Append page number fields |
| 62 | + footerParagraph.AppendText("\t"); // Insert tab space |
| 63 | + footerParagraph.AppendField("Page", FieldType.FieldPage); // Current page number |
| 64 | + footerParagraph.AppendText(" of "); |
| 65 | + footerParagraph.AppendField("NumPages", FieldType.FieldNumPages); // Total number of pages |
| 66 | +} |
| 67 | + |
| 68 | +/// <summary> |
| 69 | +/// Adds headers and footers to all sections of the document. |
| 70 | +/// </summary> |
| 71 | +static void AddHeaderFooter(WordDocument document) |
| 72 | +{ |
| 73 | + for (int i = 0; i < document.Sections.Count; i++) |
| 74 | + { |
| 75 | + WSection section = document.Sections[i]; |
| 76 | + IWParagraph headerParagraph = section.HeadersFooters.FirstPageHeader.AddParagraph(); |
| 77 | + GetHeaderContent(headerParagraph); |
| 78 | + IWParagraph headerOddParagraph = section.HeadersFooters.OddHeader.AddParagraph(); |
| 79 | + GetHeaderContent(headerOddParagraph); |
| 80 | + IWParagraph evenParagraph = section.HeadersFooters.EvenHeader.AddParagraph(); |
| 81 | + GetHeaderContent(evenParagraph); |
| 82 | + IWParagraph footerParagraph = section.HeadersFooters.FirstPageFooter.AddParagraph(); |
| 83 | + GetFooterContent(footerParagraph); |
| 84 | + IWParagraph footerOddParagraph = section.HeadersFooters.OddFooter.AddParagraph(); |
| 85 | + GetFooterContent(footerOddParagraph); |
| 86 | + IWParagraph footerEvenParagraph = section.HeadersFooters.EvenFooter.AddParagraph(); |
| 87 | + GetFooterContent(footerEvenParagraph); |
| 88 | + } |
| 89 | +} |
0 commit comments