Watermark location is different in word and PDF document in MS interop C#

Viewed 64

In my program, there is a function with which users can add watermark to docx or PDF files.

Application wordApp = new Application();
wordApp.Visible = false;
wordApp.ScreenUpdating = false;
Microsoft.Office.Interop.Word.Document wordDoc = null;
wordDoc = wordApp.Documents.Open(file.path, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o);
wordDoc.Activate();

if (file.watermark == true)
{
    Microsoft.Office.Interop.Word.Shape wordShape = null;
    foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)
    {
        wordShape = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(
                         Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
                         "What a lovely day", "Arial", (float)30,
                         Microsoft.Office.Core.MsoTriState.msoTrue,
                         Microsoft.Office.Core.MsoTriState.msoFalse,
                         150, 150, ref o);
        wordShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
        wordShape.Fill.Solid();
        wordShape.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Word.WdColor.wdColorGray15;
        wordShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
        wordShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
        wordShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
        wordShape.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter; // here I set the location to center
        wordShape.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter; 
        wordShape.Rotation = -45;
    }
}

if (file.PDFGenerate == true)
{
    object wordFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
    wordDoc.SaveAs2(file.newPath + ".pdf", ref wordFileFormat, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o);
    wordDoc.Close(oFalse, o, o);
    wordApp.Quit(o, o, o);
    
}
if (file.PDFGenerate != true) 
{
    
    wordDoc.SaveAs2(file.newPath +".docx", ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    wordDoc.Close();
    wordApp.Quit();
}

The problem is that the location of the added watermark is not identical as you can see in the uploaded pictures.

watermark of pdf

watermark of docx

In PDF files, watermarks are correctly placed in the center of the document, while in docx files, they aren't. I'd like to fix this problem, keeping the structure, algorithm of my code. I appreciate any help. Thanks a lot in advance.

1 Answers
foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)
            {
                Microsoft.Office.Interop.Word.Shape wordShape = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect
                (Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "What a lovely day", "Arial", (float)30, Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, o);
                
                wordShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                wordShape.Fill.Solid();
                wordShape.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Word.WdColor.wdColorGray15;
                wordShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
                wordShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
                wordShape.Rotation = -45;
                wordShape.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
                wordShape.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
                wordShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
                wordShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
            }
Related