Bookmarks added to PdfViewer.LoadedDocument are not saved when saving PdfViewer

Hi,

I am trying to add a bookmark to a PdfViewer.LoadedDocument and save the content of PdfViewer to a StorageFile, so that the bookmark and the annotations are present in the output file.

Here's my code , where I'm trying to achieve this:

private async void Button_Click(object sender, RoutedEventArgs e)

        {

            var bookmark = pdfViewer.LoadedDocument.Bookmarks.Add("First page");

            bookmark.Destination = new PdfDestination(pdfViewer.LoadedDocument.Pages[0]);

            var stream = pdfViewer.Save();

            stream.Position = 0;


            var fileSavePicker = new FileSavePicker();

            fileSavePicker.FileTypeChoices.Add("PDF", new[] { ".pdf" });

            var file = await fileSavePicker.PickSaveFileAsync();


            using (var fileWriteStream = await file.OpenStreamForWriteAsync())

            {

                await stream.CopyToAsync(fileWriteStream);

            }

        }
The output file includes annotations, but it does not include added bookmark.
Could somebody help me to find the solution?

Thanks very much.


1 Reply

MA ManojKumar Arumugasamy Syncfusion Team May 24, 2023 12:15 PM UTC

Hi Andrii Hnatushchenko,

We need to insert the bookmark in the existing pdfLoadedDocument. Find more details at the following link:  https://help.syncfusion.com/file-formats/pdf/working-with-bookmarks?cs-save-lang=1&cs-lang=csharp#inserting-bookmarks-in-an-existing-pdf


Please find the modified code here:

private async void Button_Click(object sender, RoutedEventArgs e)

        {

            PdfLoadedDocument pdfLoadedDocument = PdfViewer.LoadedDocument;

            PdfBookmark bookmark = pdfLoadedDocument.Bookmarks.Insert(0,"First page");

 

            bookmark.Destination = new PdfDestination(pdfLoadedDocument.Pages[0]);

            bookmark.Destination.Location = new PointF(0, 300);

 

            var stream = new MemoryStream();

            pdfLoadedDocument.Save(stream);

            pdfLoadedDocument.Close();

            stream.Position = 0;

 

            var fileSavePicker = new FileSavePicker();

            fileSavePicker.FileTypeChoices.Add("PDF", new[] { ".pdf" });

            var file = await fileSavePicker.PickSaveFileAsync();

            using (var fileWriteStream = await file.OpenStreamForWriteAsync())

            {

                await stream.CopyToAsync(fileWriteStream);

            }

        }


Loader.
Up arrow icon