• Word控件Spire.Doc 【图像形状】教程(4) 用 C# 中的文本替换 Word 中的图像


    Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。在 C#、VB.NET 中从 Word 中提取图像。

    Spire.Doc for.NET 最新下载(767755948)icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

    让我们看看 Spire.Doc 是如何帮助开发人员解决与 Office 技术编程相关的问题。根据描述,发帖人希望将Word文件中的每张图片对应替换为“Here was image {image index}”。但是,我们想提供一个带有以下示例代码的解决方案。

    测试文件

    用文本替换图像的代码片段;

    第 1 步:创建一个新的 Word 文档并加载测试文件。

    Document document = new Document(@"..\..\test.docx");

    第 2 步:获取Word文档中的所有图片并将它们替换为文本“Here was image {image index}”。

    int j = 1;
    foreach (Section sec in document.Sections)
    {
    foreach (Paragraph para in sec.Paragraphs)
    {
    List pictures = new List();
    foreach (DocumentObject docObj in para.ChildObjects)
    {
    if (docObj.DocumentObjectType == DocumentObjectType.Picture)
    {
    pictures.Add(docObj);
    }
    }
    foreach (DocumentObject pic in pictures)
    {
    int index = para.ChildObjects.IndexOf(pic);
    TextRange range = new TextRange(document);
    range.Text = string.Format("Here was image {0}", j);
    para.ChildObjects.Insert(index, range);
    para.ChildObjects.Remove(pic);
    j++;
    }
    }
    }

    第 3 步:保存并启动文件。

    document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start(@"..\..\result.docx");

    结果:

    完整的 C# 代码:

    [C#]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using Spire.Doc.Interface;
    using System.Drawing;
    
    namespace ReplaceImageWithText
    {
    class Program
    {
    static void Main(string[] args)
    {
    Document document = new Document(@"..\..\test.docx");
    int j = 1;
    foreach (Section sec in document.Sections)
    {
    foreach (Paragraph para in sec.Paragraphs)
    {
    List pictures = new List();
    foreach (DocumentObject docObj in para.ChildObjects)
    {
    if (docObj.DocumentObjectType == DocumentObjectType.Picture)
    {
    pictures.Add(docObj);
    }
    }
    foreach (DocumentObject pic in pictures)
    {
    int index = para.ChildObjects.IndexOf(pic);
    TextRange range = new TextRange(document);
    range.Text = string.Format("Here was image {0}", j);
    para.ChildObjects.Insert(index, range);
    para.ChildObjects.Remove(pic);
    j++;
    }
    }
    }
    document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start(@"..\..\result.docx");
    }
    }
    }

    以上便是如何在 C#/VB.NET 中的指定位置插入图像,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

  • 相关阅读:
    汽车螺丝扭力标准/汽车常见螺栓扭矩参照
    TiDB 环境与系统配置检查
    你不知道的 HTML 属性
    [CF643F]Bears and Juice
    V2X应用 汇总
    PostgreSQL基本操作
    面向对象oop的理解
    解决Mysql8.0不存在mysql.proc表
    helm使用
    从0到1的解释器
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/128013764