Rotate Images and PDFs in C# .NET
With PSPDFKit GdPicture.NET Library, it’s possible to rotate PDF pages and images in the following ways:
-
PDF
-
Rotate a PDF page — Rotate a selected page by 90, -90, 180, -180, 270, or -270 (or any multiple of 90) degrees.
-
Rotate a PDF page at any angle — Rotate a selected page by any angle.
-
Rotate all PDF pages — Rotate all PDF pages by 90, -90, 180, -180, 270, or -270 (or any multiple of 90) degrees.
-
-
Images
-
Rotate and flip an image — Rotate an image by 90, -90, 180, -180, 270, or -270 (or any multiple of 90) degrees and flip it around its axis.
-
Rotate an image at any angle – Rotate an image using any angle.
-
Rotating a PDF Page
To rotate a page of a PDF clockwise by a multiple of 90 degrees, follow these steps:
-
Select the page with the
SelectPage
method. -
Rotate the page with the
RotatePage
method. Use multiples of 90 degrees, such as 90, 180, and 270. To rotate a page counterclockwise, use negative values.
The example below rotates the specified page of a PDF file clockwise by 90 degrees:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf", false); gdpicturePDF.SelectPage(2); // Rotate the selected page 90 degrees clockwise. gdpicturePDF.RotatePage(90); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf", False) gdpicturePDF.SelectPage(2) ' Rotate the selected page 90 degrees clockwise. gdpicturePDF.RotatePage(90) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Rotating a PDF Page at Any Angle
To rotate a PDF page counterclockwise by any angle, use the RotatePageEx
method. To rotate a page counterclockwise, use negative values. Before rotating a page, select the page with the SelectPage
method.
To rotate the specified page of a PDF file by -23 degrees, use the following example:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf", false); gdpicturePDF.SelectPage(2); // Rotate the selected page counterclockwise by 23 degrees. gdpicturePDF.RotatePageEx(-23); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf", False) gdpicturePDF.SelectPage(2) ' Rotate the selected page counterclockwise by 23 degrees. gdpicturePDF.RotatePageEx(-23) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Rotating All PDF Pages
To rotate all PDF pages clockwise at the same angle at once, use the RotatePages
method. This method accepts multiples of 90 degrees, such as 90, 180, and 270. To rotate a page counterclockwise, use negative values.
To rotate all pages of a PDF file by -270 degrees, use the following example:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf", false); // Rotate the selected page counterclockwise by 270 degrees. gdpicturePDF.RotatePages(-270); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf", False) ' Rotate the selected page counterclockwise by 270 degrees. gdpicturePDF.RotatePages(-270) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Rotating and Flipping an Image
To both rotate and flip an image by multiples of 90 degrees, such as 90, 180, and 270, use the Rotate
method. To rotate counterclockwise, use negative values. This method requires the RotateFlipType
enumeration, which specifies the angle by which the image is rotated and how it’s flipped. For more information, refer to the RotateFlipType
enumeration.
The
RotateFlipType
enumeration is provided in the .NET COM interop edition only. Applications linking other editions must use theSystem.Drawing.RotateFlipType
enumeration.
To rotate an image 270 degrees and flip it on its Y axis, use the following example:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Rotate the image by 270 degrees clockwise and flip it on its Y axis. gdpictureImaging.Rotate(imageID, RotateFlipType.Rotate270FlipY); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg", 75); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Rotate the image by 270 degrees clockwise and flip it on its Y axis. gdpictureImaging.Rotate(imageID, RotateFlipType.Rotate270FlipY) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg", 75) gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Rotating an Image at Any Angle
To rotate an image clockwise by any angle, use the RotateAngle
method. To rotate a page counterclockwise, use negative values.
To rotate an image by -23 degrees, use the following example:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Rotate the image counterclockwise by 23 degrees. gdpictureImaging.RotateAngle(imageID, -23); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg", 75); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Rotate the image counterclockwise by 23 degrees. gdpictureImaging.RotateAngle(imageID, -23) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg", 75) gdpictureImaging.ReleaseGdPictureImage(imageID) End Using