.NET 8 使用官方OpenXml SDK,替换Word中的文字和图片

安装好DocumentFormat.OpenXml后,准备好一个docx文件

using DocumentFormat.OpenXml.Drawing.Wordprocessing; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using System.Text.RegularExpressions; using A = DocumentFormat.OpenXml.Drawing; namespace Test02 { internal class Program { static void Main(string[] args) { ReplaceTextRequest textRequest = new ReplaceTextRequest { FileFullPath = "D:\\test-app\\test.docx", TextPlaceholderName = "占位符", Text = "恐怖如斯丶吴彦祖" }; ReplaceText(textRequest); var imageRequest = new ReplaceImageRequest { FileFullPath = "D:\\test-app\\test.docx", ImagePlaceholderName = "Picture 1", ImageFullPath = "C:\\Users\\xudashan\\Pictures\\高达\\1.png" }; ReplaceImage(imageRequest); Console.WriteLine("OK"); } static void ReplaceText(ReplaceTextRequest request) { using WordprocessingDocument wordDoc = WordprocessingDocument.Open(request.FileFullPath, true); if (wordDoc.MainDocumentPart == null) { throw new Exception("文档为空"); } string docText = string.Empty; using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { docText = sr.ReadToEnd(); } Regex regexText = new Regex(request.TextPlaceholderName); docText = regexText.Replace(docText, request.Text); using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.WriteLine(docText); } } static void ReplaceImage(ReplaceImageRequest request) { using WordprocessingDocument document = WordprocessingDocument.Open(request.FileFullPath, true); if (document.MainDocumentPart == null) { throw new Exception("文档为空"); } IEnumerable<Inline> imageElements = from run in document.MainDocumentPart.Document.Descendants<Run>() where run.Descendants<Inline>().First() != null select run.Descendants<Inline>().First(); Inline selectedImage = (from image in imageElements where (image.DocProperties != null && image.DocProperties.Name == request.ImagePlaceholderName) select image).First(); A.Blip blipElement = selectedImage.Descendants<A.Blip>().First(); if (blipElement.Embed == null || blipElement.Embed.Value == null) { throw new Exception("错误的模板标签"); } var imageId = blipElement.Embed.Value; ImagePart imagePart = (ImagePart)document.MainDocumentPart.GetPartById(imageId); byte[] imageBytes = File.ReadAllBytes(request.ImageFullPath); using BinaryWriter writer = new BinaryWriter(imagePart.GetStream()); writer.Write(imageBytes); } } class ReplaceImageRequest { /// <summary> /// docx 文件地址 /// </summary> public string FileFullPath { get; set; } /// <summary> /// 图片占位的Name /// </summary> public string ImagePlaceholderName { get; set; } /// <summary> /// 待替换的图片文件 /// </summary> public string ImageFullPath { get; set; } } class ReplaceTextRequest { /// <summary> /// docx 文件地址 /// </summary> public string FileFullPath { get; set; } /// <summary> /// 文字占位的Name /// </summary> public string TextPlaceholderName { get; set; } /// <summary> /// 待替换的文字 /// </summary> public string Text { get; set; } } }

替换后

ps: 高达原图


__EOF__

本文作者徐徐赵赵
本文链接https://www.cnblogs.com/xuxuzhaozhao/p/18206436.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   xuxuzhaozhao  阅读(240)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示