忘記在長電子郵件線程中討論文檔的內容和反饋。只需使用註釋來標記帶有消息和回复的文檔。在本文中,您將了解如何使用 .NET 應用程序以編程方式在 C# 中添加和刪除註釋以標記 Word 文檔。

以下是下面簡要討論的主題:

用於註釋和標記 Word 文件的 .NET API

GroupDocs.Annotation 提供 .NET API 來處理 .NET 應用程序中文檔和圖像的註釋。該 API 允許您從 Word 文檔中添加、刪除和提取註釋。此外,它還支持電子表格、演示文稿、圖像、PDF 文件、網頁、電子郵件、Visio 繪圖。一些 AutoCAD 繪圖和數字成像格式(如 DICOM)也在列表中。有關註釋支持的文檔格式 的完整列表,您可以訪問文檔。

下載部分 下載 DLL 或 MSI 安裝程序,或通過 NuGet 在您的 .NET 應用程序中安裝 API。您也可以使用包管理器中的以下命令。

PM> Install-Package GroupDocs.Annotation

在 C# 中為 Word 添加批註

讓我們向 Word 文檔添加一些不同類型的註釋。有許多不同類型的註釋,因此我們將在本文中僅介紹其中的幾種。

使用 GroupDocs API 向 DOC DOCX 添加註釋

有一些支持的註解類型,您可以單獨了解每個註解

  • 面積/矩形標註
  • 強調
  • 水印
  • 距離
  • 三振
  • 文本域
  • 橢圓
  • 強調
  • 關聯
  • 觀點
  • 折線
  • 替代品
  • 資源編輯
  • 文本編輯

使用 C# 為 Word 添加箭頭註釋

以下是在 C# 中為 Word 文檔添加箭頭註釋的步驟。

在 Java 和 .NET 中以編程方式添加箭頭註釋
  • 使用 Annotator 類加載文檔。
  • 使用 ArrowAnnotation 類初始化箭頭註釋。
  • 調整箭頭註釋的位置、大小、頁碼。
  • 使用 Add 方法添加創建的箭頭註釋。
  • 使用Save方法將帶註釋的Word文檔保存到路徑中。

以下代碼示例演示如何使用 C# 向 Word 文檔添加箭頭註釋。

// 使用C#為Word文檔添加箭頭註釋
using (Annotator annotator = new Annotator("path/document.docx"))
{
    ArrowAnnotation arrow = new ArrowAnnotation
    {
        Box = new Rectangle(100, 100, 50, 50),
        CreatedOn = DateTime.Now,
        Message = "Your Message",
        Opacity = 0.7,
        PageNumber = 0,
        PenColor = -3407872,
        PenStyle = PenStyle.Solid,
        PenWidth = 2
    };
    annotator.Add(arrow);
    annotator.Save("path/annotation.docx");
}

使用 C# 向 Word 插入矩形或區域註釋

可以在將任何註釋添加到文檔時對其進行自定義。以下是通過一些自定義向 DOC/DOCX 文檔添加矩形或區域註釋的步驟。它與添加箭頭註釋非常相似,但這次使用 AreaAnnotation 類。

  • 使用 Annotator 類加載 DOC/DOCX 文檔。
  • 使用 AreaAnnotation 類初始化矩形註釋。
  • 調整矩形的位置、大小和顏色。
  • 設置其他屬性,如頁碼、背景、不透明度、樣式、筆寬、消息和時間。
  • 將創建的矩形註釋添加到註釋器中。
  • 使用Save方法將註釋文件保存到路徑。
在 .NET 和 Java 中以編程方式添加矩形或區域註釋

以下代碼示例顯示如何使用 C# 向 Word 文檔添加矩形/區域註釋。

// 使用 C# 在 Word 文檔中添加區域或矩形註釋
using (Annotator annotator = new Annotator("path/document.docx"))
{
    AreaAnnotation area = new AreaAnnotation
    {
        BackgroundColor = 65535,
        Box = new Rectangle(80, 75, 450, 135),
        Message = "This is area annotation",
        Opacity = 0.2,
        PageNumber = 0,
        PenColor = -131,
        PenStyle = PenStyle.Dash,
        PenWidth = 3
    };
    annotator.Add(area);
    annotator.Save("path/annotation.docx");
}

使用 C# 向 Word 添加 Oval 或 Ellipse 註解

下面是在C#中給文檔添加橢圓註解或橢圓註解的步驟。

在 C# .NET 和 Java 中以編程方式添加橢圓或橢圓註釋
  • 使用 Annotator 類加載 DOC/DOCX 文檔。
  • 使用 EllipseAnnotation 類初始化橢圓註釋。
  • 設置初始化橢圓標註的位置和大小。
  • 將創建的橢圓註釋添加到 Annotator 對像中。
  • 提供路徑並使用 Save 方法保存帶註釋的 Word 文件。

以下代碼示例演示如何使用 C# 向 Word 文檔添加橢圓形或橢圓形批註。

// 使用 C# 在 Word 文檔中添加 Oval 或 Ellipse Annotation
using (Annotator annotator = new Annotator("path/document.docx"))
{
    EllipseAnnotation ellipse = new EllipseAnnotation
    {
        BackgroundColor = -16034924,
        Box = new Rectangle(275, 475, 300, 80),
        Message = "This is ellipse annotation",
        Opacity = 0.2,
        PageNumber = 0,
        PenColor = -16034924,
        PenStyle = PenStyle.Dot,
        PenWidth = 3
    };
    annotator.Add(ellipse);
    annotator.Save("path/annotation.docx");
}

