Skip to content

Commit 0da6baa

Browse files
Merge pull request #367 from SyncfusionExamples/ES-921363-Add-watermark-specificpage
ES-921363- Add the sample Add-watermark-specificpage
2 parents ec46b62 + 98e9816 commit 0da6baa

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-watermark-specificpage", "Add-watermark-specificpage\Add-watermark-specificpage.csproj", "{173D87B9-539B-4097-B2E6-701EFC312169}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{173D87B9-539B-4097-B2E6-701EFC312169}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{173D87B9-539B-4097-B2E6-701EFC312169}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{173D87B9-539B-4097-B2E6-701EFC312169}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{173D87B9-539B-4097-B2E6-701EFC312169}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {67220009-FE1E-4A42-9C12-260CD1A3A33F}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_watermark_specificpage</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)