Add watermark to word file using C# windows application
In this article i am going to explain you that how you can add watermark to word (document) file. Which is so simple by using c# in windows application
You just have to download the reference file which is attached as a link to this resource and add it your project and follow the below mentioned steps than you are done.
Step 1:
Add new form to project
Step 2:
Add button to form
Step 3:
Add the below namespaces to the form class file
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using V = DocumentFormat.OpenXml.Vml;
using System.Diagnostics;Step 4:
Add functions to the form class file as mentioned below code
private void InsertCustomWatermark(WordprocessingDocument package, string p)
{
SetWaterMarkPicture(p);
MainDocumentPart mainDocumentPart1 = package.MainDocumentPart;
if (mainDocumentPart1 != null)
{
mainDocumentPart1.DeleteParts(mainDocumentPart1.HeaderParts);
HeaderPart headPart1 = mainDocumentPart1.AddNewPart
GenerateHeaderPart1Content(headPart1);
string rId = mainDocumentPart1.GetIdOfPart(headPart1);
ImagePart image = headPart1.AddNewPart
GenerateImagePart1Content(image);
IEnumerable
foreach (var sectPr in sectPrs)
{
sectPr.RemoveAllChildren
sectPr.PrependChild
}
}
else
{
MessageBox.Show("alert");
}
}
private void GenerateHeaderPart1Content(HeaderPart headerPart1)
{
Header header1 = new Header();
Paragraph paragraph2 = new Paragraph();
Run run1 = new Run();
Picture picture1 = new Picture();
V.Shape shape1 = new V.Shape() { Id = "WordPictureWatermark75517470", Style = "position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:415.2pt;height:456.15pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2051", AllowInCell = false, Type = "#_x0000_t75" };
V.ImageData imageData1 = new V.ImageData() { Gain = "19661f", BlackLevel = "22938f", Title = "??", RelationshipId = "rId999" };
shape1.Append(imageData1);
picture1.Append(shape1);
run1.Append(picture1);
paragraph2.Append(run1);
header1.Append(paragraph2);
headerPart1.Header = header1;
}
private void GenerateImagePart1Content(ImagePart imagePart1)
{
System.IO.Stream data = GetBinaryDataStream(imagePart1Data);
imagePart1.FeedData(data);
data.Close();
}
private string imagePart1Data = "";
private System.IO.Stream GetBinaryDataStream(string base64String)
{
return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}
public void SetWaterMarkPicture(string file)
{
FileStream inFile;
byte[] byteArray;
try
{
inFile = new FileStream(file, FileMode.Open, FileAccess.Read);
byteArray = new byte[inFile.Length];
long byteRead = inFile.Read(byteArray, 0, (int)inFile.Length);
inFile.Close();
imagePart1Data = Convert.ToBase64String(byteArray, 0, byteArray.Length);
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}Step 5:
Write the below mentioned code on button click event
private void ButtonProcess_Click(object sender, EventArgs e)
{
string filename = null;
string docfilename = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select document file to add watermark";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Document Files|*.doc;*.docx;";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
docfilename = openFileDialog1.FileName;
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.InitialDirectory = @"C:\";
openFileDialog2.Title = "Select image file";
openFileDialog2.CheckFileExists = true;
openFileDialog2.CheckPathExists = true;
openFileDialog2.DefaultExt = "txt";
openFileDialog2.Filter = "All Image Files|*.jpg;*.jpeg;*.png;*.gif;*.tif;";
openFileDialog2.FilterIndex = 2;
openFileDialog2.RestoreDirectory = true;
openFileDialog2.ReadOnlyChecked = true;
openFileDialog2.ShowReadOnly = true;
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog2.FileName;
using (WordprocessingDocument package = WordprocessingDocument.Open(docfilename, true))
{
InsertCustomWatermark(package, filename);
}
MessageBox.Show("Check your file with watermark", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit(); // This will stop running application
}
}
}Step 6:
Follow the below link to download reference file to run the project and add it in your project as a reference
Click here to download an attachment
You are done.
Go to your application and run it.
Happy coding.