none
Add Custom Water Mark in word Document using VSTO RRS feed

  • Question

  • Hi, I found some article on 

    "http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/57011a5d-d521-466e-9c1f-abf08c86367a

    and another blog http://blogs.msdn.com/b/pstubbs/archive/2005/03/08/389666.aspx  regarding the method of adding watermark on word document.

    Following the steps i am able to add a picture as water mark . My code is

     

    Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
       Microsoft.Office.Interop.Word.Shape logoCustom = null;
       object oMissing = System.Reflection.Missing.Value;
       object oFalse = false;
       object oTrue = true;
       String logoPath = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\myimage.jpg";
       logoCustom = Globals.ThisAddIn.Application.Selection.HeaderFooter.Shapes.AddPicture(logoPath,
        ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
       logoCustom.Select(ref oMissing);
       logoCustom.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
       logoCustom.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeLeft;
       Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
    
    

    But my problem is i need to specify the Scale Property as 100% and wrap property as NoWrap  for the shape. How can i achieve this?

    I am not getting any effect of this code (logoCustom.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;)


     


    jibin.mn
    Tuesday, June 7, 2011 10:24 AM

Answers

  • Hi,

     i am able to solve the problem of getting line on top and left

    What i did is set  all margins as 0

     thisdoc.PageSetup.LeftMargin = 0;

     thisdoc.PageSetup.RightMargin = 0;

     thisdoc.PageSetup.TopMargin = 0;

     thisdoc.PageSetup.BottomMargin = 0;

    Then place the image using the coe 

     Microsoft.Office.Interop.Word.Shape bgImage = null;
          object missing = System.Reflection.Missing.Value;
          String logoPath = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\VBPolska.jpg";
          bgImage = Globals.ThisAddIn.Application.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(logoPath,
                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
          bgImage.Top = thisdoc.PageSetup.PageHeight - (thisdoc.PageSetup.PageHeight+thisdoc.PageSetup.HeaderDistance);
    

    last line make sure that image is in top left corner

    now the problem is solved.

      But when i change the margin property to 1 

    thisdoc.PageSetup.LeftMargin = 1;

    the left line again comes in the document .. Any way to solve it .I hope now you got a clear picture of the problem. (I thought all these happends because of the size of image)

     

     

     

     


    jibin.mn
    • Proposed as answer by Bruce Song Tuesday, June 14, 2011 1:13 PM
    • Marked as answer by Bruce Song Tuesday, June 28, 2011 12:27 PM
    Friday, June 10, 2011 8:53 AM

All replies

  • This thread might help.

    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/0c912774-c55d-4fa0-a22a-50ad3ce0e0d6


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, June 7, 2011 12:57 PM
  • Hi Jibin

    mmm. I very much recommend you avoid using SeekView and Select when working with Shapes in the header/footer of the document. Comment out those lines, and change the one that adds the picture:

    Globals.ThisAddIn.Application.Selection.Sections[1].Headers [Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(//params here);

    as to the Wrap property: Could you please explain how you want the text flow to interact with this watermark? Usually, you want text to "flow" over a watermark. If you use wdWrapNone then the text will NOT flow over the graphic and I don't think you want that. I think what you want is wdWrapBehind.

    There is no Scale property, there's ScaleHeight and ScaleWidth. You might also want to consider setting LockAspectRatio before scaling.


    Cindy Meister, VSTO/Word MVP
    Tuesday, June 7, 2011 3:02 PM
    Moderator
  • Hi Cindy,

    Regarding Wrap Property you are right , i need wdWrapBehind and i got the effect

    Why i am asking for Scale property is my image is of high size(  means width*height  is  2480 * 3508 ) and when i place this image as water mark using this code

    (Modified based ob your suggestion by avoid using SeekView and selection and now its like this)

     Microsoft.Office.Interop.Word.Shape bgImage = null;
          object missing = System.Reflection.Missing.Value;
          String logoPath = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\needed.jpg";
          bgImage = Globals.ThisAddIn.Application.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(logoPath,
                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
    

    The result i got is like this  "http://www.divshare.com/img/15043960-326.jpg"

    A border like appearence on top and left( I thought its because of the high width and height. Since if i add a small image this will not happen)

    How can i overcome this. (By selecting Picture, picture format tools > Picture Styles as softedge rectangles then this effect is removed.But i am not able to find any property for setting Picture Styles from code )

    Thanks

     

     


    jibin.mn
    Wednesday, June 8, 2011 4:39 AM
  • Hi Jibin

    <<A border like appearence on top and left( I thought its because of the high width and height. Since if i add a small image this will not happen)

    How can i overcome this. (By selecting Picture, picture format tools > Picture Styles as softedge rectangles then this effect is removed.But i am not able to find any property for setting Picture Styles from code )>>

    Which version of Word are we discussing?

    Have you checked whether this border is actually in the graphic (opened it in a graphics program)?

    If you open the graphic in Word and scale it manually, does that border disappear?

    I did give you the Shape properties for changing the pictures size in my previous reply...


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 8, 2011 5:16 AM
    Moderator
  • Hi,

    I  am using Word 2010 and vs2010 for creating addin.

    If i use a small image. ( say of width=50 and height =50). then the  border like appearance on top and left is not there .

    And i am sure that this border is not in the graphics( since If i add this image from the watermark option of word then this border is not coming )

     

    Is any way to use  Picture Styles as softedge rectangles for a shape object

    Thanks


    jibin.mn
    Wednesday, June 8, 2011 5:22 AM
  • Thank you for the information about the version of Word. These advanced graphics properties aren't available in all versions, which is why I had to know...

    See: Shape.SoftEdge.Type = Office.MsoBevelType.msoSoftEdgeTypeNone


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 8, 2011 5:35 AM
    Moderator
  • Hi 

    I am not able to find a property like this

    Shape.SoftEdge.Type = Office.MsoBevelType.msoSoftEdgeTypeNone

    (For Office.  only 3 options are there Core , Interop and Tols. There is no property called   MsoBevelType)

    But i found 

    Shape.SoftEdge.Type = Microsoft.Office.Core.MsoSoftEdgeType.msoSoftEdgeTypeNone;

    I add the above code but no change in result. Am i missing some additional namespaces to use MsoBevelType

     

    I also found

     Microsoft.Office.Core.MsoBevelType.msoBevelNone;  but we cant assign that as SoftEdge.Type  for a shape


    jibin.mn
    Wednesday, June 8, 2011 5:50 AM
  • Hi Jibin

    <<Shape.SoftEdge.Type = Microsoft.Office.Core.MsoSoftEdgeType.msoSoftEdgeTypeNone>>

    Yes, that would be the right one (I was having to search the MSDN Developer Reference because the standard Help files weren't available this morning, so it was a bit of guesswork involved).

    <<I add the above code but no change in result. Am i missing some additional namespaces to use MsoBevelType>>

    As far as I know, that's all there is. Have you tried printing the result? Sometimes, it's a question of the monitor...


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 8, 2011 10:25 AM
    Moderator
  • Hi,

     i am able to solve the problem of getting line on top and left

    What i did is set  all margins as 0

     thisdoc.PageSetup.LeftMargin = 0;

     thisdoc.PageSetup.RightMargin = 0;

     thisdoc.PageSetup.TopMargin = 0;

     thisdoc.PageSetup.BottomMargin = 0;

    Then place the image using the coe 

     Microsoft.Office.Interop.Word.Shape bgImage = null;
          object missing = System.Reflection.Missing.Value;
          String logoPath = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\VBPolska.jpg";
          bgImage = Globals.ThisAddIn.Application.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(logoPath,
                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
          bgImage.Top = thisdoc.PageSetup.PageHeight - (thisdoc.PageSetup.PageHeight+thisdoc.PageSetup.HeaderDistance);
    

    last line make sure that image is in top left corner

    now the problem is solved.

      But when i change the margin property to 1 

    thisdoc.PageSetup.LeftMargin = 1;

    the left line again comes in the document .. Any way to solve it .I hope now you got a clear picture of the problem. (I thought all these happends because of the size of image)

     

     

     

     


    jibin.mn
    • Proposed as answer by Bruce Song Tuesday, June 14, 2011 1:13 PM
    • Marked as answer by Bruce Song Tuesday, June 28, 2011 12:27 PM
    Friday, June 10, 2011 8:53 AM