We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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
<o:p></o:p>

Aspose.Pdf.Document doc = new Document();

// add page to pages collection of document instance

doc.Pages.Add();

Aspose.Pdf.Image image = new Aspose.Pdf.Image();

// create Bitmap image so we can get image information

Bitmap bitmap = new Bitmap("c:/pdftest/-logo.png");

image.File = "c:/pdftest/logo.png";

// add image to pagaraphs 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 widht of first page equal to image width

doc.Pages[1].PageInfo.Width = bitmap.Width;

// trip 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 PDF page containing image. Please take a look over following code snippet. Please note that I have saved the file to MemoryStream instance and then I have created another Document object because when placing image inside PDF which occupies complete page area, the page rotation is not producing correct results.

[C#]

// Create Document instance<o:p></o:p>

Aspose.Pdf.Document doc = new Document();

// add page to pages collection of document instance

doc.Pages.Add();

Aspose.Pdf.Image image = new Aspose.Pdf.Image();

// create Bitmap image so we can get image information

Bitmap bitmap = new Bitmap("c:/pdftest/PDF_A_1b_Compliance-Test.PNG");

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

// add image to pagaraphs 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 widht of first page equal to image width

doc.Pages[1].PageInfo.Width = bitmap.Width;

// trip 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;

MemoryStream ms = new MemoryStream();

// save the PDF document

doc.Save(ms);

Aspose.Pdf.Document temp_doc = new Document(ms);

temp_doc.Pages[1].Rotate = Rotation.on90;

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

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.