2

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 { }
        }
    }
}  
  • From where you are opening a word document? like from computer or from within SharePoint? – Hardik Mar 15 '16 at 9:50
  • The word document is already on SharePoint document library in server. – Md. Tahmidul Abedin Mar 15 '16 at 10:51
6

I solved problem as the requirement, but in a unusual way. Though it is not the best solution.

As i can't solve the problem to prevent SharePoint asking for credentials in the time of open a word file programmatically and to insert Watermark into the file, need be opened. So i download the files on users computer, insert watermark and upload to the SharePoint library again programmatically.

This will help me for now. But as this not the best solution, if anyone has any please let me know.

|improve this answer|||||
  • Plz accept your own answer as answer, so the question will not appear in Unanswered Questions List – P S Mar 29 '16 at 12:21
  • was expecting a better solution. bt now i selected mine as answer. – Md. Tahmidul Abedin Mar 29 '16 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.