Dotnet水印之3:Word文档加水印图片

一两年前的代码了,三个版本都在实际中运用过。



/// WORD文件加水印
/// </summary>
/// <param name="srcDocName">源WORD文件名</param>
/// <param name="SaveDocName">加水印后文件名</param>
/// <param name="ModelPicName">水印的图片文件</param>
/// <param name="top">离顶部距离</param>
/// <param name="left">离左边距离</param>
/// <returns>返回失败的错误信息1.返回空错误信息表示成功,2.返回非空,即为相应错误信息</returns>
public static string WordWaterMark(string srcDocName,string SaveDocName,string ModelPicName,float top,float left)
{

object Nothing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object filename =srcDocName;
object docname = SaveDocName;


//新建一个word应用
Word.Application WordApp = new Word.ApplicationClass();
//打开word对象
Word.Document WordDoc=null;
try
{
WordDoc = WordApp.Documents.Open(ref filename,ref Nothing, ref readOnly,ref Nothing,
ref Nothing, ref Nothing, ref Nothing,ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

WordDoc.PageSetup.DifferentFirstPageHeaderFooter=-1;//设置首页不同

WordDoc.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
WordDoc.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;
WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter("");

WordDoc.Application.ActiveWindow.Selection.Range.Select();
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekFirstPageHeader;//第一页开始插入.wdSeekCurrentPageHeader;
WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(ModelPicName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);
WordDoc.Application.Selection.ShapeRange.LockAspectRatio = Office.MsoTriState.msoTrue;
WordDoc.Application.Selection.ShapeRange.Height = 40f;
WordDoc.Application.Selection.ShapeRange.Width = 180f;
//WordDoc.Application.Selection.ShapeRange.Left = Convert.ToSingle(WdShapePosition.wdShapeRight);//居中
//WordDoc.Application.Selection.ShapeRange.Top = Convert.ToSingle(WdShapePosition.wdShapeTop);//居中
WordDoc.Application.Selection.ShapeRange.Left = left;
WordDoc.Application.Selection.ShapeRange.Top = top;
WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;
WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone; //
WordDoc.Application.Selection.ShapeRange.ZOrder(Office.MsoZOrderCmd.msoSendBehindText);//文本底下
WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;//相对于页面
WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
//WordDoc.Application.Selection.ClearFormatting();
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;

//文件保存
WordDoc.SaveAs(ref docname, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
ErrorString="";

}
catch(Exception ex)
{
ErrorString=ex.Message.Trim();
}
finally
{
if (WordDoc!= null)
{
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
}
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
GC.Collect();
}
//}
return ErrorString;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值