天天看點

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

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

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

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

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

工具

  •   Free Spire.Doc for .NET

下載下傳安裝後,注意在程式中添加引用Spire.Doc.dll(如下圖),dll檔案可在安裝路徑下的Bin檔案夾中擷取。

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

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);      

替換結果:

全部代碼:

C# 替換Word文本—— 用文檔、圖檔、表格替換文本
C# 替換Word文本—— 用文檔、圖檔、表格替換文本
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);      
C# 替換Word文本—— 用文檔、圖檔、表格替換文本
C# 替換Word文本—— 用文檔、圖檔、表格替換文本
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");
        }
    }
}      

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

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);      
C# 替換Word文本—— 用文檔、圖檔、表格替換文本
C# 替換Word文本—— 用文檔、圖檔、表格替換文本
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");
             
        }
    }
}      

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

(本文完)

繼續閱讀