DEV Community

Cover image for Rotating PDF page in C#
Ali Jago Seif
Ali Jago Seif

Posted on

Rotating PDF page in C#

Here I am going to show you how to rotate PDF in C# with RotatePage method. I use BCL's PDF Library to process data in PDF files. It also allows you to create, manipulate and convert files to PDF from multiple sources accurately.

PDF Processor API, that allows to process and manipulate existing PDF documents, has several methods. The one that does pages rotation is called "RotatePages".

This particular code below rotates the page(s). All the parameters should be set in this string:
oPDFProcessor.RotatePages(string InputFileName,string OutputFileName,int From,int To,prcPageRotation.PRC_ROT_{angle}_DEG
MessageBox.Show("Rotate Success!");

Parameters:
InputFileName - Input file name;
OutputFileName - Output file name;
From - Rotate page from (first page = 0);
To - Rotate Page to;
Angle - 0|90|180|270|clockwise|counterclockwise|anticlockwise|upsidedown.

This code demonstrates how to rotate pages. The code rotates the first page in a PDF file by 90 degrees.

private void Rotate_Test(string inFile, string outFile)
{
    try
    {
        PDFProcessor oPDFProcessor = new PDFProcessor();
            // rotate the first page 90 by degrees
oPDFProcessor.RotatePages(inFile,outFile,0,0,prcPageRotation.PRC_ROT_90_DEG 
        MessageBox.Show("Rotate Success!");
    }
    catch(System.Runtime.InteropServices.COMException err)
    {
        MessageBox.Show(err.Message + " (" + err.ErrorCode.ToString() + ")");
    }
}

Top comments (1)

Collapse
 
atir_tahir profile image
Atir Tahir • Edited

Page rotation or other document manipulation operations (split, swap, trim, remove) become easier with GroupDocs.Merger for .NET API.
API allows you to rotate single or list of pages. See how comfy is the code:

The best thing about this API is, it is not just limited to PDF it also supports Word, Excel, PowerPoint formats.