替换word中的数据,并给导入word的图片添加水印
Posted liguix
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换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;
以上是关于替换word中的数据,并给导入word的图片添加水印的主要内容,如果未能解决你的问题,请参考以下文章