title: How do I modify external link in an existing PDF such that it points to an internal page within document using IText7 and C#?
body: <p>I have an existing PDF file which has links to external PDF files. I want to edit these links to make them point to pages within the same PDF document. This functionality was working previously with iTextsharp, now I'm migrating to iText7 but can't get it to work.</p>
  <p>Below is a sample code that I have tried and feel is very close to the solution but something is missing. This code basically loads up a PDF with 2 pages. The first page has around 15 links all pointing to an external file. I'm trying to edit the links so that they take the user to page 2 of the same document. I am able to load all the links and query their values, but changing them doesn't happen.</p>
  <pre class="lang-c# prettyprint-override"><code>    private bool MakeLinksInternal(string inputFile)
      {
        if (Path.GetExtension(inputFile).ToLower() != &quot;.pdf&quot;)
          return false;
  
        using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile)))
        {
          //get the index page
          PdfPage pdfPage = pdfDocument.GetPage(1);
          //get all of the link annotations for the current page
          var annots = pdfPage.GetAnnotations().Where(a =&gt; a.GetSubtype().Equals(PdfName.Link));
  
          //Make sure we have something
          if ((annots == null) || (annots.Count() == 0))
            return true;
  
          foreach (PdfLinkAnnotation linkAnnotation in annots)
          {
            //get action associated to the annotation
            var action = linkAnnotation.GetAction();
            if (action == null)
              continue;
  
            // Test if it is a URI action 
            if (action.Get(PdfName.S).Equals(PdfName.URI)
              || action.Get(PdfName.S).Equals(PdfName.GoToR))
            {
              action.Remove(PdfName.S);
  
              action.Put(PdfName.S, PdfName.GoTo);
  
              var newLocalDestination = new PdfArray();
              newLocalDestination.Add(pdfDocument.GetPage(2).GetPdfObject());
              newLocalDestination.Add(PdfName.Fit);
  
              action.Put(PdfName.D, newLocalDestination);
            }
  
          }
          pdfDocument.Close();
        }
  
        return true;
      }
  </code></pre>
  <p>This is my first question in stackoverflow so please be kind if I have made any mistakes in creating this post.</p>