HTML JPG PDF XML DOCX
  Product Family
PDF

Replace Link in PDF using .NET SDK

Replace a Link annotation in a PDF Document using server-side .NET API.

Get Started

How to replace Link via Cloud .NET SDK

In order to replace a Link annotation via Cloud .NET SDK , we’ll use Aspose.PDF Cloud .NET SDK This Cloud SDK allows you to easily build cloud-based PDF creator, editor & converter apps in C#, ASP.NET, or other .NET languages for various cloud platforms. Open NuGet package manager, search for Aspose.PDF Cloud and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


     
    PM> Install-Package Aspose.Pdf-Cloud
     
     

Steps to replace a Link via .NET SDK

Aspose.PDF Cloud developers can easily load & change link annotation in PDF in just a few lines of code.

  1. Create a new Configuration object with your Application Secret and Key
  2. Create an object to connect to the Cloud API
  3. Upload your document file
  4. Set desired property using PutLinkAnnotationAsync function
  5. Checks the response and logs the result
  6. Downloads the updated file for local use
 

This sample code shows replacing a Link annotation in PDF documents


    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);
                    }
                }
            }
        }
    }