Articles in this section

How to remove page rotation in a PDF document using C#

The Syncfusion Essential® PDF is a feature-rich and high performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can remove page rotation in a PDF document without altering its content using C#.

Steps to remove page rotation in a PDF document without altering content programmatically:

  1. Create a new console application project.Screenshot (1337).png
  2. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
    Screenshot (2264).png
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
  1. Use the following code sample in Program.cs to remove page rotation in a PDF document without altering content.
    C#
// Create a new PdfDocument instance.
PdfDocument document = new PdfDocument();
// Open the input PDF file using FileStream.
FileStream fileStream = new FileStream(@"../../../Input.pdf", FileMode.Open, FileAccess.Read);
// Load the existing PDF document into a PdfLoadedDocument.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
// Loop through each page in the loaded document.
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
   // Retrieve the loaded page as a PdfLoadedPage.
   PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage;
   // Create a template from the loaded page for rendering.
   PdfTemplate template = loadedPage.CreateTemplate();
   // Add a new section to the output document.
   PdfSection pdfSection = document.Sections.Add();
   // Set margins for the section to zero.
   pdfSection.PageSettings.Margins.All = 0;
   // Set the page size to match the loaded page's client size.
   pdfSection.PageSettings.Size = new SizeF(loadedPage.Graphics.ClientSize);
   // Add a new page to the section.
   PdfPage newPage = pdfSection.Pages.Add();
   // Save the current graphics state for the new page.
   PdfGraphicsState state = newPage.Graphics.Save();
   // Handle page rotation based on the loaded page's rotation angle.
   if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle90)
   {
       // Rotate the graphics context 90 degrees clockwise.
       newPage.Graphics.TranslateTransform(newPage.Graphics.ClientSize.Width, 0);
       newPage.Graphics.RotateTransform(90);
   }
   else if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle270)
   {
       // Rotate the graphics context 270 degrees clockwise (90 degrees counter-clockwise).
       newPage.Graphics.TranslateTransform(0, newPage.Graphics.ClientSize.Height);
       newPage.Graphics.RotateTransform(270);
   }
   // Draw the template onto the new page at the origin (0,0).
   newPage.Graphics.DrawPdfTemplate(template, new PointF(0, 0));
   // Restore the graphics state to its original state.
   newPage.Graphics.Restore(state);
}
// Create a MemoryStream to hold the output PDF data.
MemoryStream ms = new MemoryStream();
// Save the document to the MemoryStream.
document.Save(ms);
// Write the content of the MemoryStream to an output PDF file.
File.WriteAllBytes("output.pdf", ms.ToArray());
// Close the output document and release resources.
document.Close(true);
// Close the loaded document and release resources.
loadedDocument.Close(true);

A complete working sample can be downloaded from Removing_page_rotation.zip

By executing the program, you will get the PDF document as follows.

Screenshot (2265).png

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied