替换word中的数据,并给导入word的图片添加水印

复制代码
public static void ExportWord(string tempFilePath, string outPath, Dictionary<string, string> data, Dictionary<string, string> imageList)
        {

            using (FileStream stream = File.OpenRead(tempFilePath))
            {
                XWPFDocument doc = new XWPFDocument(stream);
                //遍历段落                  
                foreach (var para in doc.Paragraphs)
                {
                    ReplaceKey(para, data);
                }
                foreach (var key in imageList.Keys)
                {
                    //给word中导入数据
                    //创建新的一行
                    XWPFRun r2 = doc.CreateParagraph().CreateRun();
                    var dd = imageList[key];
                    var newPath= Path.GetDirectoryName(imageList[key]) + "\\" + System.IO.Path.GetFileNameWithoutExtension(imageList[key]) + DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(imageList[key]);
                    var newimage = AddWaterMark(imageList[key], newPath);
                    var img = Image.FromFile(newimage);

                    var widthEmus = (int)(400.0 * 9525);
                    var heightEmus = (int)(300.0 * 9525);

                    using (FileStream picData = new FileStream(newimage, FileMode.Open, FileAccess.Read))
                    {
                        r2.AddPicture(picData, (int)PictureType.PNG, System.IO.Path.GetFileName(imageList[key]), widthEmus, heightEmus);
                    }
                }
                //遍历表格      
                foreach (var table in doc.Tables)
                {
                    foreach (var row in table.Rows)
                    {
                        foreach (var cell in row.GetTableCells())
                        {
                            foreach (var para in cell.Paragraphs)
                            {
                                ReplaceKey(para, data);
                            }
                        }
                    }
                }
                using (var outFile = new FileStream(outPath, FileMode.Create))
                {
                    doc.Write(outFile);
                    //outFile.Close();
                }
                doc.Close();
                //写文件
                //FileStream outFile = new FileStream(outPath, FileMode.Create);

            }
        }
复制代码
复制代码
 /// <summary>
        /// 给图片加水印
        /// </summary>
        /// <param name="imgPath"></param>
        /// <param name="sImgPath"></param>
        /// <returns></returns>
        private static string AddWaterMark(string imgPath, string sImgPath)
        {
            using (Image image = Image.FromFile(imgPath))
            {
                try
                {
                    Bitmap bitmap = new Bitmap(image);

                    int width = bitmap.Width, height = bitmap.Height;
                    //水印文字
                    string text = "版权保密";

                    Graphics g = Graphics.FromImage(bitmap);

                    g.DrawImage(bitmap, 0, 0);
                    //获取或设置与此 Graphics 关联的插补模式
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    //获取或设置此 Graphics 的呈现质量
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    //在指定的位置使用原始物理大小绘制指定的 Image。
                    g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);

                    Font crFont = new Font("微软雅黑", 40, FontStyle.Bold);
                    SizeF crSize = new SizeF();
                    crSize = g.MeasureString(text, crFont);

                    //背景位置(去掉了. 如果想用可以自己调一调 位置.)
                    //graphics.FillRectangle(new SolidBrush(Color.FromArgb(200, 255, 255, 255)), (width - crSize.Width) / 2, (height - crSize.Height) / 2, crSize.Width, crSize.Height);

                    SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(120, 177, 171, 171));

                    //将原点移动 到图片中点
                    //g.TranslateTransform(width / 2, height / 2);
                    //以原点为中心 转 -45度
                    //g.RotateTransform(-45);
                    //在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串
                    g.DrawString(text, crFont, semiTransBrush, new PointF(0, 0));

                    //保存文件
                    bitmap.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);

                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }

            return sImgPath;
        }
复制代码

 

posted @   昵称已存在嘿  阅读(455)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
欢迎阅读『替换word中的数据,并给导入word的图片添加水印』
欢迎阅读『替换word中的数据,并给导入word的图片添加水印』
点击右上角即可分享
微信分享提示