给Office文档添加水印效果【测试有效】
2024-08-28 02:41:57
private void button1_Click(object sender, EventArgs e)
{
string test1 = "C:\\test.docx";//源文件
string test2 = "D:\\test.docx";//添加后的文件
AddWaterMark("test", test1, test2);//添加水印
} public void AddWaterMark(string docName, string FilePath, string SavePath)
{
System.Windows.Forms.Application.DoEvents();
object Nothing = System.Reflection.Missing.Value;
object filename = FilePath;
object docname = SavePath;
//图片存放位置
String logoPath = "D:\\1.jpg";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, 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);
try
{
WordDoc.Application.ActiveWindow.Selection.Range.Select();
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
//增加水印图片
WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);
//增加水印文字
WordDoc.Application.Selection.HeaderFooter.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Dog", "Arial", (float)100, MsoTriState.msoTrue, MsoTriState.msoFalse, 0, 0, ref Nothing).Select(ref Nothing);
WordDoc.Application.Selection.ShapeRange.Name = "WordPictureWatermark1";
WordDoc.Application.Selection.ShapeRange.LockAspectRatio = MsoTriState.msoTrue;
WordDoc.Application.Selection.ShapeRange.Height = 845f;
WordDoc.Application.Selection.ShapeRange.Width = 595f;
WordDoc.Application.Selection.ShapeRange.Left = -999998f; //WdShapePosition.wdShapeCenter;居中
WordDoc.Application.Selection.ShapeRange.Top = -999999f;// WdShapePosition.wdShapeCenter;居中
WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;
WordDoc.Application.Selection.ShapeRange.LayoutInCell = 0;
WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone; //
WordDoc.Application.Selection.ShapeRange.ZOrder(MsoZOrderCmd.msoSendBehindText);//文本底下
WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
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, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception ee)
{
}
finally
{
}
}
}
最新文章
- thinkphp自定义标签库
- I/O 请求数据包
- Oracle Linux
- android 点击屏幕关闭 软键盘
- 渐变背景 css3渐变效果及代码
- Solaris桌面CDE
- [转]javascript中style.left和offsetLeft的使用
- Linux中应用程序如何使用系统调用syscall
- java Double保留小数点位数
- PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings.
- Jdbc访问数据库篇
- 【扩展欧几里得】NOIP2012同余方程
- Python 练习——计算1-2+3-4...+99
- 使用Fiddle抓取IOS手机
- 定制FileField中的上传文件名称
- 深入理解Linux网络技术内幕——PCI层和网络接口卡
- C# 中 PadLeft和PadRight 的用法
- Android 权限信息
- 基于.Net下整合RestSharp,实现REST服务客户端
- ffpmeg下rtmp踩坑记录
热门文章
- HDU 5461:Largest Point
- vs2012(或2013)与虚拟机连调试
- 抗干扰性极强非接触式读卡13.56mhz芯片:SI522
- COGS1487 麻球繁衍
- 十四、CI框架之数据库以参数形式插入操作
- Resource interpreted as Stylesheet but transferred with MIME || DevTools failed to parse SourceMap:
- jobs|ps|杀死nohup
- Q6:ZigZag Conversion
- aliyun二级域名绑定
- LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)