diff --git a/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage.sln b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage.sln new file mode 100644 index 000000000..4b4773952 --- /dev/null +++ b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-watermark-specificpage", "Add-watermark-specificpage\Add-watermark-specificpage.csproj", "{173D87B9-539B-4097-B2E6-701EFC312169}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {173D87B9-539B-4097-B2E6-701EFC312169}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {173D87B9-539B-4097-B2E6-701EFC312169}.Debug|Any CPU.Build.0 = Debug|Any CPU + {173D87B9-539B-4097-B2E6-701EFC312169}.Release|Any CPU.ActiveCfg = Release|Any CPU + {173D87B9-539B-4097-B2E6-701EFC312169}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {67220009-FE1E-4A42-9C12-260CD1A3A33F} + EndGlobalSection +EndGlobal diff --git a/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Add-watermark-specificpage.csproj b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Add-watermark-specificpage.csproj new file mode 100644 index 000000000..009ec68ce --- /dev/null +++ b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Add-watermark-specificpage.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Add_watermark_specificpage + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Data/Template.docx b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Data/Template.docx new file mode 100644 index 000000000..9635887f3 Binary files /dev/null and b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Data/Template.docx differ diff --git a/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Output/.gitkeep b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Program.cs b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Program.cs new file mode 100644 index 000000000..0fefee657 --- /dev/null +++ b/Watermark/Add-watermark-specificpage/.NET/Add-watermark-specificpage/Program.cs @@ -0,0 +1,73 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; +using Syncfusion.Drawing; + +namespace Add_watermark_specificpage +{ + internal class Program + { + static void Main(string[] args) + { + // Open the Word document file for reading + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) + { + // Load the document into the WordDocument object + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) + { + // Retrieve all sections in the document + List sections = document.FindAllItemsByProperty(EntityType.Section, null, null); + + // Add "Syncfusion" watermark to the first section + AddWatermarkToPage(sections[0] as WSection, "Adventures"); + + // Add "Draft" watermark to the second section + AddWatermarkToPage(sections[1] as WSection, "Pandas"); + + // Save the modified document to a new file + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) + { + document.Save(docStream1, FormatType.Docx); + } + } + } + } + + // Method to add a watermark in the document + static void AddWatermarkToPage(IWSection section, string watermarkText) + { + // Access the body of the specified section + WTextBody textBody = section.Body; + // Adds a block content control (RichText) to the section + BlockContentControl blockContentControl = textBody.AddBlockContentControl(ContentControlType.RichText) as BlockContentControl; + + // Adds a new paragraph inside the block content control + WParagraph paragraph = blockContentControl.TextBody.AddParagraph() as WParagraph; + // Create a text box to hold the watermark text + WTextBox watermarkTextBox = paragraph.AppendTextBox(494, 164) as WTextBox; + // Center-align the text box horizontally + watermarkTextBox.TextBoxFormat.HorizontalAlignment = ShapeHorizontalAlignment.Center; + // Center-align the text box vertically + watermarkTextBox.TextBoxFormat.VerticalAlignment = ShapeVerticalAlignment.Center; + // Remove the border line of the text box + watermarkTextBox.TextBoxFormat.NoLine = true; + // Set rotation angle for the watermark text box + watermarkTextBox.TextBoxFormat.Rotation = 315; + // Allow overlapping of the text box with other elements + watermarkTextBox.TextBoxFormat.AllowOverlap = true; + // Align the text box relative to the page margins + watermarkTextBox.TextBoxFormat.HorizontalOrigin = HorizontalOrigin.Margin; + watermarkTextBox.TextBoxFormat.VerticalOrigin = VerticalOrigin.Margin; + // Set text wrapping style to behind, so the watermark does not interfere with content + watermarkTextBox.TextBoxFormat.TextWrappingStyle = TextWrappingStyle.Behind; + + // Add another paragraph inside the text box to contain the watermark text + IWParagraph watermarkParagraph = watermarkTextBox.TextBoxBody.AddParagraph(); + // Append the watermark text to the paragraph and set the font size and color + IWTextRange textRange = watermarkParagraph.AppendText(watermarkText); + // Set a large font size for the watermark text + textRange.CharacterFormat.FontSize = 100; + // Set a light gray color for the watermark text + textRange.CharacterFormat.TextColor = Color.FromArgb(255, 192, 192, 192); + } + } +}