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.

Fri May 31, 2019 2:00 am

Hello,

Our Dev team is looking into this issue now. We will keep you informed once there is any progress. Thanks for your patience.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Mon Jun 10, 2019 10:52 am

Hi,

Thanks for your patient waiting.
Please use the following code to copy the bookmarks from your sample PDF.
Code: Select all
//load original pdf
PdfDocument document = new PdfDocument();
document.LoadFromFile(@"TST_BookMark_original.pdf");
//create a new pdf
PdfDocument newPdf = new PdfDocument();
foreach (PdfPageBase page in document.Pages)
{
    PdfPageBase newPage;
    if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
        newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
    else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
        newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
    else
        newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
    page.CreateTemplate().Draw(newPage, new PointF(0, 0));

    //get bookmarks from original pdf
    var bms = document.Bookmarks;

    for (int w = 0; w < bms.Count; w++)
    {
        //Add parent bookmark
        PdfDestination vendorBookmarkDest = bms[w].Destination;
        if (vendorBookmarkDest.Page == page)
        {
            vendorBookmarkDest.Page = newPage;
            PdfBookmark vendorBookmark = newPdf.Bookmarks.Add(bms[w].Title);
            vendorBookmark.Color = bms[w].Color;
            vendorBookmark.DisplayStyle = bms[w].DisplayStyle;
            vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);

            for (int j = 0; j < bms[w].Count; j++)
            {
                //Add child bookmark
                PdfDestination partBookmarkDest = bms[w][j].Destination;
                partBookmarkDest.Page = newPage;
                PdfBookmark partBookmark = vendorBookmark.Add(bms[w][j].Title);
                partBookmark.Color = bms[w][j].Color;
                partBookmark.DisplayStyle = bms[w][j].DisplayStyle;
                partBookmark.Action = new PdfGoToAction(partBookmarkDest);
            }
        }
    }
}
newPdf.SaveToFile(@"final.pdf", Spire.Pdf.FileFormat.PDF);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Wed Jun 12, 2019 12:13 pm

Hi,

I tested this code on some of my files, the most of them works but still have some that the bookmark gets messed up.

Thank You
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Thu Jun 13, 2019 1:29 am

Hello,

Thanks for your feedback.
Please provide your files which could reproduce your issue, and share the error details about the messed up bookmarks. Then we will look into them and help you accordingly. You could send them to us via email ([email protected]). Thanks in advance.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Tue Jun 18, 2019 3:45 am

Hi,

Greetings from E-iceblue!
How is your issue going? Could you please provide the requested information for our reference? So that we can look into it accurately and help you out. Thanks in advance.

Sinecrely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Tue Jun 18, 2019 12:25 pm

Hi, I'm preparing the PDF file in order to send to you guys.

Thank You.
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Wed Jun 19, 2019 2:02 am

Hi,

I will wait for your files and the error details about the messed up bookmarks.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Tue Jun 25, 2019 2:55 pm

Hi,

I'm sorry for the delay to send the files, I just uploaded 2 files one before apply some notes and another file after apply notes, take a look what's happens with the bookmarks.

Thank You
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Wed Jun 26, 2019 12:39 pm

Hi,

Thanks for your sharing.
Please use the following code to copy the correct bookmarks. If there is any question, please feel free to write back.
Code: Select all
 static void Main(string[] args)
 {
     PdfDocument document = new PdfDocument();
     document.LoadFromFile(@"Template_BeforeSpirePDF.pdf");
     PdfDocument newPdf = new PdfDocument();
     Dictionary<PdfBookmark, PdfDestination> allBookmarks = new Dictionary<PdfBookmark, PdfDestination>();
     var bms = document.Bookmarks;
     for (int w = 0; w < bms.Count; w++)
     {
         PdfDestination vendorBookmarkDest = bms[w].Destination;
         PdfBookmark vendorBookmark = newPdf.Bookmarks.Add(bms[w].Title);
         allBookmarks.Add(vendorBookmark, vendorBookmarkDest);
         vendorBookmark.Color = bms[w].Color;
         vendorBookmark.DisplayStyle = bms[w].DisplayStyle;
         vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);

         for (int j = 0; j < bms[w].Count; j++)
         {
             //Add child bookmark
             PdfDestination partBookmarkDest = bms[w][j].Destination;
             PdfBookmark partBookmark = vendorBookmark.Add(bms[w][j].Title);
             allBookmarks.Add(partBookmark, partBookmarkDest);
             partBookmark.Color = bms[w][j].Color;
             partBookmark.DisplayStyle = bms[w][j].DisplayStyle;
             partBookmark.Action = new PdfGoToAction(partBookmarkDest);
         }
     }
     foreach (PdfPageBase page in document.Pages)
     {
         PdfPageBase newPage;
         if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
             newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
         else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
             newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
         else
             newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
         page.CreateTemplate().Draw(newPage, new PointF(0, 0));
         foreach (KeyValuePair<PdfBookmark, PdfDestination> kv in allBookmarks)
         {
             if (kv.Value.Page == page)
                 kv.Value.Page = newPage;
         }
     }
     newPdf.SaveToFile("After.pdf", FileFormat.PDF);
 }

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Thu Jun 27, 2019 9:25 pm

Hi,

I tested out the source code you sent and the good news is looks like it is working, I will perform some more tests and let you know the results.

Thank You very much for the support.
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Fri Jun 28, 2019 1:43 am

Hi,

Thank you so much for your reply.
If you need further assistance, please feel free to contact us.
Wish you all the best!

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF