HTML JPG PDF XML DOCX
  Product Family
PDF

使用 .NET SDK 在 PDF 中替换链接

使用服务器端 .NET API 替换 PDF 文档中的链接注释。

Get Started

如何通过 Cloud .NET SDK 替换链接

为了通过 Cloud .NET SDK 替换链接注释,我们将使用 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. 创建一个对象以连接到 Cloud API
  3. 上传您的文档文件
  4. 使用 PutLinkAnnotationAsync 函数设置所需属性
  5. 检查响应并记录结果
  6. 下载更新后的文件以供本地使用
 

此示例代码显示如何在 PDF 文档中替换链接注释


    using Aspose.Pdf.Cloud.Sdk.Model;

    namespace Links
    {
        public class LinksReplace
        {
            public static async Task Modify(string documentName, string outputName, string LinkID, string LinkAction, string remoteFolder)
            {
		// Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
		pdfApi = new PdfApi(AppSecret, AppSid);

                using (var file = File.OpenRead(Path.Combine(localFolder, documentName)))
		{ // Upload the local PDF to cloud storage folder name.
                    FilesUploadResult uploadResponse = await pdfApi.UploadFileAsync(Path.Combine(remoteFolder, documentName), documentName);
                    Console.WriteLine(uploadResponse.Uploaded[0]);
                }

                // Extract Link annotation by Id
                LinkAnnotationResponse getResponse = await pdfApi.GetLinkAnnotationAsync(documentName, LinkID, folder: remoteFolder);

                if (getResponse == null)
                    Console.WriteLine("LinksReplace(): Unexpected error in GetLink!");
                else if (getResponse.Code < 200 || getResponse.Code > 299)
                    Console.WriteLine("LinksReplace(): Failed to receive link from the document.");
                else if (getResponse.Link == null)
                     Console.WriteLine("LinksReplace(): link '{0}' not found in the document '{1]'.", LinkID, documentName);
                else {
                     Console.WriteLine("LinksReplace(): link '{0}' successfully received from the document '{1}.", LinkID, documentName);
                     Console.WriteLine(getResponse.Link.ToString());

                     Link link = new Link(LinkAction);

                     LinkAnnotation newLink = new LinkAnnotation(
                         new List<Link>() { link },
                         ActionType: getResponse.Link.ActionType,
                         Action: LinkAction,
                         Highlighting: getResponse.Link.Highlighting,
                         Color: new Color(A: 0xFF, R: 0xAA, G: 0x00, B: 0x00),
                         Rect: getResponse.Link.Rect
                     );

                     // Replace a link annotation with LinkId in the PDF on cloud storage.
                     AsposeResponse response = await pdfApi.PutLinkAnnotationAsync(documentName, LinkID, newLink, folder: remoteFolder);

                    if (response == null)
                        Console.WriteLine("LinksReplace(): Unexpected error in Modify!");
                    else if (response.Code < 200 || response.Code > 299)
                        Console.WriteLine("LinksReplace(): Failed to replaced link in the document.");
                    else { // Downloads the updated file for local use.
                        Console.WriteLine("LinksReplace(): link '{0}' successfully replaced in the document '{1}.", LinkID, documentName);
                        Stream stream = pdfApi.DownloadFile(Path.Combine(remoteFolder, documentName));
                        using var fileStream = File.Create(Path.Combine(localFolder, "replace_linkk_" + outputName));
                        stream.Position = 0;
                        await stream.CopyToAsync(fileStream);
                        Console.WriteLine("LinksReplace(): File '{0}' successfully downloaded.", "replace_link_" + outputName);
                    }
                }
            }
        }
    }