How to rotate the PDF file in C#

With the help of Spire.PDF for .NET, developers can easily rotate the PDF in C# and VB.NET. We have already demonstrated how to set the rotation for the newly added PDF files via setting of section.PageSettings.Rotate. Spire.PDF also supports to rotate the existing PDF files. When we need to rotate an existing PDF file, usually it has already rotated with some angle. This article will show you to how to rotate an existing PDF page in C#.

Please check the original PDF file already with 90 rotation angle:

How to rotate the PDF file in C#

Here comes to the code snippet of how to rotate an existing PDF file in C#.

Step 1: Create a new instance of PdfDocument and load a PDF document from file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample.pdf");

Step 2: Get the first page of the loaded PDF file.

PdfPageBase page = doc.Pages[0];

Step 3: Rotate the PDF page based on the original rotation angle.

int rotation = (int)page.Rotation;
rotation += (int)PdfPageRotateAngle.RotateAngle270;
page.Rotation = (PdfPageRotateAngle)rotation;

Step 4: Save the document to file.

doc.SaveToFile("Result.pdf");

The effective screenshot of the PDF page which rotated 270 rotation angle:

How to rotate the PDF file in C#

Full codes:

{
    PdfDocument pdf = new PdfDocument();
    pdf.LoadFromFile("Sample.PDF");

    PdfPageBase page = pdf.Pages[0];

    int rotation = (int)page.Rotation;
    rotation += (int)PdfPageRotateAngle.RotateAngle270;
    page.Rotation = (PdfPageRotateAngle)rotation;
    pdf.SaveToFile("Result.pdf");
}