使用 C# 向 Word 插入距離註釋

同樣,您可以添加距離註釋來標記兩點之間的距離。以下是向文檔添加距離註釋的步驟。

在 C# .NET 和 Java 中以編程方式添加距離註釋
  • 加載 Word 文檔後,使用 DistanceAnnotation 類初始化距離註釋。
  • 設置註釋的外觀。
  • 將距離註釋添加到 Annotator 對象。
  • 通過指定路徑將帶註釋的 Word 文件保存在給定位置。

以下代碼示例展示瞭如何使用 C# 向 DOC/DOCX 文檔添加距離註釋。

// 使用 C# 為 Word 文檔添加距離註釋
using (Annotator annotator = new Annotator("path/document.docx"))
{
    DistanceAnnotation distance = new DistanceAnnotation
    {
        Box = new Rectangle(750, 235, 0, 150),
        Message = "This is the heading area",
        Opacity = 0.7,
        PageNumber = 0,
        PenColor = -21197,
        PenStyle = PenStyle.Solid,
        PenWidth = 3
    };
    annotator.Add(distance);
    annotator.Save("path/annotation.docx");
}

完整代碼

總而言之,這是完整的代碼,輸出顯示了所有添加的註釋和帶有回复的消息。以下 C# 代碼向 Word 文件添加箭頭、矩形、橢圓、距離註釋、消息和回复。

// 使用 C# 向 Word 添加多個批註
// 使用 C# 向帶有消息和回复的 DOC/DOCX 添加箭頭、區域、橢圓 (Ellipse)、距離註釋
string outputPath = @"outputPath/annotatedDoc.docx";
string inputFile = @"inputPath/document.docx";

using (Annotator annotator = new Annotator(inputFile))
{
    ArrowAnnotation arrow = new ArrowAnnotation
    {
        Box = new Rectangle(550, 250, 60, -60),
        CreatedOn = DateTime.Now,
        Message = "This image is little upwards.",
        Opacity = 0.7,
        PageNumber = 0,
        PenColor = -3407872,
        PenStyle = PenStyle.Solid,
        PenWidth = 2,
        Replies = new List<Reply>
        {
            new Reply
            {
                Comment = "Please look in to these issues.",
                RepliedOn = DateTime.Now
            },
            new Reply
            {
                    Comment = "Change Description",
                RepliedOn = DateTime.Now
            },
            new Reply
            {
                Comment = "On-Premises APIs",
                RepliedOn = DateTime.Now
            },
            new Reply
            {
                Comment = "Add images as well.",
                RepliedOn = DateTime.Now
            }
        }
    };
    AreaAnnotation area = new AreaAnnotation
    {
        BackgroundColor = 65535,
        Box = new Rectangle(80, 75, 450, 135),
        Message = "This is area annotation",
        Opacity = 0.2,
        PageNumber = 0,
        PenColor = -131,
        PenStyle = PenStyle.Dash,
        PenWidth = 3
    };
    EllipseAnnotation ellipse = new EllipseAnnotation
    {
        BackgroundColor = -16034924,
        Box = new Rectangle(275, 475, 300, 80),
        Message = "This is ellipse annotation",
        Opacity = 0.2,
        PageNumber = 0,
        PenColor = -16034924,
        PenStyle = PenStyle.Dot,
        PenWidth = 3
    };
    DistanceAnnotation distance = new DistanceAnnotation
    {
        Box = new Rectangle(750, 235, 0, 150),
        Message = "This is the heading area",
        Opacity = 0.7,
        PageNumber = 0,
        PenColor = -21197,
        PenStyle = PenStyle.Solid,
        PenWidth = 3
    };
    annotator.Add(arrow);
    annotator.Add(area);
    annotator.Add(ellipse);
    annotator.Add(distance);

    annotator.Save(outputPath);
}

使用 C# 從 Word DOC/DOCX 文件中刪除註釋

可以輕鬆刪除文檔中的註釋。有許多選項可用於從 Word 文檔中刪除註釋。您可以一次刪除所有註釋。此外,您可以提供索引以刪除特定註釋。有關更多選項,請訪問 文檔 文章。

以下是從 Word 文件中刪除所有註釋的步驟。

  • 裝入文檔。
  • 使用 SaveOptions 類初始化保存選項。
  • 將註釋類型設置為無。
  • 保存 Word 文件。它將沒有註釋。

以下代碼顯示如何使用 C# 從 Word 文件中刪除批註。

// 使用 C# 從 Word 文檔中刪除所有註釋
using (Annotator annotator = new Annotator(outputPath))
{
    annotator.Save(remOutputPath, new SaveOptions {AnnotationTypes = AnnotationType.None});
}

結論

簡而言之,您已經了解瞭如何使用 C# 在 .NET 應用程序中向 Word 文檔添加批註。具體來說,我們向 Word DOC/DOCX 文件添加了箭頭、橢圓、面積和距離註釋。此外,您還看到瞭如何從任何 Word 文件中刪除所有註釋。現在,您可以考慮構建自己的文檔註釋器 .NET 應用程序。

文檔GitHub 存儲庫中了解有關 GroupDocs.Annotation for .NET 的更多信息。如需進一步查詢,請聯繫 論壇 上的支持人員。

也可以看看