在 Word 文档中,内容控件使得文档的内容可以动态地更新和修改,为用户提供了更灵活的编辑和管理选项。通过内容控件,用户可以轻松地插入、删除或修改特定部分的内容,而无需改变文档的整体结构。本文将介绍如何使用 Spire.Doc for .NET 在 C# 项目中修改 Word 文档中的内容控件。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for .NET 包含的 DLL 文件作为引用添加到您的 .NET项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.DocC# 修改正文内容控件
在 Spire.Doc 中,正文内容控件的对象类型是 StructureDocumentTag。您可以通过遍历 Section.Body 中的子对象集合,查找到类型为 StructureDocumentTag 的对象,然后对其进行修改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Section.Body 获取文档一个节的正文部分。
- 遍历正文部分的子对象集合 Body.ChildObjects,获取到类型为 StructureDocumentTag 的子对象。
- 进入 StructureDocumentTag.ChildObjects 子对象集合,根据子对象的类型执行相应的修改操作。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;
namespace SpireXlsDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的文档对象
            Document doc = new Document();
            // 从文件加载文档内容
            doc.LoadFromFile("示例1.docx");
            // 获取文档正文部分
            Body body = doc.Sections[0].Body;
            // 创建段落列表和表格列表
            List<Paragraph> paragraphs = new List<Paragraph>();
            List<Table> tables = new List<Table>();
            for (int i = 0; i < body.ChildObjects.Count; i++)
            {
                // 获取文档对象
                DocumentObject documentObject = body.ChildObjects[i];
                // 如果是StructureDocumentTag对象
                if (documentObject.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                {
                    StructureDocumentTag structureDocumentTag = (StructureDocumentTag)documentObject;
                    // 如果标签为"c1"或者别名为"c1"
                    if (structureDocumentTag.SDTProperties.Tag == "c1" || structureDocumentTag.SDTProperties.Alias == "c1")
                    {
                        for (int j = 0; j < structureDocumentTag.ChildObjects.Count; j++)
                        {
                            // 如果是段落对象
                            if (structureDocumentTag.ChildObjects[j].DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                Paragraph paragraph = (Paragraph)structureDocumentTag.ChildObjects[j];
                                paragraphs.Add(paragraph);
                            }
                            // 如果是表格对象
                            if (structureDocumentTag.ChildObjects[j].DocumentObjectType == DocumentObjectType.Table)
                            {
                                Table table = (Table)structureDocumentTag.ChildObjects[j];
                                tables.Add(table);
                            }
                        }
                    }
                }
            }
            // 修改第一个段落的文本内容
            paragraphs[0].Text = "成都冰蓝科技有限公司致力于为开发人员提供 .NET 和 JAVA 组件开发产品。";
            // 重置第一个表格的单元格为5行4列
            tables[0].ResetCells(5, 4);
            // 将修改后的文档保存到文件
            doc.SaveToFile("修改Word文档正文内容控件.docx", FileFormat.Docx2016);
            // 释放文档资源
            doc.Dispose();
        }
    }
}
C# 修改段落中的内容控件
在 Spire.Doc 中,段落中的内容控件对象类型是 StructureDocumentTagInline。需要遍历 Paragraph.ChildObjects 的子对象集合,查找到类型为 StructureDocumentTagInline 的对象,然后对其进行修改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Section.Body 获取文档一个节的正文部分。
- 使用 Body.Paragraphs[0] 获取正文部分的第一个段落。
- 遍历段落的子对象集合 Paragraph.ChildObjects,获取到类型为 StructureDocumentTagInline 的子对象。
- 进入 StructureDocumentTagInline.ChildObjects 子对象集合,根据子对象的类型执行相应的修改操作。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;
namespace SpireXlsDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的Document对象
            Document doc = new Document();
            // 从文件加载文档内容
            doc.LoadFromFile("示例2.docx");
            // 获取文档的主体部分
            Body body = doc.Sections[0].Body;
            // 获取主体部分的第一个段落
            Paragraph paragraph = body.Paragraphs[0];
            // 遍历段落中的子对象
            for (int i = 0; i < paragraph.ChildObjects.Count; i++)
            {
                // 检查子对象是否为StructureDocumentTagInline类型
                if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                {
                    // 将子对象转换为StructureDocumentTagInline类型
                    StructureDocumentTagInline structureDocumentTagInline = (StructureDocumentTagInline)paragraph.ChildObjects[i];
                    // 检查文档标记的Tag或Alias属性是否为"text1"
                    if (structureDocumentTagInline.SDTProperties.Tag == "text1" || structureDocumentTagInline.SDTProperties.Alias == "text1")
                    {
                        // 遍历StructureDocumentTagInline对象里的子对象
                        for (int j = 0; j < structureDocumentTagInline.ChildObjects.Count; j++)
                        {
                            // 检查子对象是否为TextRange对象
                            if (structureDocumentTagInline.ChildObjects[j].DocumentObjectType == DocumentObjectType.TextRange)
                            {
                                // 将子对象转换为TextRange类型
                                TextRange range = (TextRange)structureDocumentTagInline.ChildObjects[j];
                                // 设置文本内容为指定内容
                                range.Text = "Word97-2003、Word2007、Word2010、Word2013、Word2016以及Word2019";
                            }
                        }
                    }
                    // 检查文档标记的Tag或Alias属性是否为"logo1"
                    if (structureDocumentTagInline.SDTProperties.Tag == "logo1" || structureDocumentTagInline.SDTProperties.Alias == "logo1")
                    {
                        // 遍历StructureDocumentTagInline对象里的子对象
                        for (int j = 0; j < structureDocumentTagInline.ChildObjects.Count; j++)
                        {
                            // 检查子对象是否为图片
                            if (structureDocumentTagInline.ChildObjects[j].DocumentObjectType == DocumentObjectType.Picture)
                            {
                                // 将子对象转换为DocPicture类型
                                DocPicture docPicture = (DocPicture)structureDocumentTagInline.ChildObjects[j];
                                // 加载指定图片
                                docPicture.LoadImage("Doc-NET.png");
                                // 设置图片宽度和高度
                                docPicture.Width = 100;
                                docPicture.Height = 100;
                            }
                        }
                    }
                }
            }
            // 将修改后的文档保存为新的文件
            doc.SaveToFile("修改Word文档的段落中的内容控件.docx", FileFormat.Docx2016);
            // 释放Document对象的资源
            doc.Dispose();
        }
    }
}
C# 修改表格行内容控件
在 Spire.Doc 中,表格行内容控件对象类型是 StructureDocumentTagRow。需要遍历 Table.ChildObjects 的子对象集合,查找到类型为 StructureDocumentTagRow 的对象,然后对其进行修改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Section.Body 获取文档一个节的正文部分。
- 使用 Body.Tables[0] 获取正文部分的第一个表格。
- 遍历表格的子对象集合 Table.ChildObjects,获取到类型为 StructureDocumentTagRow 的子对象。
- 进入 StructureDocumentTagRow.Cells 表格行内容控件的单元格集合,然后对单元格内容执行相应的修改操作。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的文档对象
            Document doc = new Document();
            // 从文件加载文档
            doc.LoadFromFile("示例3.docx");
            // 获取文档正文部分
            Body body = doc.Sections[0].Body;
            // 获取第一个表格
            Table table = (Table)body.Tables[0];
            // 遍历表格中的子对象
            for (int i = 0; i < table.ChildObjects.Count; i++)
            {
                // 判断子对象是否为StructureDocumentTagRow类型
                if (table.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTagRow)
                {
                    // 将子对象转换为StructureDocumentTagRow对象
                    StructureDocumentTagRow structureDocumentTagRow = (StructureDocumentTagRow)table.ChildObjects[i];
                    // 检查StructureDocumentTagRow的Tag或Alias属性是否为"row1"
                    if (structureDocumentTagRow.SDTProperties.Tag == "row1" || structureDocumentTagRow.SDTProperties.Alias == "row1")
                    {
                        // 清空单元格中的段落
                        structureDocumentTagRow.Cells[0].Paragraphs.Clear();
                        // 在单元格中添加一个段落,并设置文本
                        TextRange textRange = structureDocumentTagRow.Cells[0].AddParagraph().AppendText("艺术");
                        textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
                    }
                }
            }
            // 将修改后的文档保存到文件
            doc.SaveToFile("修改表格行内容控件.docx", FileFormat.Docx2016);
            // 释放文档资源
            doc.Dispose();
        }
    }
}
C# 修改表格单元格内容控件
在 Spire.Doc 中,表格单元格内容控件对象类型是 StructureDocumentTagCell。需要遍历 TableRow.ChildObjects 的子对象集合,查找到类型为 StructureDocumentTagCell 的对象,然后对其进行操作。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Section.Body 获取文档一个节的正文部分。
- 使用 Body.Tables[0] 获取正文部分的第一个表格。
- 遍历表格行集合 Table.Rows,进入每一个 TableRow 对象。
- 遍历表格行的子对象集合 TableRow.ChildObjects,获取到类型为 StructureDocumentTagCell 的子对象。
- 进入 StructureDocumentTagCell.Paragraphs 表格单元格内容控件的段落集合,然后对内容执行相应的修改操作。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的文档对象
            Document doc = new Document();
            // 从文件加载文档
            doc.LoadFromFile("示例4.docx");
            // 获取文档的正文部分
            Body body = doc.Sections[0].Body;
            // 获取文档中的第一个表格
            Table table = (Table)body.Tables[0];
            // 遍历表格的行
            for (int i = 0; i < table.Rows.Count; i++)
            {
                // 遍历每行中的子对象
                for (int j = 0; j < table.Rows[i].ChildObjects.Count; j++)
                {
                    // 检查该子对象是否为StructureDocumentTagCell
                    if (table.Rows[i].ChildObjects[j].DocumentObjectType == DocumentObjectType.StructureDocumentTagCell)
                    {
                        // 将子对象转换为StructureDocumentTagCell类型
                        StructureDocumentTagCell structureDocumentTagCell = (StructureDocumentTagCell)table.Rows[i].ChildObjects[j];
                        // 检查structureDocumentTagCell的Tag或Alias属性是否为"cell1"
                        if (structureDocumentTagCell.SDTProperties.Tag == "cell1" || structureDocumentTagCell.SDTProperties.Alias == "cell1")
                        {
                            // 清空单元格中的段落
                            structureDocumentTagCell.Paragraphs.Clear();
                            // 添加一个新段落,并在其中添加文本
                            TextRange textRange = structureDocumentTagCell.AddParagraph().AppendText("92");
                            textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
                        }
                    }
                }
            }
            // 将修改后的文档保存为新的文件
            doc.SaveToFile("修改表格单元格内容控件.docx", FileFormat.Docx2016);
            // 释放文档对象
            doc.Dispose();
        }
    }
}
C# 修改表格单元格中的内容控件
这个案例展示的是修改在表格单元格的段落中的内容控件。需要先进入单元格中的段落集合 TableCell.Pagragraphs,然后遍历每一个段落对象的子对象集合 Paragraph.ChildObjects,查找到类型为 StructureDocumentTagInline 的对象,然后对其进行修改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Section.Body 获取文档一个节的正文部分。
- 使用 Body.Tables[0] 获取正文部分的第一个表格。
- 遍历表格行集合 Table.Rows,进入每一个 TableRow 对象。
- 遍历单元格集合 TableRow.Cells,进入每一个 TableCell 对象。
- 遍历单元格里的段落集合 TableCell.Paragraphs,进入每一个 Paragraph 对象。
- 遍历段落的子对象集合 Paragraph.ChildObjects,查找到类型为 StructureDocumentTagInline 的对象。
- 进入 StructureDocumentTagInline.ChildObjects 子对象集合,根据子对象的类型执行相应的修改操作。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的Document对象
            Document doc = new Document();
            // 从文件加载文档内容
            doc.LoadFromFile("示例5.docx");
            // 获取文档正文部分
            Body body = doc.Sections[0].Body;
            // 获取第一个表格
            Table table = (Table)body.Tables[0];
            // 遍历表格的行
            for (int r = 0; r < table.Rows.Count; r++)
            {
                // 遍历表格行中的单元格
                for (int c = 0; c < table.Rows[r].Cells.Count; c++)
                {
                    // 遍历单元格中的段落
                    for (int p = 0; p < table.Rows[r].Cells[c].Paragraphs.Count; p++)
                    {
                        // 获取段落对象
                        Paragraph paragraph = table.Rows[r].Cells[c].Paragraphs[p];
                        // 遍历段落中的子对象
                        for (int i = 0; i < paragraph.ChildObjects.Count; i++)
                        {
                            // 判断子对象是否为StructureDocumentTagInline类型
                            if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                            {
                                // 转换为StructureDocumentTagInline对象
                                StructureDocumentTagInline structureDocumentTagInline = (StructureDocumentTagInline)paragraph.ChildObjects[i];
                                // 检查StructureDocumentTagInline的Tag或Alias属性是否为"test1"
                                if (structureDocumentTagInline.SDTProperties.Tag == "test1" || structureDocumentTagInline.SDTProperties.Alias == "test1")
                                {
                                    // 遍历StructureDocumentTagInline的子对象
                                    for (int j = 0; j < structureDocumentTagInline.ChildObjects.Count; j++)
                                    {
                                        // 判断子对象是否为TextRange类型
                                        if (structureDocumentTagInline.ChildObjects[j].DocumentObjectType == DocumentObjectType.TextRange)
                                        {
                                            // 转换为TextRange对象
                                            TextRange textRange = (TextRange)structureDocumentTagInline.ChildObjects[j];
                                            // 设置文本内容
                                            textRange.Text = "89";
                                            //设置文本颜色
                                            textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // 将修改后的文档保存为新文件
            doc.SaveToFile("修改表格单元格中的内容控件.docx", FileFormat.Docx2016);
            // 释放Document对象资源
            doc.Dispose();
        }
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



