How to find the page number the destination points to in the remote PDF document?

Q:
I have a question about remote destinations. Given a PDF document which
contains a destination label (and another file will have a link to this
document); how do I find out what page that destination label occurs
on.
----

A:

To find the page number in the remote PDF document you need to check
the destination type (as described in Section 8.5 'Remote Go-To
Actions' & TABLE 8.46 in PDF Reference).

// C# pseudocode
int num_annots = page.GetNumAnnots();
for (int i=0; i<num_annots; ++i) {
  Annot annot = page.GetAnnot(i);
  if (annot.GetType() == Annot.Type.e_Link) {
    Action action = annot.GetLinkAction();
    if (action.GetType() == Action::e_GoTo) {
      int page_num = action.GetDest().GetPage().GetIndex();
    }
    else if (action.GetType() == Action::e_GoToR) {
      // A remote/external goto action...
      Obj action_dict = action.GetSDFObj();

      // Get the file specification dict
      Obj file_dict = action_dict.Get("F").Value();
      FileSpec file_spec = new FileSpec(file_dict);
      String file_path = file_spec.GetFilePath();

    // Get the remote destination.
      Obj dest = action_dict.Get("D").Value();
      if (dest.IsString()) {
        // Find named destination in the remote document
        // Format used in PDF 1.2 and above
      ... Open the remote_doc ...
    Obj name_tree_root =
remote_doc.GetRoot().Get("Names").Value().Get("Dests").Value();
      NameTree nt = new NameTree(name_tree_root);

    NameTreeIterator itr = nt.Find(dest.GetBuffer(), dest.Size());
    if (itr != nt.End() {
          Destination remote_dest = new Destination(itr.Value());
          int page_num = remote_dest.GetPage().GetIndex();
      ...
        }
    }
      else if (dest.IsName()) {
        // Find named destination in the remote document
      // PDF 1.1 format which is obsolete these days.
      ... Open the remote_doc ...
        Obj rdest =
remote_doc.GetRoot().Get("Dests").Value().Get(dest.GetName()).Value();
    Destination remote_dest = new Destination(rdest);
    int page_num = remote_dest.GetPage().GetIndex();
      }
      else {
        // In this case 'dest' is an array defining an
        // explicit destination. Its first element must be a page
        // number within the remote document rather. The first
        // page is numbered 0.
      int page_num = (int) dest->GetAt(0)->GetNumber();
      }
    }
  }