eugenebobalo
eugenebobalo

Reputation: 11

Inserting a Watermark as a text in Office.Interop.Word C#

I'm trying to insert a text watermark into a word document on every page of it and I'm receiving a reference not set to an instance of an object exception. It's in the following code snippet:

logoWatermark = wordApp.Selection.HeaderFooter.Shapes.AddTextEffect(
                Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
                "Enter The Text Here", "Arial", (float)60,
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                0, 0, ref oMissing);

It looks like I'm missing something. This is my code:

Word.Document doc = null;
Word.Application wordApp = null;
doc = wordApp.Documents.Open(filePath);
Word.Shape txWatermark = null;
foreach(Word.Section section in doc.Sections)
{
  txWatermark = wordApp.Selection.HeaderFooter.Shapes.AddTextEffect(
                Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
                "Enter The Text Here", "Arial", (float)60,
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                0, 0, ref oMissing);
  txWatermark.Select(ref oMissing);
  txWatermark.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
  txWatermark.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
  txWatermark.Fill.Solid();
  txWatermark.Fill.ForeColor.RGB = (Int32)Word.WdColor.wdColorGray30;
  txWatermark.RelativeHorizontalPosition = 
                         Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
  txWatermark.RelativeVerticalPosition = 
                             Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
  txWatermark.Left = (float)Word.WdShapePosition.wdShapeCenter;
  txWatermark.Top = (float)Word.WdShapePosition.wdShapeCenter;
            txWatermark.Height = wordApp.InchesToPoints(2.4f);
            txWatermark.Width = wordApp.InchesToPoints(6f);
}

doc.SaveAs2(oOutputFileName, oSavePDFFormat);

Upvotes: 1

Views: 960

Answers (2)

eugenebobalo
eugenebobalo

Reputation: 11

The exception was coming from the HeaderFooter side of it. I didn't assign the shape properly. I figured out how to make it work. I figured it out. This is how it must be set up.

 txWatermark = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(
                                        MsoPresetTextEffect.msoTextEffect1,
                                        "Watermak text!", "Arial", (float)60,
                                         MsoTriState.msoTrue,
                                         MsoTriState.msoFalse,
                                         0, 0, ref oMissing);

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The problematic line of code contains multiple property and method calls:

txWatermark = wordApp.Selection.HeaderFooter.Shapes.AddTextEffect(

So, it is not clear which property or method exactly throws an exception at runtime. To understand which line causes the issue you need to declare each property or method call on a separate line of code. Following that you will be able to find the exact property (or method) which fails and find a possible workaround.

Upvotes: 1

Related Questions