Issue converting JPG to PDF (stretching image)

I’m using Aspose.Pdf for .Net and I’m having an issue with getting the image to be sized correctly for the pdf. I want to take a jpg and convert it to an exact same sized pdf file.


What happens is that if the image is landscape then the pdf file will be portrait and stretch the image.

All of the images we’ve been testing with have been taken on a cell phone.

My code seems to work fine for an image of 640x480 but not for an image of 1632x1224 also 4160x2340 doesn’t work.

This could be because the 640x480 is so small it almost looks square.

Here is the code we have. I’m also not sure about the Aspose.Pdf.Rectangle because sometimes the lower left corner is actually where the upper right corner is.

public byte[] ConvertJpgToPdf(byte[] jpgBytes)
{
PdfDocument pdf = new PdfDocument();
var page =_pageUtilities.InitializePage(pdf);
MemoryStream mystream = new MemoryStream(jpgBytes);
Bitmap bitmap = new Bitmap(mystream);

if (bitmap.Width > bitmap.Height)
{
//Landscape
page.CropBox = new Aspose.Pdf.Rectangle(bitmap.Width, bitmap.Height, 0 ,0);
}
else
{
//Portrait
page.CropBox = new Aspose.Pdf.Rectangle(0, 0, bitmap.Width, bitmap.Height);
}
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
page.Paragraphs.Add(image);
image.ImageStream = mystream;

byte[] pdfBytes;

MemoryStream memoryStream = new MemoryStream();
pdf.Save(memoryStream, PdfSaveFormat.Pdf);
pdfBytes = memoryStream.ToArray();
mystream.Close();

return pdfBytes;
}

Any advice would be much appreciated.

Hi Connor,

Thanks for contacting support. Please try using following code snippet to accomplish your requirement. In case you encounter any issue, please share your sample image files.

[C#]

// Create Document instance
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();

// Add page to pages collection of document instance
doc.Pages.Add();

// Create Bitmap image so we can to get image information
Bitmap bitmap = new Bitmap("c:/pdftest/-logo.png");

Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = "c:/pdftest/logo.png";

// Add image to paragraphs collection of first page
doc.Pages[1].Paragraphs.Add(image);

// Set height of first page equal to image height
doc.Pages[1].PageInfo.Height = bitmap.Height;

// Set width of first page equal to image width
doc.Pages[1].PageInfo.Width = bitmap.Width;

// Trim margin around all corners of page
doc.Pages[1].PageInfo.Margin.Left = 1;
doc.Pages[1].PageInfo.Margin.Top = 1;
doc.Pages[1].PageInfo.Margin.Bottom = 1;
doc.Pages[1].PageInfo.Margin.Right = 1;

// Save the PDF document
doc.Save("c:/pdftest/Image_to_PDF.pdf");

Thank you this seemed to fix the issue. Also I’m curious if there is a way to rotate the pdf image to portrait? Wondering specifically about Aspose.PDF.

Hi Connor,

In order to accomplish this requirement, you need to rotate a PDF page containing an image. Please take a look at the following code snippet. Note that I have saved the file to a MemoryStream instance and then created another Document object because when placing an image inside PDF that occupies the complete page area, the page rotation is not producing correct results.

[C#]

// Create Document instance
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();

// add page to pages collection of document instance
doc.Pages.Add();

// Create Image object
Aspose.Pdf.Image image = new Aspose.Pdf.Image();

// create Bitmap image so we can get image information
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap("c:/pdftest/PDF_A_1b_Compliance-Test.PNG");

image.File = "c:/pdftest/PDF_A_1b_Compliance-Test.PNG";

// add image to paragraphs collection of the first page
doc.Pages[1].Paragraphs.Add(image);

// set height of the first page equal to image height
doc.Pages[1].PageInfo.Height = bitmap.Height;

// set width of the first page equal to image width
doc.Pages[1].PageInfo.Width = bitmap.Width;

// trim margin around all corners of page
doc.Pages[1].PageInfo.Margin.Left = 1;
doc.Pages[1].PageInfo.Margin.Top = 1;
doc.Pages[1].PageInfo.Margin.Bottom = 1;
doc.Pages[1].PageInfo.Margin.Right = 1;

// Create a MemoryStream to save the PDF document
System.IO.MemoryStream ms = new System.IO.MemoryStream();

// save the PDF document
doc.Save(ms);

// Create a new Document object from the MemoryStream
Aspose.Pdf.Document temp_doc = new Aspose.Pdf.Document(ms);

// Rotate the first page by 90 degrees
temp_doc.Pages[1].Rotate = Aspose.Pdf.Helpers.Rotation.on90;

// Save the rotated document
temp_doc.Save("c:/pdftest/Image_to_PDF.pdf");

// Close the MemoryStream
ms.Close();

Is there a way to check to see which orientation it is in?


We would only like to rotate if it is in landscape and also want to make sure we don’t flip the image upside down.



connork:
Is there a way to check to see which orientation it is in?

We would only like to rotate if it is in landscape and also want to make sure we don’t flip the image upside down.
Hi Connor,

Thanks for sharing the details.

In order to determine the image orientation, you may follow the instructions specified over Get the Resolution and Dimensions of Embedded Images. If the Width of image being returned is greater than Image Height, the image is in Landscape orientation and vice versa.