)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change the PDF page orientation in PDF viewer

Platform: WinForms |
Control: PdfViewer |
Published Date: December 11, 2015 |
Last Revised Date: August 27, 2020

Change the PDF page orientation in PDF viewer by using PdfTemplate of the loaded document. In this sample, we have created PdfTemplate of the page and redrew the template in the new PdfDocument after changing the orientation. The following code example helps you to change the PDF page orientation in PDF viewer.

C#:

/// <summary>
/// Handles the page orientation changed functionality
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void LandScapeCB_CheckedChanged(object sender, System.EventArgs e)
        {
            //create a new document
            PdfDocument document = new PdfDocument();
            if (!IsOrientationChanged)
            {
                document = ChangeOrientation(document, IsOrientationChanged);
                IsOrientationChanged = true;
            }
            else
            {
                document = ChangeOrientation(document, IsOrientationChanged);
                IsOrientationChanged = false;
            }
            //saving the orientation changed document in stream
            Stream stream = new MemoryStream();
            document.Save(stream);
            //recreate the loaded document by using orientation changed document
            loadedDocument = new PdfLoadedDocument(stream);
 
            //reload the document in pdfviewer
            pdfViewerControl.Load(loadedDocument);
        }
/// <summary>
/// Change the pdf document page orientation 
/// </summary>
/// <param name="document">pdf document to change the page orientation</param>
/// <param name="isOrientationChanged">to identify the page orientation was changed</param>
private PdfDocument ChangeOrientation(PdfDocument document, bool isOrientationChanged)
        {
            document.PageSettings.Margins.All = 0;
            foreach (PdfPageBase loadedPage in loadedDocument.Pages)
            {
                float width = loadedPage.Size.Width;
                float height = loadedPage.Size.Height;
                if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle90 ||
                    loadedPage.Rotation == PdfPageRotateAngle.RotateAngle270)
                {
                    width = loadedPage.Size.Height;
                    height = loadedPage.Size.Width;
                }
 
                //Change the orientation
                if (width > height)
                    document.PageSettings.Orientation = PdfPageOrientation.Portrait;
                else
                    document.PageSettings.Orientation = PdfPageOrientation.Landscape;
                document.PageSettings.Size = new SizeF(width, height);
 
                //Add pages to new document
                PdfPage page = document.Pages.Add();
                PdfGraphics graphics = page.Graphics;
                PdfTemplate template = loadedPage.CreateTemplate();
 
                //Convert landscape to portrait
                if (document.PageSettings.Orientation == PdfPageOrientation.Landscape)
                {
                    PdfGraphicsState state = graphics.Save();
                    graphics.TranslateTransform(height / 2, width / 2);
                    if (!isOrientationChanged)
                    {
                        graphics.ScaleTransform(width / height, width / height);
                    }
                    else
                    {
                        graphics.ScaleTransform(height / width, height / width);
                    }
                    if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle90)
                    {
                        graphics.RotateTransform(90);
                        graphics.TranslateTransform(-height / 2, -width / 2);
                    }
                    else if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle270)
                    {
                        graphics.RotateTransform(270);
                        graphics.TranslateTransform(-height / 2, -width / 2);
                    }
                    else if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle180)
                    {
                        graphics.RotateTransform(180);
                        graphics.TranslateTransform(-width / 2, -height / 2);
                    }
                    else
                    {
                        graphics.TranslateTransform(-width / 2, -height / 2);
                    }
                    graphics.DrawPdfTemplate(template, new PointF(0, 0));
                    graphics.Restore(state);
                }
                //Convert portrait to landscape
                else
                {
                    PdfGraphicsState state = graphics.Save();
                    graphics.TranslateTransform(height / 2, width / 2);
                    if (!isOrientationChanged)
                    {
                        graphics.ScaleTransform(height / width, height / width);
                    }
                    else
                    {
                        graphics.ScaleTransform(width / height, width / height);
                    }
                    if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle90)
                    {
                        graphics.RotateTransform(90);
                        graphics.TranslateTransform(-height / 2, -width / 2);
                    }
                    else if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle270)
                    {
                        graphics.RotateTransform(270);
                        graphics.TranslateTransform(-height / 2, -width / 2);
                    }
                    else if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle180)
                    {
                        graphics.RotateTransform(180);
                        graphics.TranslateTransform(-width / 2, -height / 2);
                    }
                    else
                    {
                        graphics.TranslateTransform(-width / 2, -height / 2);
                    }
                    graphics.DrawPdfTemplate(template, new PointF(0, 0));
                    graphics.Restore(state);
                }
            }
            return document;
        }

 

Sample:

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon