none
Exception while setting watermark in doc file. RRS feed

  • Question

  • Hello Team,
    I am trying to add watermark to doc file using c#, to set watermark in file,i created a shape object using shapes.AddPicture(), shape is not null, 

    when i try to set visibility for shape then i am getting exception which is uncaught, due to this internal exception doc file is not getting opened.

    the exception is happening for below lines
    1. shape.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
    2. shape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;

    i tried to assign Microsoft.Office.Core.MsoTriState.msoFalse to other variable {Microsoft.Office.Core.MsoTriState var= Microsoft.Office.Core.MsoTriState.msoFalse}
    and it works, 

    looks like issue is while binding value of Microsoft.Office.Core.MsoTriState to shape object
    {issue is for doc file only and not for docx, this is happening when i am opening doc file from shared location}


    see my code below,
    --------------------------------------------------------------- 
        public override void SetWatermark(object o, object watermark)
            {
                Word.Section t = (Word.Section)o;
                string text = watermark.ToString();
                Word.HeaderFooter headerFooter = null;
                Word.PageSetup pageSetup = null;
                Microsoft.Office.Interop.Word.Shape shape = null;
                Word.HeadersFooters headersFooters = null;
                Word.Shapes shapes = null;
                try
                {           
                    headersFooters = t.Headers;
                    if (headersFooters == null) 
                    { headersFooters = t.Footers; }
                    if (headersFooters == null) 
                    { throw new Exception("Can't set watermark - 
                       Null headers and footers"); }

                    headerFooter= 
                    headersFooters[Word.WdHeaderFooterIndex.
                      wdHeaderFooterPrimary];

                    pageSetup = t.PageSetup;

                    string path = HFWUtils.SetAndGetWatermarkPath
                    (text, "doc", pageSetup.PageWidth, 
                    pageSetup.PageHeight, 90, true);

                    object linkToFile = false;
                    object saveWithDocument = true;

                    shapes = headerFooter.Shapes;
                    shape = shapes.AddPicture
                    (path, linkToFile, saveWithDocument);

                    //***this line throws exception ***
                    shape.Visible = 
                    Microsoft.Office.Core.MsoTriState.msoFalse; 

                    shape.Name = HFWType.CxWatermark.ToString();

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

                    // Set relative size and position according to 
                      page margins
                    shape.RelativeVerticalPosition = Word.
                    WdRelativeVerticalPosition.
                    wdRelativeVerticalPositionMargin;

                    shape.RelativeVerticalSize = Word.
                    WdRelativeVerticalSize.
                    wdRelativeVerticalSizeMargin;

                    shape.RelativeHorizontalPosition = Word.
                    WdRelativeHorizontalPosition.
                    wdRelativeHorizontalPositionMargin;

                    shape.RelativeHorizontalSize = Word.
                    WdRelativeHorizontalSize.
                    wdRelativeHorizontalSizeMargin;


                    shape.Top =(float)Word.
                    WdShapePosition.wdShapeCenter;
                    shape.Left = (float)Word.WdShapePosition.
                     wdShapeCenter; ;

                    shape.Visible = Microsoft.Office.Core.
                     MsoTriState.msoTrue;

                    }     
                    finally
                    {
                        //code to release com objects
                    }          
                }

    Not able to attach screenshot, hence putting here exception details,

    exception details - " Exception thrown at 0x68ECCCAB (WWLIB.DLL) in WINWORD.EXE:0x0000005: Access violation writing location 0x00000014 "

                                                                                                                                                 
    Friday, July 10, 2020 10:11 AM

All replies

  • This is better asked in the Word for developers forum.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 10, 2020 10:33 AM