How to avoid auto rotation of images in 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 avoid auto rotation of images while drawing in it to the PDF using C# and VB.NET.
Reason for auto rotation of images: Some of the bitmap objects do not consider the EXIF orientation tag. The EXIF (Exchangeable Image File Format) standard specifies some set of tags that can be embedded in images. One of these tags specifies the orientation of the image. If it does not take into the account, the image gets rotated. This behavior will be reproduced even in System.Drawing.Bitmap.
Steps to avoid auto rotation of images in PDF programmatically:
- Create a new 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 Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports System.Drawing
- Use the following code snippet to draw image in the PDF document.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a page to the document PdfPage page = document.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Get the image from local disk Image baseImage = Bitmap.FromFile("image.jpg"); //Set image orientation to fix FixImageOrientation(baseImage); //Save the modified image MemoryStream stream = new MemoryStream(); baseImage.Save(stream, ImageFormat.Jpeg); //Load the image from stream PdfImage image = new PdfBitmap(stream); //Draw the image graphics.DrawImage(image, 0, 0, 200, 300); //Save the document document.Save("Sample.pdf"); //Close the document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("Sample.pdf");
VB.NET
'Create a new PDF document Dim document As New PdfDocument() 'Add a page to the document Dim page As PdfPage = document.Pages.Add() 'Create PDF graphics for the page Dim graphics As PdfGraphics = page.Graphics 'Get the image from local disk Dim baseImage As Image = Bitmap.FromFile("image.jpg") 'Set image orientation to fix FixImageOrientation(baseImage) 'Save the modified image Dim stream As New MemoryStream() baseImage.Save(stream, ImageFormat.Jpeg) 'Load the image from stream Dim image As PdfImage = New PdfBitmap(stream) 'Draw the image graphics.DrawImage(image, 0, 0, 200, 300) 'Save the document document.Save("Sample.pdf") 'Close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("Sample.pdf")
- Use the following code to set image orientation to avoid auto rotation of images in a PDF document.
C#
private Image FixImageOrientation(Image image) { const int exifOrientationId = 0x112; if (!image.PropertyIdList.Contains(exifOrientationId)) return image; //Gets the specified property item from the image var property = image.GetPropertyItem(exifOrientationId); var orient = BitConverter.ToInt16(property.Value, 0); //Get the rotated or flipped image image = RotateImageSrc(orient, image); return image; } private Image RotateImageSrc(int orient, Image image) { switch (orient) { case 1: image.RotateFlip(RotateFlipType.RotateNoneFlipNone); return image; case 2: image.RotateFlip(RotateFlipType.RotateNoneFlipX); return image; case 3: image.RotateFlip(RotateFlipType.Rotate180FlipNone); return image; case 4: image.RotateFlip(RotateFlipType.Rotate180FlipX); return image; case 5: image.RotateFlip(RotateFlipType.Rotate90FlipX); return image; case 6: image.RotateFlip(RotateFlipType.Rotate90FlipNone); return image; case 7: image.RotateFlip(RotateFlipType.Rotate270FlipX); return image; case 8: image.RotateFlip(RotateFlipType.Rotate270FlipNone); return image; default: image.RotateFlip(RotateFlipType.RotateNoneFlipNone); return image; } }
VB.NET
Private Function FixImageOrientation(image As Image) As Image Const exifOrientationId As Integer = &H112 If Not image.PropertyIdList.Contains(exifOrientationId) Then Return image End If 'Gets the specified property item from the image Dim [property] = image.GetPropertyItem(exifOrientationId) Dim orient = BitConverter.ToInt16([property].Value, 0) 'Get the rotated or flipped image image = RotateImageSrc(orient, image) Return image End Function Private Function RotateImageSrc(orient As Integer, image As Image) As Image Select Case orient Case 1 image.RotateFlip(RotateFlipType.RotateNoneFlipNone) Return image Case 2 image.RotateFlip(RotateFlipType.RotateNoneFlipX) Return image Case 3 image.RotateFlip(RotateFlipType.Rotate180FlipNone) Return image Case 4 image.RotateFlip(RotateFlipType.Rotate180FlipX) Return image Case 5 image.RotateFlip(RotateFlipType.Rotate90FlipX) Return image Case 6 image.RotateFlip(RotateFlipType.Rotate90FlipNone) Return image Case 7 image.RotateFlip(RotateFlipType.Rotate270FlipX) Return image Case 8 image.RotateFlip(RotateFlipType.Rotate270FlipNone) Return image Case Else image.RotateFlip(RotateFlipType.RotateNoneFlipNone) Return image End Select End Function
A complete working sample can be downloaded from ImageSample.zip.
Find the following output document screenshots for before and after handling the auto rotation of images.
Sample 1:
Sample 2:
Take a moment to peruse the documentation, where you will find options like inserting images in a new and existing PDF document, inserting vector images, image masking, replacing images, image pagination, and rotating an image with code examples.
Refer here to explore the rich set of Essential Studio 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.