Ok I am “converting” attched JPG to a PDF, and I am running into some issues
pic.jpeg (1.0 MB)
The following is the code I am using to do the conversion:
GetImageLicence()
GetPDFLicence()
Dim oPDF As Aspose.Pdf.Document = Nothing
Dim oImage As Aspose.Imaging.Image = Nothing
Dim nPageHeight As Double
Dim nPageWidth As Double
Dim oPDFImage As Aspose.Pdf.Image = Nothing
Dim oStream As System.IO.MemoryStream = Nothing
Try
Try
oPDF = New Aspose.Pdf.Document()
Catch ex As Exception
'oResult.Data = ex.Message
'oResult.Response = ConverterResponse.unknownerror
'Return oResult
End Try
'oResult.Action = "Load Image"
Try
oImage = Aspose.Imaging.Image.Load(sFile)
'START of fudge
Dim oBMPOptions As Aspose.Imaging.ImageOptionsBase = Nothing
Try
Dim sTempFile As String = System.IO.Path.Combine(AsposeTemp, "tempbmp.bmp")
If System.IO.File.Exists(sTempFile) Then
System.IO.File.Delete(sTempFile)
End If
oBMPOptions = New Aspose.Imaging.ImageOptions.BmpOptions
oImage.Save(filePath:=sTempFile, options:=oBMPOptions)
Try
oImage.Dispose()
Catch
End Try
oImage = Nothing
oImage = Aspose.Imaging.Image.Load(filePath:=sTempFile)
Catch ex As Exception
End Try
'END of fudge
Catch ex As Exception
'oResult.Data = ex.Message
'oResult.Response = ConverterResponse.unknownerror
'Return oResult
End Try
'oResult.Action = "Add Page for Image"
Try
oPDF.Pages.Add()
Catch ex As Exception
'oResult.Data = ex.Message
'oResult.Response = ConverterResponse.unknownerror
'Return oResult
End Try
'oResult.Action = "Check image height and width and rotate image if necessary"
Dim dProportion As Double = 1
Try
nPageHeight = oPDF.Pages.Item(1).PageInfo.Height - oPDF.Pages.Item(1).PageInfo.Margin.Top - oPDF.Pages.Item(1).PageInfo.Margin.Bottom
nPageWidth = oPDF.Pages.Item(1).PageInfo.Width - oPDF.Pages.Item(1).PageInfo.Margin.Left - oPDF.Pages.Item(1).PageInfo.Margin.Right
If CDbl(oImage.Width) > oImage.Height Then
If (CDbl(oImage.Width) <= nPageWidth) And (CDbl(oImage.Height) <= nPageHeight) Then
'no need to do anything. Image will fit fine "as is"
Else
oImage.RotateFlip(Aspose.Imaging.RotateFlipType.Rotate90FlipNone)
End If
End If
If nPageHeight < CDbl(oImage.Height) Then
dProportion = nPageHeight / CDbl(oImage.Height)
End If
If nPageWidth < CDbl(oImage.Width) Then
If nPageWidth / CDbl(oImage.Width) < dProportion Then
dProportion = nPageWidth / CDbl(oImage.Width)
End If
End If
Catch ex As Exception
'oResult.Data = ex.Message
'oResult.Response = ConverterResponse.unknownerror
'Return oResult
End Try
'oResult.Action = "Save Image to Memory Stream"
Try
oStream = New IO.MemoryStream
oImage.Save(oStream)
Try
oImage.Dispose()
Catch
End Try
Catch ex As Exception
'oResult.Data = ex.Message
'oResult.Response = ConverterResponse.unknownerror
'Return oResult
End Try
'oResult.Action = "Add Image to PDF "
Try
oPDFImage = New Aspose.Pdf.Image
oPDFImage.ImageStream = oStream
If dProportion < 1 Then
oPDFImage.ImageScale = dProportion
End If
oPDF.Pages.Item(1).Paragraphs.Add(oPDFImage)
oPDF.Save(sToFileName)
Catch ex As Exception
'oResult.Data = ex.Message
'oResult.Response = ConverterResponse.unknownerror
'Return oResult
End Try
The problem is that there appears to be some information held internally in the JPG to indicate that the image has been rotated. When I look at the folder containing the image and view the folder content as, for example, “Extra large icons” I see the picture in its correct orientation, as the person took it on their phone. But when I open the image, for example in “Paint Shop Pro”, I see the image rotated 90 degrees to the left.
Look at my code. Between the comments 'START of fudge and 'END of fudge I added some code in which I take the original .jpeg and save it as a .bmp before re-opening it in a new Aspose.Imaging.Image. Once I do that the “rotation” issue is gone and the image loads 90 degrees to the left.
My first question is:
can I do this without this “fudge” ?
Secondly:
The image is larger than A4 size. The PDF page is set to be A4 (portrait) and I try to fit the image into the available space. In order to do this I rotate the image if its width is more than its height, and then I set the imagescale to a proportion that will ensure that it fits. All this is working correctly thanks to the bit of code:
If dProportion < 1 Then
oPDFImage.ImageScale = dProportion
End If
BUT
Originally I did not set the imagescale. And I note that in the resulting PDF the image is made to fit the page. However, the image is resized disproportionally to fit the maximum available width as well as the maximum available height and as a result the image appears “stretched” or “squeezed”. Second question:
Is is possible to get the image resized without affecting its width to height ratio, and without having to programmatically calculate its ImageScale?
My second question is not as urgent as my first, as my code works perfectly fine to suit our needs. But I don’t like having to save the image to a Bitmap and to re-read it from there as per the code within the 'FUDGE comments. I would really hope that there is a better solution to my first question.