Foxit Quick PDF Library

Programmatically find and replace URL links in PDF files

Foxit Quick PDF Library has an extensive API for programmatically working with links in PDF files. In the sample code below we demonstrate how to find all URLs in a PDF and then check to see if it matches our predefined URL and if it does then we replace it.

This sample just works with URLs but it would be easy to modify the code to work with other link types as well. You could also modify the code so that only a partial string match is used as your business logic requires.

The sample code is from a Windows C# console application.

// C# code snippet demonstrating which functions in 
// Foxit Quick PDF Library should be used to find and 
// replace links in PDF files.

// Load PDF to process
DPL.LoadFromFile(@"C:\Temp\Sample.pdf", "");
// Count pages in doc
int pCount = DPL.PageCount();

// Iterate through each page in the doc
for (int p = 1; p <= pCount; p++)
 // Select page to scan

 // Count annotations on the page
 int annotCount = DPL.AnnotationCount();

 // Loop through each annotation
 for (int a = 1; a <= annotCount; a++)
  // Get annotation's ID
  int annotActionID = DPL.GetAnnotActionID(a);

  // Find out annotation's action type, we are looking for URI
  string actionType = DPL.GetActionType(annotActionID);

  // If action type is URI, we've found something to change
  if (actionType == "URI")
   // Retrieve the string for the URI action type holding the URL
   string actionURL = DPL.GetActionURL(annotActionID);

   Console.WriteLine("Index: " + a + ", Annot ID: " + annotActionID + ", Action URL: " + actionURL);

   /* Option 1 */
   // Replace all URLs with a predefined new URL
   // First option is the simplest to demonstrate
   // the functions, just replace all
   // Action URL's with a new fixed URL.
   // Uncomment line below to use Option 1

   // DPL.SetActionURL(annotActionID, "");

   /* Option 2 */
   // Replace portion of URL if match found
   // The second option demonstrates how you
   // can replace one domain name with a new
   // domain name.

   string oldDomain = "";
   string newDomain = "";

   if (actionURL.IndexOf(oldDomain) != -1)
    // Create new StringBuilder from string.
    StringBuilder b = new StringBuilder(actionURL);

    // Replace the first word.
    // ... The result doesn't need assignment.
    b.Replace(oldDomain, newDomain);

    Console.WriteLine("Match found, replacing " + actionURL + " with " + b);

    DPL.SetActionURL(annotActionID, b.ToString());
// Save the updated file to a new file

This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.

Updated on May 16, 2022

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: