C# 实现 Word 加盖骑缝章效果

2024-01-25 19:04

本文主要是介绍C# 实现 Word 加盖骑缝章效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

目录

实现效果

范例运行环境

Office DCOM 配置

设计实现

创建stamp图章类 

电子章图片的计算与定位

旋转图片方法

总结 


实现效果

在OA的自动化处理系统中,通过审批的最终节点,可能会对WORD文件加盖电子章,比如定位带有指定文字的Range周围加盖电子章,骑缝章,甚至水印图片。比如如下效果图:

54f76cb8e6914b3092a5e991c3a83ae1.png

 cd92a2943a0d460dba329084920e7f9c.png

范例运行环境

操作系统: Windows Server 2019 DataCenter

操作系统上安装 Office Word 2016 ,客户端使用的 Office Word 2019

.net版本: .netFramework4.7.1 或以上

开发工具:VS2019  C#

Office DCOM 配置

请参考我的文章《C# 读取Word表格到DataSet》有对Office DCOM详细配置介绍,这里不再赘述。 

设计实现

创建stamp图章类 

导出WORD文件可以传入多个图章类(如果需要的话),图章类主要包括实现如下设置:

1、可设置三种图片(标准的盖章图片、骑缝章图片、水印图片)

2、标准的盖章图片是否显示,不显示则可以只显示骑缝章或水印图片,这个可以模拟多次盖骑缝章的效果

3、定位盖章文字,可以设置一下 x、y的偏移量,以校准指定的模板文件,达到最佳重叠效果。

4、可设置各种章的翻转角度(可随机选取)

示例代码如下: 

