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


    public static async Task LinksReplace()
    {
		const string localPdfDocument = @"C:\Samples\sample.pdf";
		const string storageFileName = "sample.pdf";
		const string localFolder = @"C:\Samples";
		const string resultFileName = "output_add_link.pdf";
		const string storageTempFolder = "YourTempFolder";
		const string linkId = "GE5UYYLVNZRWQQLDORUW63R3HA4CYNRZGQWDCMZQFQ3TAOI";
		const string newLinkAction = "https://reference.aspose.cloud/pdf/";
		
		// Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
		var pdfApi = new PdfApi(AppSecret, AppSid);
		
		using var file = File.OpenRead(localPdfDocument);
		await pdfApi.UploadFileAsync(Path.Combine(storageTempFolder, storageFileName), file);
		
		LinkAnnotation newLink = new LinkAnnotation(
		    Links: new List<Link>() { new Link(newLinkAction) },
		    ActionType: LinkActionType.GoToURIAction,
		    Action: newLinkAction,
		    Highlighting: LinkHighlightingMode.Invert,
		    Color: new Color(A: 0xFF, R: 0xAA, G: 0x00, B: 0x00),
		    Rect: new Rectangle(LLX: 238, LLY: 488.622, URX: 305, URY: 498.588)
		);
		
		LinkAnnotationResponse getResponse = await pdfApi.PutLinkAnnotationAsync(storageFileName, linkId, newLink, folder: storageTempFolder);
		
		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  {
		    using Stream downloadStream = await pdfApi.DownloadFileAsync(Path.Combine(storageTempFolder, storageFileName));
		    using FileStream localStream = File.Create(Path.Combine(localFolder, resultFileName));
		    await downloadStream.CopyToAsync(localStream);
		    Console.WriteLine("LinksReplace(): link '{0}' successfully replaced in the document '{1}.", linkId, resultFileName);
		}
    }