• 替换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;
            }
  • 相关阅读:
    喵哈哈村的魔法考试 Round #19 (Div.2) B 快速加
    hdu5676 ztr loves lucky numbers DFS
    hdu 5950 Recursive sequence 矩阵快速幂
    AI大语音(三)—傅里叶变换家族
    AI大语音(二)——语音预处理
    AI大语音(一)——语音识别基础
    第二章:声学模型之EM算法
    量子力学2
    量子力学1
    C#学习系列(一)——C#和C++的区别
  • 原文地址:https://www.cnblogs.com/liguix/p/11528866.html
Copyright © 2020-2023  润新知