在Microsoft Word文档中添加水印

时间:2016-07-11 13:40:49

标签: c# ms-word vsto watermark

我正在尝试使用VSTO和Shapes.AddTextEffect向Microsoft Word标题添加水印,它似乎正确添加,直到我检查不同的第一页和不同的奇数&甚至是页眉和页脚设计中的页面复选框。

似乎只是将它添加到Odd页面标题中,我不确定为什么当我传入不同的标题类型时,在下面找到我的代码。

private static void UpdateWatermark(ComObjectWrapper<Document> doc, string watermarkText, string watermarkName, Style style)
    {
        foreach (Section section in doc.Resource.Sections)
        { 
            if (!watermarkText.IsNullOrEmpty())
            {
                InsertWatermark(section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary], section, watermarkText, watermarkName + (int)WdHeaderFooterIndex.wdHeaderFooterPrimary, style);
                if (section.PageSetup.DifferentFirstPageHeaderFooter == -1)
                    InsertWatermark(section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage], section, watermarkText, watermarkName + (int)WdHeaderFooterIndex.wdHeaderFooterFirstPage, style);
                if (section.PageSetup.OddAndEvenPagesHeaderFooter == -1)
                    InsertWatermark(section.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages], section, watermarkText, watermarkName + (int)WdHeaderFooterIndex.wdHeaderFooterEvenPages, style);
            }
        }
    }

 private static void InsertWatermark(HeaderFooter header, Section section, string watermarkText, string watermarkName, Style style)
    {
        var wm = header.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, watermarkText,
            style.WatermarkStyle.FontFamily,
            style.WatermarkStyle.FontSize ?? 72,
            MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0, header.Range);
        wm.Visible = MsoTriState.msoFalse;
        wm.Name = watermarkName;
        wm.Fill.ForeColor.RGB = (int)WdColor.wdColorGray10;
        wm.Fill.BackColor.RGB = (int)WdColor.wdColorGray10;
        wm.Fill.Transparency = 0.2f;
        wm.Fill.Solid();
        wm.Rotation = -45;

        wm.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
        wm.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
        wm.Height = style.WatermarkStyle.Height != null
            ? (float)style.WatermarkStyle.Height
            : section.PageSetup.PageHeight - 400;
        wm.Width = section.PageSetup.PageWidth - 200;
        wm.WrapFormat.AllowOverlap = -1;
        wm.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
        wm.WrapFormat.Type = WdWrapType.wdWrapNone;

        wm.Left = (float)WdShapePosition.wdShapeCenter;
        wm.Top = (float)WdShapePosition.wdShapeCenter;
        wm.Visible = MsoTriState.msoCTrue;
    }

1 个答案:

答案 0 :(得分:0)

  

<ul class="nav"> <li class="active"><a href="#">Home</a></li> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> </ul>

这个枚举是否正确? 我还没有评论但是=)