|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.Drawing; |
| 4 | + |
| 5 | +namespace Add_watermark_specificpage |
| 6 | +{ |
| 7 | + internal class Program |
| 8 | + { |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + // Open the Word document file for reading |
| 12 | + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) |
| 13 | + { |
| 14 | + // Load the document into the WordDocument object |
| 15 | + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) |
| 16 | + { |
| 17 | + // Retrieve all sections in the document |
| 18 | + List<Entity> sections = document.FindAllItemsByProperty(EntityType.Section, null, null); |
| 19 | + |
| 20 | + // Add "Syncfusion" watermark to the first section |
| 21 | + AddWatermarkToPage(sections[0] as WSection, "Adventures"); |
| 22 | + |
| 23 | + // Add "Draft" watermark to the second section |
| 24 | + AddWatermarkToPage(sections[1] as WSection, "Pandas"); |
| 25 | + |
| 26 | + // Save the modified document to a new file |
| 27 | + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
| 28 | + { |
| 29 | + document.Save(docStream1, FormatType.Docx); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Method to add a watermark in the document |
| 36 | + static void AddWatermarkToPage(IWSection section, string watermarkText) |
| 37 | + { |
| 38 | + // Access the body of the specified section |
| 39 | + WTextBody textBody = section.Body; |
| 40 | + // Adds a block content control (RichText) to the section |
| 41 | + BlockContentControl blockContentControl = textBody.AddBlockContentControl(ContentControlType.RichText) as BlockContentControl; |
| 42 | + |
| 43 | + // Adds a new paragraph inside the block content control |
| 44 | + WParagraph paragraph = blockContentControl.TextBody.AddParagraph() as WParagraph; |
| 45 | + // Create a text box to hold the watermark text |
| 46 | + WTextBox watermarkTextBox = paragraph.AppendTextBox(494, 164) as WTextBox; |
| 47 | + // Center-align the text box horizontally |
| 48 | + watermarkTextBox.TextBoxFormat.HorizontalAlignment = ShapeHorizontalAlignment.Center; |
| 49 | + // Center-align the text box vertically |
| 50 | + watermarkTextBox.TextBoxFormat.VerticalAlignment = ShapeVerticalAlignment.Center; |
| 51 | + // Remove the border line of the text box |
| 52 | + watermarkTextBox.TextBoxFormat.NoLine = true; |
| 53 | + // Set rotation angle for the watermark text box |
| 54 | + watermarkTextBox.TextBoxFormat.Rotation = 315; |
| 55 | + // Allow overlapping of the text box with other elements |
| 56 | + watermarkTextBox.TextBoxFormat.AllowOverlap = true; |
| 57 | + // Align the text box relative to the page margins |
| 58 | + watermarkTextBox.TextBoxFormat.HorizontalOrigin = HorizontalOrigin.Margin; |
| 59 | + watermarkTextBox.TextBoxFormat.VerticalOrigin = VerticalOrigin.Margin; |
| 60 | + // Set text wrapping style to behind, so the watermark does not interfere with content |
| 61 | + watermarkTextBox.TextBoxFormat.TextWrappingStyle = TextWrappingStyle.Behind; |
| 62 | + |
| 63 | + // Add another paragraph inside the text box to contain the watermark text |
| 64 | + IWParagraph watermarkParagraph = watermarkTextBox.TextBoxBody.AddParagraph(); |
| 65 | + // Append the watermark text to the paragraph and set the font size and color |
| 66 | + IWTextRange textRange = watermarkParagraph.AppendText(watermarkText); |
| 67 | + // Set a large font size for the watermark text |
| 68 | + textRange.CharacterFormat.FontSize = 100; |
| 69 | + // Set a light gray color for the watermark text |
| 70 | + textRange.CharacterFormat.TextColor = Color.FromArgb(255, 192, 192, 192); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments