SfPdfViewer doesn't update when a change is made to SfPdfViewer.LoadedDocument property

Hi,

I am displaying a pdf-file using SfPdfViewer. I want to rotate the first page of the pdf file, so that it's displayed on the UI.

Here's my code:

 public async Task LoadAndRotateAsync(StorageFile storageFile)

        {

            await PdfViewerControl.LoadDocumentAsync(storageFile);

            var firstPage = PdfViewerControl.LoadedDocument.Pages[0];

            if (firstPage.Rotation != PdfPageRotateAngle.RotateAngle90)

            {

                firstPage.Rotation = PdfPageRotateAngle.RotateAngle90;

            }

            else

            {

                firstPage.Rotation = PdfPageRotateAngle.RotateAngle270;

            }

        }

I also tried subscribing to DocumentLoaded event, but it didn't help

private void OnDocumentLoaded(object sender, DocumentLoadedEventArgs e)

        {

            var firstPage = PdfViewerControl.LoadedDocument.Pages[0];

            if (firstPage.Rotation != PdfPageRotateAngle.RotateAngle90)

            {

                firstPage.Rotation = PdfPageRotateAngle.RotateAngle90;

            }

            else

            {

                firstPage.Rotation = PdfPageRotateAngle.RotateAngle270;

            }

        }

Could somebody help me to find the solution?

Thanks very much.


3 Replies

MA ManojKumar Arumugasamy Syncfusion Team May 23, 2023 05:22 AM UTC

Hi Andrii Hnatushchenko,

We don’t support the rotation of documents on the pdf viewer, although you could use the pdf library (Syncfusion.Pdf.UWP) to rotate and load in our viewer. Please find the documentation to rotate the pdf page.

https://help.syncfusion.com/file-formats/pdf/working-with-pages#rotating-an-existing-pdf-page

 

We have created a sample that meets your requirements.

https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample1824651930782716



AH Andrii Hnatushchenko replied to ManojKumar Arumugasamy May 23, 2023 07:47 AM UTC

Hi,

Thank you for your response.

Now I understand that it's not directly supported. However, I would like to explore an alternative solution. Could you please suggest an efficient way to change the underlying document of the PdfViewer without losing the user's current view.

I have the following example

public async Task LoadAndRotateAsync(StorageFile storageFile)

{

await PdfViewerControl.LoadDocumentAsync(storageFile);

var loadedDocument = PdfViewerControl.LoadedDocument;

var firstPage = loadedDocument.Pages[0];

if (firstPage.Rotation != PdfPageRotateAngle.RotateAngle90)

{

firstPage.Rotation = PdfPageRotateAngle.RotateAngle90;

}

else

{

firstPage.Rotation = PdfPageRotateAngle.RotateAngle270;

}


var memoryStream = new MemoryStream();

loadedDocument.Save(memoryStream);

loadedDocument.Dispose();

PdfViewerControl.LoadDocument(memoryStream);

}

Is this an efficient way of dealing with my problem? Can you suggest a better alternative?

Thank you once again for your assistance.



MA ManojKumar Arumugasamy Syncfusion Team May 24, 2023 05:21 AM UTC

Hi Andrii Hnatushchenko,

Our current architecture does not support the rotation of individual pages after they are loaded to the viewer; they need to be reloaded after rotation. However, you could rotate and load the viewer using the following code:

public async Task LoadAndRotateAsync(StorageFile storageFile)

        {

            var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

            Stream fileStream = stream.AsStreamForRead();

            byte[] buffer = new byte[fileStream.Length];

            fileStream.Read(buffer, 0, buffer.Length);

            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);

            var firstPage = loadedDocument.Pages[0];

            if (firstPage.Rotation != PdfPageRotateAngle.RotateAngle90)

            {

                firstPage.Rotation = PdfPageRotateAngle.RotateAngle90;

            }

            else

            {

                firstPage.Rotation = PdfPageRotateAngle.RotateAngle270;

            }

            var memoryStream = new MemoryStream();

            loadedDocument.Save(memoryStream);

            loadedDocument.Dispose();

            PdfViewerControl.LoadDocument(memoryStream);

        }


Loader.
Up arrow icon