How to rotate a PDF document based on angle using C# and VB.NET?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can rotate a PDF document based on angle using C# and VB.NET.
Steps to rotate PDF based on angle programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- Use the following code snippet to rotate a PDF document based on angle.
C#
//Load a PDF document PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf"); //Create a new instance of PdfDocument class PdfDocument doc = new PdfDocument(); //Set document rotation angle as 90 degrees doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90; //Import PDF document doc.ImportPageRange(ldoc, 0, ldoc.Pages.Count - 1); //Save and Close the document doc.Save("rotate90.pdf"); doc.Close(true); //Create a new instance of PdfDocument class doc = new PdfDocument(); //Set document rotation angle as 180 degrees doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180; //Import PDF document doc.ImportPageRange(ldoc, 0, ldoc.Pages.Count - 1); //Save and Close the document doc.Save("rotate180.pdf"); doc.Close(true); //Create a new instance of PdfDocument class doc = new PdfDocument(); //Set document rotation angle as 270 degrees doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270; //Import PDF document doc.ImportPageRange(ldoc, 0, ldoc.Pages.Count - 1); //Save and Close the document doc.Save("rotate270.pdf"); doc.Close(true);
VB.NET
'Load a PDF document Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf") 'Create a new instance of PdfDocument class Dim doc As PdfDocument = New PdfDocument() 'Set document rotation angle as 90 degree doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90 'Import PDF document doc.ImportPageRange(ldoc, 0, (ldoc.Pages.Count - 1)) 'Save and Close the document doc.Save("rotate90.pdf") doc.Close(True) 'Create a new instance of PdfDocument class doc = New PdfDocument() 'Set document rotation angle as 180 degree doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180 'Import PDF document doc.ImportPageRange(ldoc, 0, (ldoc.Pages.Count - 1)) 'Save and Close the document doc.Save("rotate180.pdf") doc.Close(True) 'Create a new instance of PdfDocument class doc = New PdfDocument() 'Set document rotation angle as 270 degree doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270 'Import PDF document doc.ImportPageRange(ldoc, 0, (ldoc.Pages.Count - 1)) 'Save and Close the document doc.Save("rotate270.pdf") doc.Close(True)
A complete working sample can be downloaded from RotatePDFBasedOnAngle.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with document, where you will find other options like orientation, transition, and standard or custom page size in the PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link for adding pages with different settings such as rotation, orientation, page size, and transition.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to rotate a PDF document based on angle using C# and VB.NET.
You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and otherWinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!