Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Add_watermark_specificpage</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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<Entity> 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);
}
}
}
Loading