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

Free Support Forum - aspose.com

Blurry pdf when convering an image

Hi,

I’m trying to convert .jpg and .tif files to pdf. When I do the image in the pdf is very blurry. Is there a way to keep the dimensions and still fit on the page? Or maybe if it’s too big for the page then switch to landscape? Here this the code I’m using:

public byte[] convertImagetoPDF(Stream imageToConvertStream)

{

//set Aspose license

Aspose.Pdf.License license = new Aspose.Pdf.License();

license.SetLicense(“Aspose.Total.lic”);

//license.Embedded = true;

// Instantiate Document Object

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

// Add a page to pages collection of document

Aspose.Pdf.Page page = doc.Pages.Add();

// Load the source image file to Stream object

//FileStream fs = new FileStream(“c:/Projects/test.jpg”, FileMode.Open, FileAccess.Read);

imageToConvertStream.Position = 0;

byte[] tmpBytes = new byte[imageToConvertStream.Length];

imageToConvertStream.Read(tmpBytes, 0, Convert.ToInt32(imageToConvertStream.Length));

MemoryStream myStream = new MemoryStream(tmpBytes);

// Instantiate BitMap object with loaded image stream

Bitmap b = new Bitmap(myStream);

// Set margins so image will fit, etc.

page.PageInfo.Margin.Bottom = 0.5;

page.PageInfo.Margin.Top = 0.5;

page.PageInfo.Margin.Left = 0.5;

page.PageInfo.Margin.Right = 0.5;

page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);

// Create an image object

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

// Add the image into paragraphs collection of the section

page.Paragraphs.Add(image1);

image1.FixWidth = page.PageInfo.Width - page.PageInfo.Margin.Left - page.PageInfo.Margin.Right;

// specify the image Height information equal to page Height

image1.FixHeight = page.PageInfo.Height - page.PageInfo.Margin.Top - page.PageInfo.Margin.Bottom;

// Set the image file stream

image1.ImageStream = myStream;

// Save resultant PDF file

//doc.Save(“c:/Projects/Image2PDF_DOM.pdf”);

//doc.Save(image1.ImageStream);

// Create a new memory stream.

MemoryStream outStream = new MemoryStream();

doc.Save(outStream);
// Close memoryStream object

myStream.Close();

outStream.Close();

// Convert the document to byte form.

byte[] docBytes = outStream.ToArray();

//Console.WriteLine(docBytes.Length);

outStream.Close();

return docBytes;
}

Hi Krista,

Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 11.2.0 and I am unable to notice any image blur issue in resultant file. Furthermore, the dimensions of input image are so big that when transforming it to PDF format, the size of output file is not according to image dimensions.

For your reference, I have also attached the output file generated over my end. We are sorry for this inconvenience.

[C#]

// Instantiate Document Object <o:p></o:p>

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

// Add a page to pages collection of document

Aspose.Pdf.Page page = doc.Pages.Add();

// Instantiate BitMap object with loaded image stream

Bitmap b = new Bitmap("c:/pdftest/IMG_1344.jpg");

// Set margins so image will fit, etc.

page.PageInfo.Margin.Bottom = 0.5;

page.PageInfo.Margin.Top = 0.5;

page.PageInfo.Margin.Left = 0.5;

page.PageInfo.Margin.Right = 0.5;

page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);

// Create an image object

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

// Add the image into paragraphs collection of the section

page.Paragraphs.Add(image1);

Console.WriteLine("Input Image Width = " + b.Width);

Console.WriteLine("Input Image Height = " + b.Height);

image1.FixWidth = b.Width- page.PageInfo.Margin.Left - page.PageInfo.Margin.Right;

// specify the image Height information equal to page Height

image1.FixHeight = b.Height - page.PageInfo.Margin.Top - page.PageInfo.Margin.Bottom;

Console.WriteLine("Image updated Width = " + image1.FixWidth);

Console.WriteLine("Image updated Height = " + image1.FixHeight);

// Set the image file stream

image1.File = "c:/pdftest/IMG_1344.jpg";

doc.Save(“c:/pdftest/ImageToPDF.pdf”);