DEV Community

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

Posted on

3 1

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() + ")");
    }
}

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

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:

// For complete examples and data files, please go to https://github.com/groupdocs-merger/GroupDocs.Merger-for-.NET
string sourceFile = CommonUtilities.sourcePath + fileName;
Stream openFile = new FileStream(sourceFile, FileMode.Open);
DocumentResult result = new DocumentHandler().RotatePages(openFile, RotateMode.Rotate270);
Stream documentStream = result.Stream;
//output file
var fileStream = File.Create(CommonUtilities.outputPath + "OutPut." + result.FileFormat);
documentStream.CopyTo(fileStream);
documentStream.Close();

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay