微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 替换Word文本—— 用文档、图片、表格替换文本

编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文档、图片或者表格来替换文档中的指定文本字符串。示例要点如下:

1. 用文档替换Word中的文本

2. 用图片替换Word中的文本

3. 用表格替换Word中的文本

 

工具

下载安装后,注意在程序中添加引用Spire.Doc.dll(如下图),dll文件可在安装路径下的Bin文件夹中获取

C#代码示例

【示例1】用文档替换Word中的文本

测试文档:

步骤1:加载文档

//加载源文档
Document document = new Document("Original.docx");

加载用于替换的文档
IDocument replaceDocument = test.docx");

步骤2:用文档替换文本

document.Replace(History",replaceDocument,false,1)">true);

步骤3:保存文档

document.SavetoFile(result.docx

 

全部代码

using Spire.Doc;
 Spire.Doc.Interface;

namespace ReplaceTextWithDocument_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            加载源文档
            Document document = );

            加载用于替换的文档
            IDocument replaceDocument = 用文档替换源文档中的指定文本
            document.Replace(true保存文档
            document.SavetoFile(,FileFormat.Docx2013);
            System.Diagnostics.Process.Start();
        }
    }
}
View Code

 

 

【示例2】用图片替换Word中的文本

测试文档:

步骤1:加载文件

实例化Document类的对象,并加载测试文档
Document doc = new Document();
doc.LoadFromFile(testfile.docx);
加载替换的图片
Image image = Image.FromFile(g.png");

步骤2:查找需要替换掉的文本字符串

获取一个section
Section sec= doc.Sections[0];

查找文档中的指定文本内容
TextSelection[] selections = doc.FindAllString(Googletrue,1)">int index = ;
TextRange range = null;

步骤3:用图片替换文本

遍历文档,移除文本内容,插入图片
foreach (TextSelection selection in selections)
{
    DocPicture pic =  DocPicture(doc);
    pic.LoadImage(image);
    range = selection.GetAsOneRange();
    index = range.OwnerParagraph.Childobjects.IndexOf(range);
    range.OwnerParagraph.Childobjects.Insert(index,pic);
    range.OwnerParagraph.Childobjects.Remove(range);
}

步骤4:保存文档

doc.SavetoFile(

全部代码

 Spire.Doc.Documents;
 Spire.Doc.Fields;
 System.Drawing;

 ReplaceTextWithImg_Doc
{
    实例化Document类的对象,并加载测试文档
            Document doc =  Document();
            doc.LoadFromFile();
            加载替换的图片
            Image image = Image.FromFile(获取一个section
            Section sec= doc.Sections[];

            查找文档中的指定文本内容
            TextSelection[] selections = doc.FindAllString(;
            TextRange range = null;

            遍历文档,移除文本内容,插入图片
             selections)
            {
                DocPicture pic =  DocPicture(doc);
                pic.LoadImage(image);
                range = selection.GetAsOneRange();
                index = range.OwnerParagraph.Childobjects.IndexOf(range);
                range.OwnerParagraph.Childobjects.Insert(index,pic);
                range.OwnerParagraph.Childobjects.Remove(range);
            }

            保存文档
            doc.SavetoFile(stem.Diagnostics.Process.Start();
        }
    }
}
View Code

 

【示例3】用表格替换Word中的文本

测试文档:

 

步骤1:加载文档

Document doc = ");

步骤2:查找关键字符串

Section section = doc.Sections[];
TextSelection selection = doc.FindString(参考附录true);

步骤3:获取关键字符串所在段落的索引

TextRange range = selection.GetAsOneRange();
Paragraph paragraph = range.OwnerParagraph;
Body body = paragraph.OwnerTextBody;
int index = body.Childobjects.IndexOf(paragraph);

步骤4:添加表格

Table table = section.AddTable();
table.ResetCells(2,3);
range = table[0,1)">0].AddParagraph().AppendText(管号(McFarland)1].AddParagraph().AppendText(0.52].AddParagraph().AppendText(11,1)">0.25%BaCl2(ml)0.20.4");

步骤5:移除段落,插入表格

body.Childobjects.Remove(paragraph);
body.Childobjects.Insert(index,table);

步骤6:保存文档

doc.SavetoFile(result.doc

全部代码

 Spire.Doc.Fields;


 ReplaceTextWithTable_Doc
{
    查找关键字符串文本
            Section section = doc.Sections[];
            TextSelection selection = doc.FindString(获取关键字符串所在的段落
            TextRange range = selection.GetAsOneRange();
            Paragraph paragraph = range.OwnerParagraph;
            Body body = paragraph.OwnerTextBody;
            int index = body.Childobjects.IndexOf(paragraph);

            添加一个两行三列的表格
            Table table = section.AddTable();
            table.ResetCells();
            range = table[移除段落,插入表格 
            body.Childobjects.Remove(paragraph);
            body.Childobjects.Insert(index,table);

            stem.Diagnostics.Process.Start();
             
        }
    }
}
View Code

 

以上是本次关于“C# 用文档、图片、表格替换Word中的文本字符串的”的全部内容

(本文完)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