码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-16 19:16:25      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:The   cep   ima   中心   close   lis   pre   替换   bit   

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;
        }

 

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

标签:The   cep   ima   中心   close   lis   pre   替换   bit   

原文地址:https://www.cnblogs.com/liguix/p/11528866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!