Thursday, June 10, 2010

Watermarking Using Vsto

I found this very Intersting Code about water marking in Word using C#.

I found the code on Net So I like to It modfided for My requirement and I am shareing..

Dnot ask link it was some were in MSDN Blog or some where...


Let The Code start:-

private void AddWatermark(string WatermarkText)
{

Word.Selection Selection = ThisApplication.Selection;

Word.Shape wmShape;

//Select the section

this.Sections[1].Range.Select();

ActiveWindow.ActivePane.View.SeekView =

Word.WdSeekView.wdSeekCurrentPageHeader;

//Create the watermar shape

wmShape = Selection.HeaderFooter.Shapes.AddTextEffect(

Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,

WatermarkText, "Times New Roman", 1,

Microsoft.Office.Core.MsoTriState.msoFalse,

Microsoft.Office.Core.MsoTriState.msoFalse,

0, 0, ref missing);

//Set all of the attributes of the watermark
string strname ="";

try
{
strname = Selection.HeaderFooter.Shapes.get_Item(ref shapeName).Name;

Selection.HeaderFooter.Shapes.get_Item(ref shapeName).Delete();
}
catch (Exception ex)
{
}


wmShape.Select(ref missing);

wmShape.Name = "Avinash Tiwari";

wmShape.TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse;

wmShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

wmShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

wmShape.Fill.Solid();

wmShape.Fill.ForeColor.RGB = (int)Word.WdColor.wdColorGray25;

wmShape.Fill.Transparency = 0.5f;

wmShape.Rotation = 90.0f;

wmShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;

wmShape.Height = ThisApplication.InchesToPoints(2.82f);

wmShape.Width = ThisApplication.InchesToPoints(5.64f);

wmShape.WrapFormat.AllowOverlap = -1; //true

wmShape.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth;

wmShape.WrapFormat.Type = Word.WdWrapType.wdWrapNone; //3

wmShape.RelativeHorizontalPosition =

Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionRightMarginArea;

wmShape.RelativeVerticalPosition =

Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
;

wmShape.Left = (float)Word.WdShapePosition.wdShapeCenter;

wmShape.Top = (float)Word.WdShapePosition.wdShapeCenter;



//set focus back to document

ActiveWindow.ActivePane.View.SeekView =

Word.WdSeekView.wdSeekMainDocument;

}



private void DeleteWatermark()
{

Word.Selection Selection = ThisApplication.Selection;

//Select the section

this.Sections[1].Range.Select();

ActiveWindow.ActivePane.View.SeekView =

Word.WdSeekView.wdSeekCurrentPageHeader;

object shapeName = "PowerPlusWaterMarkObject1";

Selection.HeaderFooter.Shapes.get_Item(ref shapeName).Delete();

//set focus back to document

ActiveWindow.ActivePane.View.SeekView =

Word.WdSeekView.wdSeekMainDocument;

}



private void AddWM_Click(object sender, EventArgs e)
{

try
{

AddWatermark("Hello World");

}

catch (Exception ex)
{

MessageBox.Show(ex.Message);

}

}



private void DeleteWm_Click(object sender, EventArgs e)
{

try
{

DeleteWatermark();

}

catch (Exception ex)
{

MessageBox.Show(ex.Message);

}

}

No comments:

Post a Comment