Category / Section
How to change the PDF page orientation in PDF viewer
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