Conduct Exam
New member
- Joined
- Feb 29, 2024
- Messages
- 4
- Programming Experience
- Beginner
I want to add watermark Using C#. And I expect this solution. first the watermark should be added as a watermark object rather than a shape in the header. And Watermark add to all pages of word file. The second watermark should be editable and when I edit the watermark Manually in Wordfile, the watermark should change in all pages of the wordfile.
For More Information I Use This code.....
Issue:-Add watermark to wordfile using this code which is good. But when the watermark is added using this code we open the created word file and click on the header to edit the watermark there is an option to remove the header in the header menu. When we click on the RemoveHeader option the watermark is also removed. It means that the watermark added to the header is not valid because it has been removed. Watermark like when we manually add it to word file from design menu is not added to header which is ok. And if we remove the header, the watermark is not removed because it is not added to the header. I want to add watermark like this by coding. So if the header is removed the watermark will not be removed. And second point watermarks should be editable. So if someone makes a change, it should be changed in all the pages of the word file.
For More Information I Use This code.....
C#:
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
Application wordApp = new Application();
Document doc = wordApp.Documents.Open(@"C:\path\to\your\document.docx");
wordApp.Visible = true;
foreach (Section section in doc.Sections)
{
// Add a watermark shape
Shape watermark = section.Shapes.AddTextEffect(
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
"Confidential",
"Arial",
36,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
0, 0);
// Set the position and properties of the watermark
watermark.WrapFormat.AllowOverlap = -1; // Allow overlap
watermark.Left = (float)(doc.PageSetup.PageWidth / 2) - (watermark.Width / 2);
watermark.Top = (float)(doc.PageSetup.PageHeight / 2) - (watermark.Height / 2);
watermark.Fill.Transparency = 0.5f; // Set transparency
watermark.TextEffect.NormalizedHeight = false;
}
doc.Save();
doc.Close();
wordApp.Quit();
}
}
Last edited by a moderator: