Created
March 25, 2024 07:51
How to Rotate PDF using C#. For more details: https://kb.conholdate.com/total/net/how-to-rotate-pdf-using-csharp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Pdf; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Initialize license | |
License lic = new License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Load the PDF | |
Document originalDoc = new Document("input.pdf"); | |
// Parse the pages | |
foreach (Page pg in originalDoc.Pages) | |
{ | |
// Change the page orientation | |
Rectangle r = pg.MediaBox; | |
double updatedHeight = r.Width; | |
double updatedWidth = r.Height; | |
double updatedLLX = r.LLX; | |
double updatedLLY = r.LLY + (r.Height - updatedHeight); | |
pg.MediaBox = new Rectangle(updatedLLX, updatedLLY, updatedLLX + updatedWidth, updatedLLY + updatedHeight); | |
pg.CropBox = new Rectangle(updatedLLX, updatedLLY, updatedLLX + updatedWidth, updatedLLY + updatedHeight); | |
// Rotate the page contents | |
pg.Rotate = Rotation.on270; | |
} | |
// Save the rotated PDF file | |
originalDoc.Save("rotated.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment