当前位置: 首页 > news >正文

C# 在Word文档模板中,按照占位符插入文字或图片

1,引入包:DocX

2,代码如下

  public class CC{public static void Ma22in(){// 示例:加载现有模板并替换占位符string templatePath = "报告模板.docx";  // 模板文件路径string outputPath = "Output.docx";      // 输出文件路径// 创建键值对,用于替换占位符var replacements = new Dictionary<string, string>(){["{{name}}"] = "ExamineeModel.Name",["{{hospitalName}}"] = "yyyy",["{{age}}"] = "11",["{{sex}}"] = "sex",["{{date}}"] = $"{DateTime.Now:yyyy-MM-dd}",["{{datetime}}"] = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}",["{{department}}"] = "devartName",["{{checkDescription}}"] = "CheckDescription",["{{conclusionAndAnalysis}}"] = "ConclusionAnalysis",["{{doctorName}}"] = "DocterName",};// 创建图片集合,存储图片路径及对应的占位符var imagePaths = new Dictionary<string, List<string>>{{ "{{image}}", new List<string>(){@"123222.jpg",@"123123.jpg",@"123123.jpg",@"123123.jpg",} } // 填写图片路径};ReplacePlaceholdersInTemplate(templatePath, outputPath, replacements, imagePaths);}public static void ReplacePlaceholdersInTemplate(string templatePath, string outputPath,Dictionary<string, string> replacements, Dictionary<string, List<string>> imagePaths){using (DocX document = DocX.Load(templatePath)){// 替换文本占位符foreach (var replacement in replacements){foreach (Paragraph paragraph in document.Paragraphs){if (paragraph.Text.Contains(replacement.Key)){paragraph.ReplaceText(replacement.Key, replacement.Value);}}}#region 眉头// 替换页眉中的文本占位符if (document.Headers.Even != null){foreach (Paragraph paragraph in document.Headers.Even.Paragraphs){foreach (var replacement in replacements){if (paragraph.Text.Contains(replacement.Key)){paragraph.ReplaceText(replacement.Key, replacement.Value);}}}}if (document.Headers.First != null){foreach (Paragraph paragraph in document.Headers.First.Paragraphs){foreach (var replacement in replacements){if (paragraph.Text.Contains(replacement.Key)){paragraph.ReplaceText(replacement.Key, replacement.Value);}}}}if (document.Headers.Odd != null){foreach (Paragraph paragraph in document.Headers.Odd.Paragraphs){foreach (var replacement in replacements){if (paragraph.Text.Contains(replacement.Key)){paragraph.ReplaceText(replacement.Key, replacement.Value);}}}}#endregion// 替换图片占位符foreach (var imagePath in imagePaths){foreach (Paragraph paragraph in document.Paragraphs){if (paragraph.Text.Contains(imagePath.Key)){InsertImage(paragraph, document, imagePath.Value);paragraph.ReplaceText(imagePath.Key, "");}}}// 保存输出文档document.SaveAs(outputPath);}}public static void InsertImage(Paragraph paragraph, DocX document, List<string> imagePath2, float width = 150, float height = 200f){foreach (var imagePath in imagePath2){if (System.IO.File.Exists(imagePath)){// 添加图片到文档var image = document.AddImage(imagePath);var picture = image.CreatePicture();// 设置图片大小//picture.SetSize(width, height);picture.Width = width;picture.Height = height;// 在对应段落插入图片paragraph.InsertPicture(picture);}else{}}}}

3,模板例如:
在这里插入图片描述

http://www.lryc.cn/news/492319.html

相关文章:

  • 在使用PCA算法进行数据压缩降维时,如何确定最佳维度是一个关键问题?
  • 深度学习3
  • Qt5.14.2的安装与环境变量及一些依赖库的配置
  • PYNQ 框架 - 时钟系统 + pl_clk 时钟输出不准确问题
  • CDAF / PDAF 原理 | PDAF、CDAF 和 LAAF 对比 | 图像清晰度评价指标
  • 类和对象--中--初始化列表(重要)、隐式类型转化(理解)、最后两个默认成员函数
  • uni-app运行 安卓模拟器 MuMu模拟器
  • java 打印对象所有属性的值 循环
  • k8s认证、授权
  • 基于spring boot的纺织品企业财务管理系统论文
  • @RequestBody和前端的关系以及,如何在前后端之间传递数据?
  • 详解登录MySQL时出现SSL connection error: unknown error number错误
  • 【大数据学习 | Spark-Core】Spark的改变分区的算子
  • Spring Boot Web应用开发:测试
  • 服务器数据恢复—光纤存储FC硬盘数据恢复案例
  • Android Binder技术概览
  • 09 —— Webpack搭建开发环境
  • 深度学习模型:卷积神经网络(CNN)
  • Flask 自定义路由转换器
  • 【淘汰9成NLP面试者的高频面题】LSTM中的tanh和sigmoid分别用在什么地方?为什么?
  • gocv调用opencv添加中文乱码的解决方案
  • org.apache.log4j的日志记录级别和基础使用Demo
  • IC数字后端实现之大厂IC笔试真题(经典时序计算和时序分析题)
  • java centos 离线使用sherpa-onnx文字转语音TTS
  • Android 11 三方应用监听关机广播ACTION_SHUTDOWN
  • OpenHarmony-3.驱动HDF
  • 《白帽子讲Web安全》13-14章
  • CSS - CSS One-Line
  • gitlab ssh-key 绑定
  • wordpress使用Markdown语法写的文章图片显示不正常,记录一次折腾之旅