Articles in this section
Category / Section

How to rotate an image around its center in PDF document using C#?

6 mins read

The Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library that is used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files. Using this library, you can rotate an image around its center in a pdf document.

Steps to rotate an image around its center in the pdf document programmatically

1. Create a new C# console application project.

Create console application

2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from the NuGet.org.

NuGet package reference

3. 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

 

4. The following code example shows how to rotate an image around its center in the pdf document using C#.

C#

            //Create a document
            PdfDocument doc = new PdfDocument();
            string[] files = Directory.GetFiles(Path.Combine("../../Images/"));
            //Add a new page
            PdfPage page = doc.Pages.Add();
            int rotationAngle = 270;
            float x = 40, y = 400;
            for (int i = 0; i < files.Length; i++)
            {
                //Load an image
                PdfBitmap image = new PdfBitmap(files[i]);
 
                DrawRotateImage(page, image, rotationAngle, x, y);
 
                //provide the angle and x and y position based on your requirement. 
                rotationAngle += 40;
                x += 300;
                y += 100;
            }
 
            string outputFileName = rotationAngle.ToString() + ".pdf";
            //Save the PDF
            doc.Save(outputFileName);
            //Close the PDF 
            doc.Close(true);
 
 
        /// <summary>
        /// Calculate the translate transform
        /// </summary>
        private static void DrawRotateImage(PdfPage page, PdfBitmap image, int rotationAngle, float x, float y)
        {
            //Save the current graphics state
            PdfGraphicsState state = page.Graphics.Save();
            page.Graphics.TranslateTransform(x, y);
            //Translate the coordinate system to the center of image
            page.Graphics.TranslateTransform(image.GetBounds().Width / 2, image.GetBounds().Height / 2);
            //Rotate the coordinate system
            page.Graphics.RotateTransform(-rotationAngle);
            page.Graphics.TranslateTransform(-image.GetBounds().Width / 2, -image.GetBounds().Height / 2);
            // Draw an image
            page.Graphics.DrawImage(image, 0, 0);
            //Restore the graphics state
            page.Graphics.Restore(state);
        }

 

VB.NET

       'Create a document
        Dim doc As PdfDocument = New PdfDocument()
        Dim files As String() = Directory.GetFiles(Path.Combine("../../Images/"))
        'Add a new page
        Dim page As PdfPage = doc.Pages.Add()
        Dim rotationAngle As Integer = 270
        Dim x As Single = 40, y As Single = 400
 
        For i As Integer = 0 To files.Length - 1
            'Load an image
            Dim image As PdfBitmap = New PdfBitmap(files(i))
            DrawRotateImage(page, image, rotationAngle, x, y)
            'provide the angle and x and y position based on your requirement. 
            rotationAngle += 40
            x += 300
            y += 100
        Next
 
        Dim outputFileName As String = rotationAngle.ToString() & ".pdf"
        'Save the PDF
        doc.Save(outputFileName)
        'Close the PDF 
        doc.Close(True)
 
'Calculate the translate transform
Private Shared Sub DrawRotateImage(ByVal page As PdfPage, ByVal image As PdfBitmap, ByVal rotationAngle As Integer, ByVal x As Single, ByVal y As Single)
        'Save the current graphics state
        Dim state As PdfGraphicsState = page.Graphics.Save()
        page.Graphics.TranslateTransform(x, y)
        'Translate the coordinate system to the center of image
        page.Graphics.TranslateTransform(image.GetBounds().Width / 2, image.GetBounds().Height / 2)
        'Rotate the coordinate system
        page.Graphics.RotateTransform(-rotationAngle)
        page.Graphics.TranslateTransform(-image.GetBounds().Width / 2, -image.GetBounds().Height / 2)
        'Draw an image
        page.Graphics.DrawImage(image, 0, 0)
        'Restore the graphics state
        page.Graphics.Restore(state)
    End Sub

 

A complete working sample can be downloaded from the rotate image in all positions in the pdf document sample.

By executing the program, you will get the PDF document as follows.

Output pdf document

 

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

https://support.syncfusion.com/kb/article/8533/how-to-rotate-images-in-a-pdf-using-c-and-vb-net

https://help.syncfusion.com/file-formats/pdf/working-with-images

https://support.syncfusion.com/kb/article/8684/how-to-avoid-auto-rotation-of-images-in-pdf-using-c-and-vb-net

https://support.syncfusion.com/kb/article/8067/how-to-rotate-pages-in-winforms-pdf-file

https://support.syncfusion.com/kb/article/8575/how-to-rotate-a-pdf-document-based-on-angle-using-c-and-vb-net

 

Conclusion

I hope you enjoyed learning about how to rotate an image around its centre in PDF document using C#.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.

For current customers, you can check out our 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 other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied