导出的Word中添加水印

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;

namespace sciencePraiseSystem.BLL
{
    public class ExportHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="text">水印文字</param>
        /// <param name="left">左间距</param>
        /// <param name="top">上间距</param>
        /// <param name="rotation"></param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="size">文字大小</param>
        /// <param name="transparency">透明度</param>
        public static void AddWatermark(Microsoft.Office.Interop.Word.Document doc, string text, Microsoft.Office.Interop.Word.WdShapePosition left,
            Microsoft.Office.Interop.Word.WdShapePosition top, float rotation, float height, float width, float size, float transparency,
            Microsoft.Office.Interop.Word.WdColor color= Microsoft.Office.Interop.Word.WdColor.wdColorGray375)
        {
            doc.Application.ActiveWindow.Selection.Range.Select();
            doc.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;

            doc.Application.Selection.HeaderFooter.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, text, "楷体", size,
                MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0).Select();
            Microsoft.Office.Interop.Word.ShapeRange oShapeRange = doc.Application.Selection.ShapeRange;
            oShapeRange.Name = DateTime.Now.ToString("yyyyMMddhhmmssff") + RandomHelper.CreateRandomCode(8);//水印名称

            oShapeRange.Rotation = rotation;
            oShapeRange.LockAspectRatio = MsoTriState.msoTrue;
            oShapeRange.Height = doc.Application.CentimetersToPoints(height);
            oShapeRange.Width = doc.Application.CentimetersToPoints(width);
            oShapeRange.WrapFormat.AllowOverlap = -1;
            oShapeRange.WrapFormat.Side = Microsoft.Office.Interop.Word.WdWrapSideType.wdWrapBoth;
            oShapeRange.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
            oShapeRange.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
            oShapeRange.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
            oShapeRange.Left = (float)left;
            oShapeRange.Top = (float)top;

            oShapeRange.TextEffect.NormalizedHeight = MsoTriState.msoFalse;
            oShapeRange.Line.Visible = MsoTriState.msoFalse;
            oShapeRange.Line.Transparency = 1.0f;
            oShapeRange.Fill.Visible = MsoTriState.msoTrue;
            oShapeRange.Fill.Solid();
            oShapeRange.Fill.ForeColor.RGB = (Int32)color;
            oShapeRange.Fill.Transparency = transparency;
            doc.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

            //Microsoft.Office.Interop.Word.HeaderFooter hdr = doc.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
            //hdr.Range.Delete();
            //Microsoft.Office.Interop.Word.HeaderFooter ftr = doc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
            //ftr.Range.Delete();
        }

    }
}
直接引用
原文地址:https://www.cnblogs.com/shadow-wolf/p/11049980.html