在本教程中,我们将向您展示如何使用 Spire.Doc 和 C# 将指定的图像替换为另一个图像,欢迎查阅~
在 Spire.Doc 中可以用 Word 文档中的文本或图像替换图像,在本教程中,我们将向您展示如何使用 Spire.Doc 和 C# 将指定的图像替换为另一个图像。
下面是替换图片之前的原始Word文档:

代码片段:
第 1 步:加载 Word 文档。
Document document = new Document("Input.docx");
第 2 步:用新图像替换标题为“图1”的图像。
//Loop through the paragraphs of the sectionforeach (Paragraph paragraph in document.Sections[0].Paragraphs){//Loop through the child elements of paragraphforeach (DocumentObject docObj in paragraph.ChildObjects){if (docObj.DocumentObjectType == DocumentObjectType.Picture){DocPicture picture = docObj as DocPicture;if (picture.Title == "Figure 1"){//Replace the imagepicture.LoadImage(Image.FromFile("PinkRoses.jpg"));}}}}
第 3 步:保存并关闭文档。
document.SaveToFile("ReplaceImage.docx");document.Close();
生成的文档如下所示:

完整代码:
using System.Drawing;using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace Replace_Image{class Program{static void Main(string[] args){//Load the Word documentDocument document = new Document("Input.docx");//Loop through the paragraphs of the sectionforeach (Paragraph paragraph in document.Sections[0].Paragraphs){//Loop through the child elements of paragraphforeach (DocumentObject docObj in paragraph.ChildObjects){if (docObj.DocumentObjectType == DocumentObjectType.Picture){DocPicture picture = docObj as DocPicture;if (picture.Title == "Figure 1"){//Replace the imagepicture.LoadImage(Image.FromFile("PinkRoses.jpg"));}}}}//Save and close the documentdocument.SaveToFile("ReplaceImage.docx");document.Close();}}}
以上便是如何使用 C# 在 Word 中替换图像,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询在线客服 ;
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!