How to rotate images in a PDF 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 draw rotated image in the PDF document using C# and VB.NET.
Steps to rotate image in PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics
- Use the following code snippet to create PDF and draw rotated image.
C#
//Create a document PdfDocument doc = new PdfDocument(); //Add a new page PdfPage page = doc.Pages.Add(); //Load an image PdfBitmap image = new PdfBitmap( "logo.gif"); //Save the current graphics state PdfGraphicsState state = page.Graphics.Save(); //Translate the coordinate system to the required position page.Graphics.TranslateTransform(120, 200); //Rotate the coordinate system page.Graphics.RotateTransform(-45); // Draw an image image.Draw(page, 0, 0); //Restore the graphics state page.Graphics.Restore(state); //Save the PDF doc.Save("RotatedImage.pdf"); doc.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("RotatedImage.pdf");
VB.NET
'Create a document Dim doc As PdfDocument = New PdfDocument 'Add a new page Dim page As PdfPage = doc.Pages.Add 'Load an image Dim image As PdfBitmap = New PdfBitmap(( "logo.gif")) 'Save the current graphics state Dim state As PdfGraphicsState = page.Graphics.Save 'Translate the coordinate system to the required position page.Graphics.TranslateTransform(120, 200) 'Rotate the coordinate system page.Graphics.RotateTransform(-45) ' Draw an image image.Draw(page, 0, 0) 'Restore the graphics state page.Graphics.Restore(state) 'Save the PDF doc.Save("RotatedImage.pdf") doc.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("RotatedImage.pdf")
A complete working sample can be downloaded from RotatePDFImage.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with images, where you will find other options like inserting, replacing image in PDF document, image pagination, and image masking.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
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.