Best practice to consider image alignment when converting image to PDF

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);
    }
}

}

@AStelzner
Thanks, I’ll try to compare results and see how they can be combined

And we have to shrink the image proportionally if it’s bigger than A4. That’s why the code is shortened :slight_smile: 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

1 Like

@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)