Word文档加水印需要引用Office组件,我把组件复制到本地项目,然后引用。最后发现水印图片没有加到文字的后面而是插入到了文字中间。部署到服务器上测试时,无法加载水印。请哪位高手帮忙解决吧,小弟实在没办法了!!!下面是代码:
public byte[] AddMarkWord(string path, string ext, int state)
        {
            object Nothing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.ApplicationClass wordAppObj = null;
            Microsoft.Office.Interop.Word.Document WordDoc = null;
            // 水印图片
            string Name = HttpContext.Current.Server.MapPath(string.Format("~/Images/Mark/Contract/{0}.png", state));
            SysDrawing.Image Pic = SysDrawing.Image.FromFile(Name);            object srcFileName = path;
            object dstFileName = "C:\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ext;
            try
            {
                object obj = true;
                wordAppObj = new Microsoft.Office.Interop.Word.ApplicationClass();
                WordDoc = wordAppObj.Documents.Open(ref srcFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                Microsoft.Office.Interop.Word.Shape oShape;
                WordDoc.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
                WordDoc.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;                int iWidth = (int)WordDoc.PageSetup.PageWidth;
                int iHeight = (int)WordDoc.PageSetup.PageHeight;                int iLeft = 0;
                if (iWidth > Pic.Width)
                {
                    iLeft = (iWidth - Pic.Width) / 4;
                }                int iTop = 36;
                if (iHeight > Pic.Height)
                {
                    iTop = (iHeight - Pic.Height) / 3;
                }                object top = iTop;
                object left = iLeft;
                object width = Pic.Width;
                object height = Pic.Height;                oShape = WordDoc.ActiveWindow.ActivePane.Selection.HeaderFooter.Shapes.AddPicture(Name, ref Nothing,
                    ref Nothing, ref left, ref top, ref width, ref height, ref Nothing);
                oShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapInline;
                oShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText);
                WordDoc.SaveAs(ref dstFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            }
            catch (Exception ex)
            {
                throw new WarningException("Add Mark Word:" + ex.Message);
            }
            finally
            {
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                wordAppObj.Quit(ref Nothing, ref Nothing, ref Nothing);
            }            FileStream fs = new FileStream(dstFileName.ToString(), FileMode.Open, FileAccess.Read);
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            fs.Dispose();            System.IO.File.Delete(dstFileName.ToString());            return bytes;
        }Word水印