zoukankan      html  css  js  c++  java
  • 替换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;
            }
  • 相关阅读:
    内存溢出常见原因分析
    内存溢出,与内存泄露
    ping不通linux服务器排查
    内存,缓存,cpu,硬盘关系
    cpu 基础知识
    sql查询以及常见问题理解解析
    月入一万,活在北京 (zz)
    超越信息和通讯 迎接新一轮数字化变革 (转载)
    The specified DSN contains an architecture mismatch between the Driver and Application (zz.IS2120@B)
    Excel .xls文件导入、导出 的例子
  • 原文地址:https://www.cnblogs.com/liguix/p/11528866.html
Copyright © 2011-2022 走看看