In a SharePoint 2010 project of mine, i have to insert watermark in a word file using Event Receive. To do that i am trying to use Microsoft.Office.Interop.Word namespace. It do the job. But to insert the watermark to the document file, it need to open the file. And to do that this function is called:
WordApp.Application.Documents.Open(fileName);
And when it calls the function, it asks for credentials. But as my project requirements, i can't ask credentials on that time.
So it there any way to prevent SharePoint asking for credentials in this step? Or is their any other way to insert watermark in a word document without asking for credentials.
I am giving the function here which is used to insert watermark in word document which i used in my project.
public void AddWatermarkToFile(string text, string fileName, SPFolder currentListItem, SPWeb spWeb)
{
// word app
ApplicationClass WordApp = null;
// word document
Document document = null;
Microsoft.Office.Interop.Word.Shape nShape = null;
try
{
currentListItem.ParentWeb.AllowUnsafeUpdates = true;
WordApp = new ApplicationClass();
WordApp.Visible = true;
WordApp.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
Uri destUri = new Uri(fileName);
WebRequest req = WebRequest.Create(destUri);
req.Credentials = CredentialCache.DefaultCredentials;
// open
document = WordApp.Application.Documents.Open(fileName);
// add watermark
foreach (Section section in document.Sections)
{
nShape = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, text, "Tahoma", 9, MsoTriState.msoCTrue, MsoTriState.msoFalse, 0, 0);
nShape.Fill.Visible = MsoTriState.msoTrue;
nShape.Line.Visible = MsoTriState.msoFalse;
nShape.Fill.Solid();
nShape.Fill.ForeColor.RGB = (Int32)WdColor.wdColorGray65;
nShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
nShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
// center location
nShape.Left = (float)WdShapePosition.wdShapeCenter;
nShape.Top = (float)WdShapePosition.wdShapeCenter;
}
// save
document.Save();
currentListItem.ParentWeb.AllowUnsafeUpdates = false;
}
finally
{
// close excel document & application
if (document != null)
{
try
{
document.Close(WdSaveOptions.wdDoNotSaveChanges);
}
catch { }
}
if (WordApp != null)
{
try
{
WordApp.Quit();
}
catch { }
}
}
}