题注在 Word 文档中是提高可理解性和组织结构的重要元素。它提供对图片、表格等内容的解释和补充信息,增强文档的可读性和清晰度。题注还用于强调重点和关键信息,方便引用和索引特定内容。通过合理使用题注,读者能更好地理解和解读文档中的数据和图像,并能快速定位所需信息。本文将介绍如何使 Spire.Doc for .NET 在 C# 项目中添加和删除 Word 文档中的题注。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc添加图片题注到 Word 文档
实现向 Word 文档中的图片添加题注,即创建段落、添加图片内容,以及调用 DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) 方法来生成题注的编号。详细步骤如下:
- 创建一个 Document 类的对象。
- 使用 Document.AddSection() 方法添加一个章节。
- 创建一个 Paragraph 对象 pictureParagraphCaption 并将其添加到文档中的指定章节(section)。
- 使用 Paragraph.AppendPicture(Image image) 方法向段落中添加 DocPicture 图片对象 pic1。
- 通过 DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) 方法来添加题注以 CaptionNumberingFormat.Number 数字方式进行编号。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace AddPictureCaption
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建Word文档对象
            Document document = new Document();
            // 添加一个章节
            Section section = document.AddSection();
            // 添加一个新段落并给它添加一个图片
            Paragraph pictureParagraphCaption = section.AddParagraph();
            pictureParagraphCaption.Format.AfterSpacing = 10;
            DocPicture pic1 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\1.png"));
            pic1.Height = 100;
            pic1.Width = 100;
            // 给图片添加题注
            CaptionNumberingFormat format = CaptionNumberingFormat.Number;
            pic1.AddCaption("图片", format, CaptionPosition.BelowItem);
            // 再新添加一个新段落并给它添加一个图片
            pictureParagraphCaption = section.AddParagraph();
            DocPicture pic2 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\2.png"));
            pic2.Height = 100;
            pic2.Width = 100;
            // 给图片添加题注
            pic2.AddCaption("图片", format, CaptionPosition.BelowItem);
            // 更新文档中的所有的域
            document.IsUpdateFields = true;
            // 保存到一个docx文档
            string result = "添加图片题注.docx";
            document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
            // 关闭document对象释放资源
            document.Close();
            document.Dispose();
        }
     }
}
添加表格题注到 Word 文档
实现向 Word 文档中的表格添加题注,即创建表格,调用 Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) 方法来生成题注的编号。详细步骤如下:
- 创建一个 Document 类的对象。
- 使用 Document.AddSection() 方法添加一个章节。
- 创建一个 Table 对象 tableCaption 并将其添加到文档中的指定章节(section)。
- 使用 Table.ResetCells(int rowsNum, int columnsNum) 方法来设置表格的行数和列数。
- 通过 Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) 方法来添加题注以 CaptionNumberingFormat.Number 数字方式进行编号。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
using Spire.Doc;
namespace AddTableCation
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建Word文档对象
            Document document = new Document();
            // 添加一个章节
            Section section = document.AddSection();
            // 添加一个表格
            Table tableCaption = section.AddTable(true);
            tableCaption.ResetCells(3, 2);
            // 给表格添加题注
            tableCaption.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem);
            // 再新添加一个表格并给表格添加题注
            tableCaption = section.AddTable(true);
            tableCaption.ResetCells(2, 3);
            tableCaption.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem);
            // 更新文档中的所有的域
            document.IsUpdateFields = true;
            // 保存到一个docx文档
            string result = "添加表格题注.docx";
            document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
            // 关闭document对象释放资源
            document.Close();
            document.Dispose();
        }
    }
}
从 Word 文档中删除题注
Spire.Doc for .NET 还可以实现从存在题注的 Word 文档中将题注进行删除。详细步骤如下:
- 创建 Document 类的对象。
- 使用 Document.LoadFromFile() 方法加载一个 Word 文档。
- 创建一个自定义方法 DetectCaptionParagraph(Paragraph paragraph) 来判断此段落是否包含题注。
- 循环遍历文档中所有的段落 Paragraph 对象,并使用自定义方法 DetectCaptionParagraph(Paragraph paragraph) 找出包含题注的段落,将它们全部删除掉。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace DeleteCaptions
{
    internal class Program1
    {
        static void Main(string[] args)
        {
            // 创建Word文档对象
            Document document = new Document();
            // 加载示例.docx文件
            document.LoadFromFile("Data/示例.docx");
            Section section;
            // 遍历所有节
            for (int i = 0; i < document.Sections.Count; i++)
            {
                section = document.Sections[i];
                // 倒序遍历节中的段落
                for (int j = section.Body.Paragraphs.Count - 1; j >= 0; j--)
                {
                    // 检测段落是否为题注段落
                    if (DetectCaptionParagraph(section.Body.Paragraphs[j]))
                    {
                        // 如果是题注段落,则移除该段落
                        section.Body.Paragraphs.RemoveAt(j);
                    }
                }
            }
            // 保存删除题注后的文档
            string result = "删除题注.docx";
            document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
            // 关闭document对象释放资源
            document.Close();
            document.Dispose();
        }
        // 判断段落是否为题注段落的方法
        static bool DetectCaptionParagraph(Paragraph paragraph)
        {
            bool tag = false;
            Field field;
            // 遍历段落中的子对象
            for (int i = 0; i < paragraph.ChildObjects.Count; i++)
            {
                if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.Field)
                {
                    // 判断子对象是否为Field类型
                    field = (Field)paragraph.ChildObjects[i];
                    if (field.Type == FieldType.FieldSequence)
                    {
                        // 判断Field类型是否为FieldSequence,即题注域类型
                        return true;
                    }
                }
            }
            return tag;
        }
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



