title: iTextSharp retain named destinations and bookmarks after writing a new document
body: <p>I have a method that fixes my document so it opens on Page Level Zoom, the bookmarks are retained but I can't seem to keep my named destinations (anchors) in my new document. I commented in my method what I thought would fix the problem, I used the code before in another method.</p>
  
  <pre><code>private static void FixZoom(string source)
      {
          var reader = new PdfReader(new FileStream(source, FileMode.Open, FileAccess.Read));                        
          var size = reader.GetPageSizeWithRotation(1);
  
          using (var document = new iTextSharp.text.Document(size))
          {                
              var path = String.Empty;
              var dirName = Path.GetDirectoryName(source);
              if (dirName != null)
              {
                  path = Path.Combine(dirName, "Zoom" + Path.GetFileName(source));
              }
  
              using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
              {
                  using (var writer = PdfWriter.GetInstance(document, stream))                
                  {                                                                    
                      document.Open();
  
                      var bookmarks = SimpleBookmark.GetBookmark(reader);
  
                      var cb = writer.DirectContent;
  
                      var pdfDest = new PdfDestination(PdfDestination.FIT);
  
                      var action = PdfAction.GotoLocalPage(1, pdfDest, writer);
  
                      for (var pageNumber = 1; pageNumber &lt;= reader.NumberOfPages; pageNumber++)
                      {                        
                          document.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));
                          document.NewPage();
                          var page = writer.GetImportedPage(reader, pageNumber);
                          cb.AddTemplate(page, 0, 0);
                      }
  
                      //BEGIN: This is not working
                      var map = SimpleNamedDestination.GetNamedDestination(reader, false);
                      if (map.Count &gt; 0)
                      {
                          writer.AddNamedDestinations(map, reader.NumberOfPages);
                      }
                      //END: This is not working
  
                      var pageMode = 0;
                      pageMode += PdfWriter.PageLayoutOneColumn;
  
                      writer.SetOpenAction(action);
                      writer.ViewerPreferences = pageMode;
  
                      writer.Outlines = bookmarks;
                      document.Close();
                  }
              }
  
              File.Delete(source);
              File.Move(path, source);                
          }
      }
  </code></pre>
  
  <p>After the comment I fixed it with this code.</p>
  
  <pre><code>private static void FixZoom(string source)
      {
          var reader = new PdfReader(new FileStream(source, FileMode.Open, FileAccess.Read));
          var size = reader.GetPageSizeWithRotation(1);
  
          using (var document = new iTextSharp.text.Document(size))
          {
              var path = String.Empty;
              var dirName = Path.GetDirectoryName(source);
              if (dirName != null)
              {
                  path = Path.Combine(dirName, "Zoom" + Path.GetFileName(source));
              }
  
              using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
              {
                  using (var stamper = new PdfStamper(reader, stream))
                  {
                      document.Open();                        
                      var pdfDestination = new PdfDestination(PdfDestination.FIT);
                      var pdfAction = PdfAction.GotoLocalPage(1, pdfDestination, stamper.Writer);
  
                      stamper.Writer.SetOpenAction(pdfAction);
  
                      document.Close();
                  }                                                                
              }
  
              File.Delete(source);
              File.Move(path, source);
          }
      }
  </code></pre>