public class stamp{public string stampImageFilename = "";  //盖章图片public string stampImageFilename2 = "";  //骑缝章图片public string stampImageFilename3 = "";  //水印章图片public bool stampImageVisible = true;  //主章是否可显示public string findWord = "";   //查找盖章定位文字public int findWordOffsetX = 0; //查找盖章文字后,章的定位偏移量public int findWordOffsetY = 0; //查找盖章文字后,章的定位偏移量public int stamp2X = 0; //骑缝章偏移量public int stamp2Y = 0; //骑缝章偏移量public int roteAngle = 0; //骑缝章翻转角度,12点方向为0度,顺时针计算角度public bool roteReFix = false; //骑缝章翻转角度重新计算适应图片(多见于对角线)public bool randomRoteAngle = false; //骑缝章是否按指定角度的最大随机值提取public int stampImageWidth = 0; //章宽度public int stampImageHeight = 0; //章高度public string stamp2Direction = "right";  //骑缝章盖章方向 默认right ,包括 left/top/bottompublic int stampAngle = 0; //骑缝章翻转角度,12点方向为0度,顺时针计算角度public bool randomStampAngle = false; //骑缝章是否按指定角度的最大随机值提取public int stamp3X = 0; //水印章每页Xpublic int stamp3Y = 0; //水印章每页Ypublic int stamp3Angle = 0; //水印章翻转角度,12点方向为0度,顺时针计算角度}

电子章图片的计算与定位

可以创建多个图章类添加 ArrayList 中进行方法传递, 初始值为public ArrayList Stamps = null;

创建方法  public string setWordStamps(string _filename,ArrayList Stamps)

实现的功能大致如下:

1、主章根据提供查找的关键字,如 “盖章处:”、“盖章:”,然后添加图片重叠在文字的上方周围

2、骑缝章根据页数进行分割计算,每页分隔宽度不小于 1 像素

3、骑缝章可选择“盖”在页面的上下左右位置,如果多个位置方向都需要“盖”,则传递多个 stamp 图章类

4、章可以随机和指定旋转角度

示例代码如下:

public string setWordStamps(string _filename,ArrayList Stamps){Object Nothing =System.Reflection.Missing.Value;string _file="",_path=Path.GetDirectoryName(_filename)+"\\tempbfile\\",_ext="";_file=Path.GetFileNameWithoutExtension(_filename);_ext=Path.GetExtension(_filename);string _validfilename=Guid.NewGuid().ToString()+_ext;string _lastfile=_path+_validfilename;string _pdfFile = _path + Guid.NewGuid().ToString() + ".pdf";System.IO.File.Copy(_filename,_lastfile,true);if(!File.Exists(_lastfile)){return "";}//取得Word文件保存路径object filename=_lastfile;//创建一个名为WordApp的组件对象Word.Application WordApp=new Word.Application();//创建一个名为WordDoc的文档对象WordApp.DisplayAlerts=Word.WdAlertLevel.wdAlertsNone;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);WordDoc.SpellingChecked = false;WordDoc.ShowSpellingErrors = false;WordDoc.ActiveWindow.View.Type = Word.WdViewType.wdNormalView;//遍历stamp图章类foreach (stamp Stamp in Stamps){bool isfirst = true;int iii = 0;int selectStart = 0;ArrayList restoreRange = new ArrayList();while (true){iii++;bool findstamptext = false;if (Stamp.findWord != ""){WordApp.Selection.Range.Start = selectStart;Word.Find fnd = WordApp.Selection.Find;Object findText = Stamp.findWord;Object matchCase = false;Object matchWholeWord = Type.Missing;Object matchWildcards = false;Object matchSoundsLike = false;Object matchAllWordForms = false;Object forward = true;Object wrap = Word.WdFindWrap.wdFindContinue;Object format = false;Object replaceWith = "";Object replace = Type.Missing; ;Object matchKashida = Type.Missing;Object matchDiacritics = Type.Missing;Object matchAlefHamza = Type.Missing;Object matchControl = Type.Missing;if (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,ref forward, ref wrap, ref format, ref replaceWith, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl)){selectStart = WordApp.Selection.Range.Start;restoreRange.Add(WordApp.Selection.Range);findstamptext = true;}else{findstamptext = false;}}if (findstamptext == false){break;}Word.InlineShape pic = WordApp.Selection.Range.InlineShapes.AddPicture(Stamp.stampImageFilename, false, true);Word.Shape picshape = pic.ConvertToShape();picshape.WrapFormat.Type = Word.WdWrapType.wdWrapNone;if (Stamp.stampImageWidth != 0){picshape.Width = Stamp.stampImageWidth;}if (Stamp.stampImageHeight != 0){picshape.Height = Stamp.stampImageHeight;}float pagewidth = 0;float pageheight = 0;if (findstamptext == true){if (Stamp.stampAngle > 0){Random rnd = new Random();picshape.Rotation = Stamp.randomStampAngle == false ? Stamp.stampAngle : rnd.Next(Stamp.stampAngle);}pagewidth = WordApp.Selection.PageSetup.PageWidth;pageheight = WordApp.Selection.PageSetup.PageHeight;int ox = 0; int oy = 0; int ow = 0; int oh = 0;WordApp.Windows[1].GetPoint(out ox, out oy, out ow, out oh, WordApp.Selection.Range);WordApp.Selection.Range.Text = "";picshape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;picshape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;picshape.Left = (float)(ox * 0.405402299) - (picshape.Width / 2);picshape.Top = WordApp.Selection.Range.Information[Word.WdInformation.wdVerticalPositionRelativeToPage] - (picshape.Height / 2);if ((bool)WordApp.Selection.Range.Information[Word.WdInformation.wdWithInTable] == true){picshape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionCharacter;picshape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionLine;picshape.Left = 0;picshape.Top = 0;}}picshape.Left = picshape.Left + Stamp.findWordOffsetX;picshape.Top = picshape.Top + Stamp.findWordOffsetY;if (Stamp.stampImageVisible == false){picshape.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;}if (Stamp.stampImageFilename2 != ""&&isfirst==true){int ra = Stamp.roteAngle;if (ra > 0){Random rnd = new Random();ra = Stamp.randomRoteAngle == false ? ra : rnd.Next(ra);}Bitmap cc = (Bitmap)Image.FromFile(Stamp.stampImageFilename2);Bitmap bb = Rotate(cc, -ra, Stamp.roteReFix);
WordDoc.Windows[1].Panes[1].Pages;int pages2 = WordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref Nothing);if (pages2 == 1){pages2 = 0; //如果一页就不盖骑缝章}for (int pi = 1; pi <= pages2; pi++){Word.Range pagerange = WordDoc.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, pi.ToString());int rx = (pi - 1) * bb.Width / pages2;int ry = 0;int rw = bb.Width / pages2;int rh = bb.Height;if (Stamp.stamp2Direction == "bottom"){rx = 0;ry = (pi - 1) * bb.Height / pages2;rw = bb.Width;rh = bb.Height / pages2;}else if (Stamp.stamp2Direction == "left"){rx = (pages2 - pi) * bb.Width / pages2;ry = 0;rw = bb.Width / pages2;rh = bb.Height;}else if (Stamp.stamp2Direction == "top"){rx = 0;ry = (pages2 - pi) * bb.Height / pages2;rw = bb.Width;rh = bb.Height / pages2;}if (rw < 1 || rh < 1){continue;}Bitmap sepbitmap1 = bb.Clone(new System.Drawing.Rectangle(rx, ry, rw, rh), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);string temppng = "d:\\" + System.Guid.NewGuid().ToString() + ".png";sepbitmap1.Save(temppng);Word.InlineShape pic2 = pagerange.InlineShapes.AddPicture(temppng, false, true);Word.Shape picshape2 = pic2.ConvertToShape();picshape2.WrapFormat.Type = Word.WdWrapType.wdWrapNone;picshape2.Width = picshape.Width / pages2;picshape2.Height = picshape.Height;if (Stamp.stamp2Direction == "bottom" || Stamp.stamp2Direction == "top"){picshape2.Width = picshape.Width;picshape2.Height = picshape.Height / pages2;}picshape2.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;picshape2.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;picshape2.Left = pagewidth - picshape2.Width;picshape2.Top = Stamp.stamp2Y;if (Stamp.stamp2Direction == "bottom"){picshape2.Left = Stamp.stamp2X;picshape2.Top = pageheight - picshape2.Height;}else if (Stamp.stamp2Direction == "left"){picshape2.Left = 0;picshape2.Top = Stamp.stamp2Y;}else if (Stamp.stamp2Direction == "top"){picshape2.Left = Stamp.stamp2X;picshape2.Top = 0;}resultReport += string.Format("stamp2 {2} left: {0} top:{1} width:{3} height:{4}<br>", picshape2.Left, picshape2.Top,pi,picshape2.Width,picshape2.Height);File.Delete(temppng);}}//stamp2if (Stamp.stampImageFilename3 != ""&&isfirst==true){int ra = Stamp.stamp3Angle;if (ra > 0){Random rnd = new Random();ra = Stamp.randomRoteAngle == false ? ra : rnd.Next(ra);}Bitmap cc = (Bitmap)Image.FromFile(Stamp.stampImageFilename3);Bitmap bb = Rotate(cc, -ra, true);int pages2 = WordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref Nothing);resultReport += string.Format(" PageCount3:{0}<br>", pages2);for (int pi = 1; pi <= pages2; pi++){Word.Range pagerange = WordDoc.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, pi.ToString());int rx = (pi - 1) * bb.Width / pages2;rx = 0;int ry = 0;int rw = bb.Width;int rh = bb.Height;Bitmap sepbitmap1 = bb.Clone(new System.Drawing.Rectangle(rx, ry, rw, rh), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);string temppng = "d:\\" + System.Guid.NewGuid().ToString() + ".png";Word.InlineShape pic2 = pagerange.InlineShapes.AddPicture(temppng, false, true);Word.Shape picshape2 = pic2.ConvertToShape();picshape2.WrapFormat.Type = Word.WdWrapType.wdWrapNone;picshape2.Width = picshape.Width;picshape2.Height = picshape.Height;picshape2.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;picshape2.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;picshape2.Left = Stamp.stamp3X;//                       picshape2.Left = Stamp.stamp2X;picshape2.Top = Stamp.stamp2Y;File.Delete(temppng);}}//stamp3isfirst = false;}// whileforeach (Word.Range range in restoreRange){range.Text = Stamp.findWord;}}//foreachWordDoc.Save();WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);//关闭WordApp组件对象WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);return _lastfile;}

旋转图片方法

        public Bitmap Rotate(Bitmap b, int angle,bool fix=false){angle = angle % 360;//弧度转换double radian = angle * Math.PI / 180.0;double cos = Math.Cos(radian);double sin = Math.Sin(radian);//原图的宽和高int w = b.Width;int h = b.Height;int ow = w;int oh = h;int d = ((int)Math.Sqrt(Math.Pow(w - 0, 2) + Math.Pow(h- 0, 2))+1);if (fix == true){w = d;h = d;}int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));//目标位图Bitmap dsImage = new Bitmap(w, h);System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//计算偏移量System.Drawing.Point Offset = new System.Drawing.Point((W - w) / 2, (H - h) / 2);//构造图像显示区域:让图像的中心与窗口的中心点一致System.Drawing.Rectangle rect = new System.Drawing.Rectangle(fix==false?0:(d-ow)/2, fix == false ? 0 : (d-oh)/2, ow, oh);
//            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Offset.X, Offset.Y, w, h);//           System.Drawing.Point center = new System.Drawing.Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);System.Drawing.Point center = new System.Drawing.Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);g.TranslateTransform(center.X, center.Y);g.RotateTransform(360 - angle);//恢复图像在水平和垂直方向的平移g.TranslateTransform(-center.X, -center.Y);g.DrawImage(b, rect);//重至绘图的所有变换g.ResetTransform();g.Save();g.Dispose();//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);return dsImage;}

总结 

以上是实现设计的一些参考代码,在实际的使用中,可能还会遇到如下问题:

1、定位关键字的叠加效果不好,因此针对每一个模板文件均需要调整图片的x、y偏移量,以达到最佳效果

2、对于超多页面的文件(如几万页),骑缝的效果可能不佳,可以采取调整图片像素宽度,或拆分模板文件进行处理

示例代码仅作参考,欢迎大家评论指教!

这篇关于C# 实现 Word 加盖骑缝章效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/644299

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

防近视护眼台灯什么牌子好?五款防近视效果好的护眼台灯推荐

在家里,灯具是属于离不开的家具,每个大大小小的地方都需要的照亮,所以一盏好灯是必不可少的,每个发挥着作用。而护眼台灯就起了一个保护眼睛,预防近视的作用。可以保护我们在学习,阅读的时候提供一个合适的光线环境,保护我们的眼睛。防近视护眼台灯什么牌子好?那我们怎么选择一个优秀的护眼台灯也是很重要,才能起到最大的护眼效果。下面五款防近视效果好的护眼台灯推荐: 一:六个推荐防近视效果好的护眼台灯的

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M