none
Add Watermark to Word 2007 document via automation

    Question

  • I have an automation application that adds a custom Watermark image to a document.  After the watermark image is added, when the user types information into the document, the display of the characters lags behind the typing by a second or two.  This is very annoying to power typist.   This only happens when the image is visable (i.e. Print View) and not in Draft View.  If I add the watermark image manually to a document, the typing delay does not occur.

    The following is the code fragment that addes the Watermark image:

    private void addWatermark(Word.Document doc)
    {
        
    object refmissing = System.Reflection.Missing.Value;
        
    string watermarkPath = Path.Combine(Directory.GetCurrentDirectory(), "Watermark.jpg");

        
    if (File.Exists(watermarkPath))
        {

            object
    linkToFile = false
    ;
            
    object saveWithDocument = true;

            
    doc.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
            
    Word.WdHeaderFooterIndex hfIndex = Word.WdHeaderFooterIndex.wdHeaderFooterPrimary;

            Word.HeaderFooter headerFooter;

            if
    (doc.Sections[1].Headers != null
    )
            
    {
                
    headerFooter = doc.Sections[1].Headers[hfIndex];
            }
            
    else if (doc.Sections[1].Footers != null)
            
    {
                
    headerFooter = doc.Sections[1].Footers[hfIndex];
            
    }
            
    else
            {
                headerFooter =
    null;
            
    }

            if (headerFooter != null)
            {

                try
                
    {
                    
    Word.Shape watermarkShape;
                    watermarkShape = headerFooter.Shapes.AddPicture(watermarkPath,
    ref linkToFile, ref saveWithDocument, ref refmissing, 
                                                                    
    ref refmissing, ref refmissing, ref refmissing, ref refmissing);
                
    }
                
    catch (System.Runtime.InteropServices.COMException e)
                
    {
                    
    Debug.WriteLine(
    String.Format("{0} - Error Code = {1}", e.Message, e.ErrorCode));
                    
    throw e;
                
    }
            
    }
            
    this.word.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
            
    this.word.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
        }
        
    else
        
    {
            
    Debug.WriteLine(
    "Could not find watermark image");
        
    }
    }

    I'm using Visual Studio 2008, and the Microsoft Word 12.0 Object Library.  The image is a black and white jpeg this is 8.5" x 11". 

    How can I fix this? 


    DDrabo
    Tuesday, January 13, 2009 5:25 PM

Answers

  • Mmmm. View.SeekView is not a reliable method to work with headers and footers programmatically. I suspect (but don't know) that this is where the problem could be.

    Instead, work directly with the HeaderFooter object (in the case of Shapes) or with the HeaderFooter.Range object (for text). In your code, just comment out the lines that use SeekView and see what difference that makes?

    And I'd put the line with View.Type before you start manipulating the HeaderFooter, as that's how Word prefers to work.
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tim Li Wednesday, January 21, 2009 2:51 AM
    Thursday, January 15, 2009 8:37 AM