替换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 * );
var heightEmus = (int)(300.0 * ); 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, , );
//获取或设置与此 Graphics 关联的插补模式
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//获取或设置此 Graphics 的呈现质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//在指定的位置使用原始物理大小绘制指定的 Image。
g.DrawImage(image, new Rectangle(, , width, height), , , width, height, GraphicsUnit.Pixel); Font crFont = new Font("微软雅黑", , 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(, , , )); //将原点移动 到图片中点
//g.TranslateTransform(width / 2, height / 2);
//以原点为中心 转 -45度
//g.RotateTransform(-45);
//在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串
g.DrawString(text, crFont, semiTransBrush, new PointF(, )); //保存文件
bitmap.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); }
catch (Exception e)
{
return e.Message;
}
} return sImgPath;
}
替换word中的数据,并给导入word的图片添加水印的更多相关文章
- Python 读取word中表格数据、读取word修改并保存、替换word中词汇、读取word中每段内容,读取一段话中相同样式内容,理解Document中run
from docx import Document path = r'D:\pywork\12' # word信息表所在文件夹 w = Document(path + '/' + 'word信息表.d ...
- Word中截取部分内容并保存为jpg图片的方法
private void button1_Click(object sender, EventArgs e) { var appWord = new Microsoft.Office.Interop. ...
- Java 添加、删除、替换、格式化Word中的文本(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本.deleteTextRange()删除文本.updateTextRang ...
- Word中批量替换软回车
在平时工作中,有时候需要拷贝一些截取自网页上的文字,当选中后拷贝到Word中时,有时候在每行的结尾出现如下的符号,,这给后期文字的整理带来了很多不便,在此记录从网上获取的解决方法,以免遗忘和便于查找. ...
- 富文本编辑器直接从 word 中复制粘贴公式
在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...
- PLSQL_数据泵Datapump导入导出数据IMPDP / EXPDP(概念)(Oracle数据导入导出工具)(转)
一.摘要 在平常备库和数据库迁移的时候,当遇到大的数据库的时候在用exp的时候往往是需要好几个小时,耗费大量时间.oracle10g以后可以用expdp来导出数据库花费的时间要远小于exp花费的时间, ...
- 图解JanusGraph系列 - 关于JanusGraph图数据批量快速导入的方案和想法(bulk load data)
大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 图数据库文章总目录: 整理所有图相关文章,请移步(超链):图数据库系列-文章总目录 源码分析相关可查看github(码文不易,求个sta ...
- 在word中使用notepad++实现代码的语法高亮
转载自:http://blog.csdn.net/woohello/article/details/7621651 有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩 ...
- Word中高效输入公式:内嵌公式和Mathtype
Word中高效输入公式:内嵌公式和Mathtype 前言:对于理工科学生而言,公式输入必不可缺.LaTeX相比Word,在公式输入及排版方面更强大.但是对于轻量级的任务,用Word而言更加轻便(起码不 ...
随机推荐
- JQuery .width()/.css("width")方法 比较
1. 获取到的值的区别 获取到的为实际宽度,不包括 内边距 和 边框: <div id="aa"> ...... </div> // 1. width() ...
- Shell之文本排序命令
目录 Shell之文本排序命令 参考 wc命令 sort命令 uniq命令 Shell之文本排序命令
- ArcGIS Server10.1 动态图层服务
动态图层的应用场景: 1 改变现有图层:符号,渲染方式和版本,这些都可以通过客户端请求的时候给定相应的参数来进行设置,从而来达到轻易改变地图的效果. 2 添加地图服务中没有的图层 添加的数据可以是矢量 ...
- Linux——服务器版本安装 (VMware)
一.Linux简介 Linux是一套免费使用和自由传播的类UNIX操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网 ...
- 快学Scala 第十三课 (类型层级,对象相等性)
Scala 类型层级: 对象相等性: 和Java一样要重写equals方法和hashcode方法 class Student(val id: Int, val name: String) { over ...
- .netcore 开发的 iNeuOS 物联网平台部署在 Ubuntu 操作系统,无缝跨平台。助力《2019 中国.NET 开发者峰会》。
2019 中国.NET 开发者峰会正式启动 目 录 1. 概述... 2 2. 准备运行程序包... 2 3. 安装.netcore. 3 4. 安 ...
- @ConfigurationProperties、@Value、@PropertySource
@ConfigurationProperties(spring-boot依赖下).@Value(spring-beans依赖下).@PropertySource(spring-context依赖下) ...
- 前端css杂记
1em等于16px,页面默认是16px,一个字宽高都等于16px; 当行内元素display:inline的元素设置float:left浮动后,display值也被设置成block;总结:无论行内元素 ...
- Python3实战spark大数据分析及调度 ☝☝☝
Python3实战spark大数据分析及调度 ☝☝☝ 一.实例分析 1.1 数据 student.txt 1.2 代码 二.代码解析 2.1函数解析 2.1.1 collect() RDD的特性 在 ...
- django2.0+反向查询抛异常处理
一.错误信息 AttributeError: 'RelatedManager' object has no attribute 'lrc' #其中RelatedManager为关键字 二.反向查询的字 ...