1. 程式人生 > >C# 替換Word文本—— 用文檔、圖片、表格替換文本

C# 替換Word文本—— 用文檔、圖片、表格替換文本

ins args introduce 圖片 一個 修改 findall child form

編輯文檔時,對一些需要修改的字符或段落可以通過查找替換的方式,快速地更改。在C# 在word中查找及替換文本一文中,主要介紹了在Word中以文本替換文本的方法,在本篇文章中,將介紹如何用一篇Word文檔、圖片或者表格來替換文檔中的指定文本字符串。示例要點如下:

1. 用文檔替換Word中的文本

2. 用圖片替換Word中的文本

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

工具

  • Free Spire.Doc for .NET

下載安裝後,註意在程序中添加引用Spire.Doc.dll(如下圖),dll文件可在安裝路徑下的Bin文件夾中獲取。

技術分享圖片

C#代碼示例

【示例1】用文檔替換Word中的文本

測試文檔:

技術分享圖片

步驟1:加載文檔

//加載源文檔
Document document = new Document("Original.docx");

//加載用於替換的文檔
IDocument replaceDocument = new Document("test.docx");

步驟2:用文檔替換文本

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

步驟3:保存文檔

document.SaveToFile("result.docx", FileFormat.Docx2013);

替換結果:

技術分享圖片

全部代碼:

技術分享圖片
using
Spire.Doc; using Spire.Doc.Interface; namespace ReplaceTextWithDocument_Doc { class Program { static void Main(string[] args) { //加載源文檔 Document document = new Document("Original.docx"); //加載用於替換的文檔 IDocument replaceDocument = new
Document("test.docx"); //用文檔替換源文檔中的指定文本 document.Replace("History", replaceDocument, false, true); //保存文檔 document.SaveToFile("result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result.docx"); } } }
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("Google", true, true);
int index = 0;
TextRange range = null;

步驟3:用圖片替換文本

//遍歷文檔,移除文本內容,插入圖片
foreach (TextSelection selection in selections)
{
    DocPicture pic = new 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("result.docx", FileFormat.Docx);

替換結果:

技術分享圖片

全部代碼:

技術分享圖片
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace ReplaceTextWithImg_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化Document類的對象,並加載測試文檔
            Document doc = new Document();
            doc.LoadFromFile("testfile.docx");
            //加載替換的圖片
            Image image = Image.FromFile("g.png");

            //獲取第一個section
            Section sec= doc.Sections[0];

            //查找文檔中的指定文本內容
            TextSelection[] selections = doc.FindAllString("Google", true, true);
            int index = 0;
            TextRange range = null;

            //遍歷文檔,移除文本內容,插入圖片
            foreach (TextSelection selection in selections)
            {
                DocPicture pic = new 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("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}
View Code

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

測試文檔:

技術分享圖片

步驟1:加載文檔

Document doc = new Document();
doc.LoadFromFile("test.docx");

步驟2:查找關鍵字符串

Section section = doc.Sections[0];
TextSelection selection = doc.FindString("參考附錄", true, 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(true);
table.ResetCells(2, 3);
range = table[0, 0].AddParagraph().AppendText("管號(McFarland)");
range = table[0, 1].AddParagraph().AppendText("0.5");
range = table[0, 2].AddParagraph().AppendText("1");
range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)");
range = table[1, 1].AddParagraph().AppendText("0.2");
range = table[1, 2].AddParagraph().AppendText("0.4");

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

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

步驟6:保存文檔

doc.SaveToFile("result.doc", FileFormat.Doc);

替換結果:

技術分享圖片

全部代碼:

技術分享圖片
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;


namespace ReplaceTextWithTable_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化Document類的對象,並加載測試文檔
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //查找關鍵字符串文本
            Section section = doc.Sections[0];
            TextSelection selection = doc.FindString("參考附錄", true, true);

            //獲取關鍵字符串所在的段落
            TextRange range = selection.GetAsOneRange();
            Paragraph paragraph = range.OwnerParagraph;
            Body body = paragraph.OwnerTextBody;
            int index = body.ChildObjects.IndexOf(paragraph);

            //添加一個兩行三列的表格
            Table table = section.AddTable(true);
            table.ResetCells(2, 3);
            range = table[0, 0].AddParagraph().AppendText("管號(McFarland)");
            range = table[0, 1].AddParagraph().AppendText("0.5");
            range = table[0, 2].AddParagraph().AppendText("1");
            range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)");
            range = table[1, 1].AddParagraph().AppendText("0.2");
            range = table[1, 2].AddParagraph().AppendText("0.4");

            //移除段落,插入表格 
            body.ChildObjects.Remove(paragraph);
            body.ChildObjects.Insert(index, table);

            //保存文檔
            doc.SaveToFile("result.doc", FileFormat.Doc);
            System.Diagnostics.Process.Start("result.doc");
             
        }
    }
}
View Code

以上是本次關於“C# 用文檔、圖片、表格替換Word中的文本字符串的”的全部內容。

(本文完)

C# 替換Word文本—— 用文檔、圖片、表格替換文本