Hello,
a technical question.
We convert pictures, including many pictures taken with mobile phones, to PDF. Is there a way to take into account the rotation of the mobile phone picture when converting to PDF?
Kind Regards,
Andy
Hello,
a technical question.
We convert pictures, including many pictures taken with mobile phones, to PDF. Is there a way to take into account the rotation of the mobile phone picture when converting to PDF?
Kind Regards,
Andy
@AStelzner
Could you provide examples of normal and rotated photos so we could test this scenario in our environment?
Here is an image where the orientation/rotation is set in the EXIF Data.
If we convert the Image to PDF, the image rotation is wrong.
In most of the image viewers the rotation will be considered correctly.
Image.jpg (124,1 KB)
@AStelzner
Could you share how you convert image to PDF?
For now I tried instruction from here Add Image to PDF using C#|Aspose.PDF for .NET
and in result file image seems to be placed correctly
The code was following
int lowerLeftX = 200;
int lowerLeftY = 200;
int upperRightX = 800;
int upperRightY = 800;
Document doc = new Document();
Page pg = doc.Pages.Add();
FileStream imageStream = new FileStream(input, FileMode.Open);
pg.Resources.Images.Add(imageStream);
pg.Contents.Add(new Aspose.Pdf.Operators.GSave());
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
pg.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
XImage ximage = pg.Resources.Images[pg.Resources.Images.Count];
pg.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
pg.Contents.Add(new Aspose.Pdf.Operators.GRestore());
doc.Save(output);
image_rotation.pdf (125.6 KB)
My (shortened) code to convert an image to PDf:
public void ConvertToPdf(string outputFile, CancellationTokenSource cancellationTokenSource, Action customPageSavingCallback)
{
using (var image = Image.Load(InputFile, new LoadOptions()))
{
using (var exportOptions = new PdfOptions())
{
exportOptions.PdfDocumentInfo = new PdfDocumentInfo();
exportOptions.UseOriginalImageResolution = false;
image.Save(outputFile, exportOptions);
}
}
}
And we have to shrink the image proportionally if it’s bigger than A4. That’s why the code is shortened
And progress wouldn’t be bad either.
@AStelzner
Judging by your example, your question is related to Aspose.Imaging project, not Aspose.Pdf
do I understand that correctly?
If that’s the case then probably it’s better to ask it here Aspose.Imaging Product Family - Free Support Forum - aspose.com
I can try to investigate how it’s done via Aspose.Pdf but it doesn’t seem to be a good idea since you’re trying to resolve your problem with another project
Ok, can you move the ticket to Aspose.Imaging Product Family - Free Support Forum - aspose.com?
Or do I have to rewrite it?
Kind Regards,
Andy
@AStelzner We will look into your issue and aim to provide a comprehensive response as soon as possible. Thank you for your patience.
@AStelzner We appreciate your patience as we looked into your issue. We have conducted tests with your code and the sample image provided. Our tests indicate that the image seems to be exporting with the correct rotation, as it appears when opened by applications such as IrfanView, XnView, and others. We’re attaching the resulting PDF file of this exported image for your reference.
Could you please provide us with additional details about the environment in which you’re encountering this issue, as well as the Aspose.Imaging version you are using?
We look forward to your response in order to assist you better.
Image.jpg.pdf (282.9 KB)
I am facing a weird issue. I accept a doc from users and then split that doc into UI elements ( images ) ask users to put text box and image box ( for signature ) and then internally add those fields into the pdf pages by doing coordinates matching and placing using aspose java 20.4 lib . I am using java 8.
the problem is at times i am getting the image inverted and miss aligned to where it should have been.
what could be the problem here.
Hi Alexey,
Ok, that’s weird.
We used the latest Aspose.Imaging version 25.12.0. Our application runs on Windows 11.
Attached are all input images. All of them, except for the file Bug_16435_rightTop.jpg, are displayed correctly in the PDF. See the attachment ExportPdf.pdf.
InputImages.zip (7,8 MB)
PDF: https://dfxspace.digitalfabrix.de/public/download-shares/ZQKLmR838bmmvDdQPoPWsgamwLbJDtWl
Here is our code again for converting from image to PDF:
public void ConvertToPdf(string outputFile,
CancellationTokenSource cancellationTokenSource,
Action<int> customPageSavingCallback)
{
LogConvertToPdfStart(outputFile);
try
{
var imagingToPdfProgressEventHandler =
new ImagingToPdfProgressEventHandler(customPageSavingCallback, cancellationTokenSource);
cancellationTokenSource?.Token.Register(() => _asposeInterruptMonitor.Interrupt());
RemoveAttributesFromFile(InputFile, FileAttributes.ReadOnly);
float horizontalResolutionFromImage;
float verticalResolutionFromImage;
using (var inputImage = System.Drawing.Image.FromFile(InputFile))
{
verticalResolutionFromImage = inputImage.VerticalResolution;
horizontalResolutionFromImage = inputImage.HorizontalResolution;
}
using (var image = Image.Load(InputFile, new LoadOptions()))
{
using (var exportOptions = new PdfOptions())
{
exportOptions.PdfDocumentInfo = new PdfDocumentInfo();
exportOptions.ProgressEventHandler = imagingToPdfProgressEventHandler.ExportProgressCallback;
exportOptions.ResolutionSettings = new ResolutionSetting(horizontalResolutionFromImage, verticalResolutionFromImage);
exportOptions.UseOriginalImageResolution = true;
image.InterruptMonitor = _asposeInterruptMonitor;
image.Save(outputFile, exportOptions);
}
}
}
catch (Exception exception)
{
LogConvertToPdfError(exception);
throw;
}
}
Kind Regards,
Andy
In some cases, the image may have a mark (Exif/Xmp) indicating that it needs to be rotated. Imaging does not make it automatically, but operates with the image as it really is. In order to bring the image to the expected appearance, you need to call the RasterImage.AutoRotate() method before exporting.
Here is a correct code
public void ConvertToPdf(string outputFile,
CancellationTokenSource cancellationTokenSource,
Action<int> customPageSavingCallback)
{
LogConvertToPdfStart(outputFile);
try
{
var imagingToPdfProgressEventHandler =
new ImagingToPdfProgressEventHandler(customPageSavingCallback, cancellationTokenSource);
cancellationTokenSource?.Token.Register(() => _asposeInterruptMonitor.Interrupt());
RemoveAttributesFromFile(InputFile, FileAttributes.ReadOnly);
double horizontalResolutionFromImage = 72;
double verticalResolutionFromImage = 72;
using (var image = Image.Load(InputFile, new LoadOptions()))
{
if (image is RasterImage rasterImage)
{
horizontalResolutionFromImage = rasterImage.HorizontalResolution;
verticalResolutionFromImage = rasterImage.VerticalResolution;
// Here we check the "logical" rotation and if it is need we perform rotation.
rasterImage.autoRotate();
}
using (var exportOptions = new PdfOptions())
{
exportOptions.PdfDocumentInfo = new PdfDocumentInfo();
exportOptions.ProgressEventHandler = imagingToPdfProgressEventHandler.ExportProgressCallback;
exportOptions.ResolutionSettings = new ResolutionSetting(horizontalResolutionFromImage, verticalResolutionFromImage);
exportOptions.UseOriginalImageResolution = true;
image.InterruptMonitor = _asposeInterruptMonitor;
image.Save(outputFile, exportOptions);
}
}
}
catch (Exception exception)
{
LogConvertToPdfError(exception);
throw;
}
}
OK, thanks
But:
The file Bug_16435_rightTop.jpg is now rotated correctly, but all the other images remain as they are, even though the Exif data contains rotations.
And two files even lead to an exception (Landscape_Keine.jpg and Portrait_0_Keine.jpg):
Parametername: orientation
bei #=z8LydfaGLt6GDIKS9o$iy0dhlpiHor6mFAA==.#=zZ5ludt0fRRsa(ExifOrientation #=z4PfeaKI=)
bei Aspose.Imaging.RasterImage.AutoRotate()
bei Digitalfabrix.ThirdParty.FileFormat.Aspose.Imaging.Conversion.AsposeImagingConversionHandling.ConvertToPdf(String outputFile, CancellationTokenSource cancellationTokenSource, Action1 customPageSavingCallback)"{"EnvironmentUserName":"AzureAD\\StelznerAndreas"} 2025-12-11 07:54:25.055 [Error] [PdfGenerator] [ThreadId: 4] [ProcessId: 44496] Die Datei 'C:\Users\StelznerAndreas\AppData\Local\Temp\komX\DownloadedFiles\kytp23zs.nff\Landscape_Keine.jpg' hat folgenden Fehler verursacht: Funktion 'ConvertToPdf' fĂĽr Input Datei 'C:\Users\StelznerAndreas\AppData\Local\Temp\komX\DownloadedFiles\kytp23zs.nff\Landscape_Keine.jpg' schlug fehl.{"EnvironmentUserName":"AzureAD\\StelznerAndreas","ExceptionDetail":{"Type":"System.Exception","HResult":-2146233088,"Message":"Funktion 'ConvertToPdf' fĂĽr Input Datei 'C:\\Users\\StelznerAndreas\\AppData\\Local\\Temp\\komX\\DownloadedFiles\\kytp23zs.nff\\Landscape_Keine.jpg' schlug fehl.","Source":"Digitalfabrix.FileFormat","InnerException":{"Type":"System.ArgumentOutOfRangeException","HResult":-2146233086,"Message":"Unsupported EXIF orientation value\r\nParametername: orientation","Source":"Aspose.Imaging","ParamName":"orientation","ActualValue":null}}} System.Exception: Funktion 'ConvertToPdf' fĂĽr Input Datei 'C:\Users\StelznerAndreas\AppData\Local\Temp\komX\DownloadedFiles\kytp23zs.nff\Landscape_Keine.jpg' schlug fehl. ---> System.ArgumentOutOfRangeException: Unsupported EXIF orientation value Parametername: orientation bei #=z8LydfaGLt6GDIKS9o$iy0dhlpiHor6mFAA==.#=zZ5ludt0fRRsa(ExifOrientation #=z4PfeaKI=) bei Aspose.Imaging.RasterImage.AutoRotate() bei Digitalfabrix.ThirdParty.FileFormat.Aspose.Imaging.Conversion.AsposeImagingConversionHandling.ConvertToPdf(String outputFile, CancellationTokenSource cancellationTokenSource, Action1 customPageSavingCallback)
bei System.Threading.Tasks.Task.Execute()
— Ende der internen Ausnahmestapelüberwachung —
bei Digitalfabrix.FileFormat.Image.Conversion.ImageConversionHandling.ConvertToPdf(String outputFile, String hyphenationDictionaryPath)
bei LivingData.komX.Pdf.PdfGenerator.Commands.PdfConversion.ImageToPdf.ConvertImageToPdfCommand.d__1.MoveNext() in C:\dev\komXwork\Dev\Infrastructure\InternalKomXReferences\LivingData.komX.Pdf\LivingData.komX.Pdf.PdfGenerator\Commands\PdfConversion\ImageToPdf\ConvertImageToPdfCommand.cs:Zeile 31.
Kind Regards,
Andy
Hmm. It is a weird situation… Let me check it.
@AStelzner
There is a bug. I have created a new task.
You can avoid this using the following workaround:
public void ConvertToPdf(string outputFile,
CancellationTokenSource cancellationTokenSource,
Action<int> customPageSavingCallback)
{
LogConvertToPdfStart(outputFile);
try
{
var imagingToPdfProgressEventHandler =
new ImagingToPdfProgressEventHandler(customPageSavingCallback, cancellationTokenSource);
cancellationTokenSource?.Token.Register(() => _asposeInterruptMonitor.Interrupt());
RemoveAttributesFromFile(InputFile, FileAttributes.ReadOnly);
double horizontalResolutionFromImage = 72;
double verticalResolutionFromImage = 72;
using (var image = Image.Load(InputFile, new LoadOptions()))
{
if (image is RasterImage rasterImage)
{
horizontalResolutionFromImage = rasterImage.HorizontalResolution;
verticalResolutionFromImage = rasterImage.VerticalResolution;
// Here we check the "logical" rotation and if it is need we perform rotation.
var orient = rasterImage.ExifData?.Orientation ?? 0;
if (orient != 0)
{
rasterImage.autoRotate();
}
}
using (var exportOptions = new PdfOptions())
{
exportOptions.PdfDocumentInfo = new PdfDocumentInfo();
exportOptions.ProgressEventHandler = imagingToPdfProgressEventHandler.ExportProgressCallback;
exportOptions.ResolutionSettings = new ResolutionSetting(horizontalResolutionFromImage, verticalResolutionFromImage);
exportOptions.UseOriginalImageResolution = true;
image.InterruptMonitor = _asposeInterruptMonitor;
image.Save(outputFile, exportOptions);
}
}
}
catch (Exception exception)
{
LogConvertToPdfError(exception);
throw;
}
}
@AStelzner
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): IMAGINGNET-7901
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Ok, thanks!
One more question. Will AutoRotate then also work for the delivered images (which currently leads to the exception) if the issue is fixed?
Because with various tools it is clear that valid Exif data is present?!
Regards,
Andy