HTML JPG PDF XML DOCX
  Product Family
PDF

通过 .NET SDK 在 PDF 中替换文本注释

使用 Aspose.PDF Cloud .NET SDK 替换 PDF 文档中的注释的 API。

Get Started

如何使用 Cloud .NET SDK 替换 PDF 文档中的文本注释

为了替换 PDF 文档中的文本注释,我们将使用 Aspose.PDF Cloud .NET SDK 此 Cloud SDK 使您可以轻松在 C#、ASP.NET 或其他 .NET 语言中构建基于云的 PDF 创建、编辑和转换应用程序,适用于各种云平台。打开 NuGet 包管理器,搜索 Aspose.PDF Cloud 并安装。您也可以在包管理器控制台中使用以下命令。

包管理器控制台命令


    PM> Install-Package Aspose.Pdf-Cloud
     

使用 .NET SDK 替换注释的步骤

Aspose.PDF Cloud 开发人员可以轻松地在 PDF 文档中加载和替换注释,仅需几行代码。

  1. 将文档上传到云。
  2. 使用 GetTextAnnotationAsync() 获取注释。
  3. 修改注释内容和图标。
  4. 将更改保存回云端
  5. 下载更新的文档。
 

此示例代码演示如何通过 C# 替换 PDF 文档中的文本注释


    using Aspose.Pdf.Cloud.Sdk.Model;
    using System.Runtime.Intrinsics.X86;

    namespace Annotations
    {
        public class ReplaceAnnotation
        {
            public static async Task<TextAnnotation> GetAnnotationAsync(AnnotationsHelper helper, string documentName, string annotationId, string remoteFolder)
            {
                // Get annotation by Id in the PDF document.
                TextAnnotation annotationResult = null;
                TextAnnotationResponse response = await helper.pdfApi.GetTextAnnotationAsync(documentName, annotationId, folder: remoteFolder);

                if (response == null)
                    Console.WriteLine("GetAnnotationAsync(): Unexpected error!");
                else if (response.Code < 200 || response.Code > 299)
                    Console.WriteLine("GetAnnotationAsync(): Failed to request text annotation from the document.");
                else
                {
                    Console.WriteLine("GetAnnotationAsync(): annotation '{0}' with '{1}' contents successfully found in the document '{2}.", response.Annotation.Id, response.Annotation.Contents, documentName);
                    annotationResult = response.Annotation;
                }
                return annotationResult;
            }

            public static async Task ModifyAsync(AnnotationsHelper helper, string documentName, string outputName, string annotationId, string remoteFolder)
            {
                // Change annotation on the page in the PDF document.
                await helper.UploadFile(documentName);
                TextAnnotation annotation = await ReplaceAnnotation.GetAnnotationAsync(helper, documentName, annotationId, remoteFolder);

                annotation.Contents = helper.config.REPLACED_CONTENT;
                annotation.Icon = TextIcon.Star;

                TextAnnotationResponse response = await helper.pdfApi.PutTextAnnotationAsync(documentName, annotationId, annotation, folder: remoteFolder);
                if (response == null)
                    Console.WriteLine("ModifyAnnotation(): Unexpected error!");
                else if (response.Code < 200 || response.Code > 299)
                    Console.WriteLine("ModifyAnnotation(): Failed to request text annotation from the document.");
                else
                {
                    Console.WriteLine("ModifyAnnotation(): annotation '{0}' successfully modified in the document '{1}.", annotationId, documentName);
                    await helper.DownloadFile(documentName, outputName, "replaced_annotatiom_");
                }
            }
        }
    }