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

Free Support Forum - aspose.com

When add JPEG image file into the new PDF file, how to Set up the page resolution? or resize it?

Hi,

My code to add image file into the new PDF file, but it seems too large size for the PDF file. is anyway to set the resolution in the pdf page or compress it.

Here is the same code, however, I don’t know how to set the resolution or quality:


            int lowerLeftX = 10;
            int lowerLeftY = 10;
            int upperRightX = 550;
            int upperRightY = 800;
            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 });
            page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
            XImage ximage = page.Resources.Images[page.Resources.Images.Count];
            page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
            page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

@louis168168

You may please try using the below code snippet and let us know in case you face any issue:
[C#]

var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();

FileStream stream = new FileStream("sample.jpg", FileMode.Open);
System.Drawing.Image img = new Bitmap(stream);
img = ScaleImage(img, (int)pdfImageSection.PageInfo.Width, (int)pdfImageSection.PageInfo.Height);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
var image = new Aspose.Pdf.Image { ImageStream = ms };
pdfImageSection.Paragraphs.Add(image);
pdf.Save(dataDir + "Image2Pdf_out.pdf");

Definition of ScaleImage() method:

public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
 var ratioX = (double)maxWidth / image.Width;
 var ratioY = (double)maxHeight / image.Height;
 var ratio = Math.Min(ratioX, ratioY);
 var newWidth = (int)(image.Width * ratio);
 var newHeight = (int)(image.Height * ratio);
 var newImage = new Bitmap(newWidth, newHeight);
 using (var graphics = Graphics.FromImage(newImage))
  graphics.DrawImage(image, 0, 0, newWidth, newHeight);
 return newImage;
}