I want to add watermark Using C#. And I expect this solution. first should not add the watermark as header. And Watermark add to all pages of word file. But not as a header. 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.
Loading
Leon DPosted Nov 12, 2024, 2:18 AM
Hi,
You can try this lib: https://www.e-iceblue.com/Download/download-word-for-net-free.html.
Jayraj ChhayaPosted Nov 11, 2024, 11:58 AM
Hi Gross Account,
Try below code
Let me know if not working
Gross AccountPosted Nov 11, 2024, 11:20 AM
Using This Code
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
AddCenteredTextWatermarkin the provided class adds a watermark as a text shape (WordShapeobject) 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.
Gross AccountPosted Nov 8, 2024, 7:09 AM
This Solution is Not Proper.
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
Watermark watermark = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].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;
// Set the watermark to be editable
watermark.TextEffect.Text = "Confidential"; // Editable text
}
doc.Save();
doc.Close();
wordApp.Quit();
}
}
Watermark watermark = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(...)
Issue: will cause a compilation error because
Watermarkis not a valid type in the Microsoft Office Interop libraries. There is no suchWatermarkclass or type available in the context of the Microsoft Office Interop library.Jayraj ChhayaPosted Nov 7, 2024, 2:29 PM
To achieve the desired functionality, the watermark should be added as a watermark object rather than a shape in the header. This can be accomplished by using the
Watermarkproperty of thePageSetupobject. Below is the revised code that implements this solution:Gross AccountPosted Nov 7, 2024, 1:00 PM
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.
Jayraj ChhayaPosted Nov 7, 2024, 9:51 AM
To add a watermark to a Word document in C# without placing it in the header, you can utilize the
Microsoft.Office.Interop.Wordlibrary. This approach allows you to insert a watermark as a shape, which can be edited and will reflect changes across all pages.This code opens a Word document, iterates through its sections, and adds a watermark that is centered on each page. The watermark can be edited directly in Word, and any changes will be reflected across all instances. Ensure you have the necessary references to
Microsoft.Office.Interop.WordandMicrosoft.Office.Corein your project.