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.....
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();
    }
}
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.
 
Last edited by a moderator:
I suggest examining the XML file underlying the .docx file to see the difference in structure between your way of adding watermarks and the indelible way of adding watermarks. Once you find the difference replicate the indelible structure. You might be forced to use OpenXML SDK (or the much more approachable ClosedXml library) to replicate the structure. It's possible that the Word automation object model doesn't support what you need to do.
 
Using This Code
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using Shape = Microsoft.Office.Interop.Word.Shape;
 namespace Watermark.GUI
{
    namespace Watermark.GUI
    {
        public class Watermark
        {
            private Application wordApp;
            private Document doc;

            public Watermark(Application wordApp, Document doc)
            {
                this.wordApp = wordApp;
                this.doc = doc;
            }

            public void AddCenteredTextWatermark(string watermarkText)
            {
                for (int pageIndex = 1; pageIndex <= doc.ComputeStatistics(WdStatistic.wdStatisticPages); pageIndex++)
                {
                    // Create a Range object for each page
                    Range pageRange = GetPageRange(doc, pageIndex);
                    if (pageRange != null)
                    {
                        // Add the watermark to this page range
                        Shape watermark = doc.Shapes.AddTextEffect(
                         MsoPresetTextEffect.msoTextEffect1,
                         watermarkText,
                         "Arial",  // Font name
                         10,        // Font size
                         MsoTriState.msoTrue,   // Bold
                         MsoTriState.msoFalse,  // Italic
                         0,       // Left position (initial value)
                         0,       // Top position (initial value)
                         pageRange // Adding it to the range of the current page
                        );

                        // Set the watermark properties
                        watermark.Fill.Visible = MsoTriState.msoTrue;
                        watermark.Fill.Solid();
                        watermark.Fill.ForeColor.RGB = (Int32)WdColor.wdColorGray20; // Watermark color
                        watermark.Line.Visible = MsoTriState.msoFalse; // No border
                        watermark.WrapFormat.Type = WdWrapType.wdWrapBehind; // Behind text
                        watermark.Rotation = -45; // Rotation angle

                        // Set width and height relative to the page size
                        watermark.Width = doc.PageSetup.PageWidth * 0.75f; // Width as 60% of page width
                        watermark.Height = watermark.Width / 4; // Height relative to width to maintain aspect ratio
                        watermark.LockAspectRatio = MsoTriState.msoTrue;

                        // Center the watermark by adjusting Left and Top properties after setting width and height
                        watermark.Left = (float)((doc.PageSetup.PageWidth - watermark.Width) / 2); // Center horizontally
                        watermark.Top = (float)((doc.PageSetup.PageHeight - watermark.Height) / 2); // Center vertically

                        watermark.ZOrder(MsoZOrderCmd.msoSendToBack);
                    }
                }
            }
            private Range GetPageRange(Document doc, int pageNumber)
            {
                object what = WdGoToItem.wdGoToPage;
                object which = WdGoToDirection.wdGoToAbsolute;
                object count = pageNumber;

                // Move selection to the start of the desired page
                Range pageStart = doc.GoTo(ref what, ref which, ref count);

                // Move to the start of the next page to get end of range (or document end if it's the last page)
                object nextPage = pageNumber + 1;
                Range pageEnd = doc.GoTo(ref what, ref which, ref nextPage);
                pageEnd.SetRange(pageEnd.Start, pageEnd.Start);

                // Return a range spanning the current page
                return doc.Range(pageStart.Start, pageEnd.Start);
            }
        }
    }

}
I add Watermark. The method AddCenteredTextWatermark in the provided class adds a watermark as a text shape (Word Shape object) to each page of the Word document. Using This if we remove the header, the watermark is not removed because it is not added to the header.That is fine.
Now I want For Edit if We Add Watermark Value In CustomeWatermark Option in Word then its editable .Now the watermark added with this code is editable but one page at a time. Not all edited at once. If you add a watermark value to Custom Watermark, you can edit the added watermark from there, and it will be edited in all pages. Or is there any other way so that I edit the watermark in one page and all pages are edited. But without adding to the header. Else when we do RemoveHeader the watermark is also removed.
if you know Another Solution then Provide it.
 
Last edited by a moderator:
You already have a thread about this... Merging threads.
 
Back
Top Bottom