Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue May 10, 2022 1:44 pm

I am using sample copy bookmark code from one document to another. The bookmarks are getting created, but when you click the bookmark in the new document it does not take you to the bookmark location in the document.

Code: Select all
public static void CopyToUnlocked(Stream lockedStream, Stream outputStream)
{
   var lockedPDF = new PdfDocument();
   var unlockedPDF = new PdfDocument();
   int lockedPDFPageCount;
   var errorUnlockingPDF = false;

   try { lockedPDF.LoadFromStream(lockedStream); }
   catch (Exception) { errorUnlockingPDF = true; }

   try { lockedPDFPageCount = lockedPDF.Pages.Count; }
   catch (Exception) { errorUnlockingPDF = true; }

   if (!errorUnlockingPDF)
   {
      foreach (PdfPageBase page in lockedPDF.Pages)
      {
         PdfPageBase newPage;
         if (page.Rotation == PdfPageRotateAngle.RotateAngle90 || page.Rotation == PdfPageRotateAngle.RotateAngle270)
            newPage = unlockedPDF.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
         else
            newPage = unlockedPDF.Pages.Add(new SizeF(page.Size.Width, page.Size.Height), new PdfMargins(0));

         page.CreateTemplate().Draw(newPage, new PointF(0, 0));
      }
      //get bookmarks from original pdf
      var bms = lockedPDF.Bookmarks;
      for (int i = 0; i < bms.Count; i++)
      {
         //add parent bookmark
         PdfDestination vendorBookmarkDest = new PdfDestination(bms[i].Destination.Page, bms[i].Destination.Location);
         PdfBookmark vendorBookmark = unlockedPDF.Bookmarks.Add(bms[i].Title);
         vendorBookmark.Color = bms[i].Color;
         vendorBookmark.DisplayStyle = bms[i].DisplayStyle;
         vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);

         for (int j = 0; j < bms[i].Count; j++)
         {
            //add child bookmark
            PdfDestination partBookmarkDest = new PdfDestination(bms[i][j].Destination.Page, bms[i][j].Destination.Location);
            PdfBookmark partBookmark = vendorBookmark.Add(bms[i][j].Title);
            partBookmark.Color = bms[i][j].Color;
            partBookmark.DisplayStyle = bms[i][j].DisplayStyle;
            partBookmark.Action = new PdfGoToAction(partBookmarkDest);
         }
      }
   }
   if (errorUnlockingPDF)
      lockedStream.CopyTo(outputStream);
   else
      unlockedPDF.SaveToStream(outputStream);
}


terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Wed May 11, 2022 9:57 am

Hello,

Thanks for your inquiry.

I found some PDFs with bookmarks and tested your code but I did not reproduce the issue you mentioned. All bookmarks works fine in the new PDF. Attached are my test files and code. Please check it.
Besides, please check the version of Spire.Pdf. If the version you are using is not the lastest(v 8.5.0), please upgrade and test again. If the issue still exist in the lastest version, please share your PDF file to us. You can upload it here or send it to us via email(support@e-iceblue.com). Thanks in advance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed May 11, 2022 3:41 pm

When I open the DeleteBookmark.pdf document and click on a bookmark it takes me to the location in the document.
When I open the Output.pdf document, the bookmarks are there, but when I click on the bookmark it does not take to the location in the document.

I am using Foxit PhantomPDF v 10.1

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Thu May 12, 2022 8:02 am

Hi,

Thanks for your reply.

At present, the functional effects of Spire.PDF are most realized with Adobe Acrobat as a reference. Therefore, the compatibility with Foxit is not particularly perfect. I recommend that you use Adobe Acrobat to open the resulting document first.
In addition, I also reported this issue to the Dev team to further improve compatibility with Foxit Reader. The issue ticket is SPIREPDF-5186. I will keep you informed of any updates.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Fri May 13, 2022 9:55 am

Hello,

I'm writing to give you an update about the issue. In fact, there are some inappropriate places in our code that need to be modified. When adding links to new bookmarks, we should use the page object of the new document instead of the original document. Please use the following modified code to test again.
Code: Select all
        public static void CopyToUnlocked(Stream lockedStream, Stream outputStream)
        {
            var lockedPDF = new PdfDocument();
            var unlockedPDF = new PdfDocument();
            int lockedPDFPageCount;
            var errorUnlockingPDF = false;

            try { lockedPDF.LoadFromStream(lockedStream); }
            catch (Exception) { errorUnlockingPDF = true; }

            try { lockedPDFPageCount = lockedPDF.Pages.Count; }
            catch (Exception) { errorUnlockingPDF = true; }

            if (!errorUnlockingPDF)
            {
                foreach (PdfPageBase page in lockedPDF.Pages)
                {
                    PdfPageBase newPage;
                    if (page.Rotation == PdfPageRotateAngle.RotateAngle90 || page.Rotation == PdfPageRotateAngle.RotateAngle270)
                        newPage = unlockedPDF.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
                    else
                        newPage = unlockedPDF.Pages.Add(new SizeF(page.Size.Width, page.Size.Height), new PdfMargins(0));

                    page.CreateTemplate().Draw(newPage, new PointF(0, 0));
                }
                //get bookmarks from original pdf
                var bms = lockedPDF.Bookmarks;
                int pageIndex = 0;
                for (int i = 0; i < bms.Count; i++)
                {
                    pageIndex = lockedPDF.Pages.IndexOf(bms[i].Destination.Page);
                    //add parent bookmark
                    //******We should link the bookmark to the page in the new PDF instead of the orginal one
                    PdfDestination vendorBookmarkDest = new PdfDestination(unlockedPDF.Pages[pageIndex], bms[i].Destination.Location);
                    PdfBookmark vendorBookmark = unlockedPDF.Bookmarks.Add(bms[i].Title);
                    vendorBookmark.Color = bms[i].Color;
                    vendorBookmark.DisplayStyle = bms[i].DisplayStyle;
                    vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);

                    for (int j = 0; j < bms[i].Count; j++)
                    {
                        //add child bookmark
                        pageIndex = lockedPDF.Pages.IndexOf(bms[i][j].Destination.Page);
                        PdfDestination partBookmarkDest = new PdfDestination(unlockedPDF.Pages[pageIndex], bms[i][j].Destination.Location);
                        PdfBookmark partBookmark = vendorBookmark.Add(bms[i][j].Title);
                        partBookmark.Color = bms[i][j].Color;
                        partBookmark.DisplayStyle = bms[i][j].DisplayStyle;
                        partBookmark.Action = new PdfGoToAction(partBookmarkDest);
                    }
                }
            }
            if (errorUnlockingPDF)
                lockedStream.CopyTo(outputStream);
            else
                unlockedPDF.SaveToStream(outputStream);
        }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Fri May 13, 2022 3:40 pm

The new code works perfectly in Foxit PhantomPDF.

Thank you. My issue is solved.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Mon May 16, 2022 7:36 am

Glad to hear that!

Have a nice day!
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.PDF