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

Free Support Forum - aspose.com

Set DPI for Images inside the PDF using C#

Hi we are using Aspose.Pdf for creating a pdf. The input image resolution is 300 DPI. But after creating the PDF, the DPI becomes 96. Is there any way to set the DPI as 300.
We did not find any property for setting resolution in Aspose.Pdf.Generator.Pdf , Aspose.Pdf.Generator.Section , Aspose.Pdf.Generator.Image
Below is the code snippet we are using:

        string tempLocalPDFPath = System.IO.Path.GetTempFileName();
        tempLocalPDFPath = tempLocalPDFPath.Replace(".tmp", ".pdf");
        MemoryStream mystream = null;
        try
        {
            Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();               
            foreach (byte[] image in ImageArray)
            {   
                try
                {                                      
                    Aspose.Pdf.Generator.Section sect1 = new 
                    Aspose.Pdf.Generator.Section(pdf1);
                    mystream = new MemoryStream(image);
                    Bitmap bitmap = new Bitmap(mystream); 
                    sect1.PageInfo.Margin.Top = 5;
                    sect1.PageInfo.Margin.Bottom = 5;
                    sect1.PageInfo.Margin.Left = 5;
                    sect1.PageInfo.Margin.Right = 5;
                  sect1.PageInfo.PageWidth = (bitmap.Width / bitmap.HorizontalResolution) * 72;
                    sect1.PageInfo.PageHeight = (bitmap.Height / bitmap.VerticalResolution) * 72;                      
                    pdf1.Sections.Add(sect1);
                    Aspose.Pdf.Generator.Image image2 = new 
                    Aspose.Pdf.Generator.Image(sect1);
                    image2.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
                    sect1.Paragraphs.Add(image2);
                    image2.ImageInfo.ImageStream = mystream;
                    image2.ImageScale = 0.95F;                                                
                }
                catch (Exception e) {
                    throw e;
                }              
            }
            pdf1.Save(tempLocalPDFPath);

@anishvj

The old legacy Aspose.Pdf.Generator approach has been obsoleted in latest release of Aspose.Pdf for .NET. In latest release you can work with DOM (Document Object Model) approach and create DOM based XML templates to generate PDF documents. For more information please visit following helpful links of API documentation.

Basics of Aspose.PDF DOM API
Add Image to PDF
Set Image Size

Hi,
Could you please specify the version of the latest Aspose dll. We are using 9.1.0.0
Also is there any property to set resolution of the image using DOM approach, like this (setting width of the image) - img.FixWidth = 100

@anishvj

Please use the latest version of Aspose.PDF for .NET 21.11.

Unfortunately, Aspose.PDF does not provide APIs to set the resolution of image in PDF. However, we have logged a feature request as PDFNET-50952 in our issue tracking system. We will check the possibility of implementation of this feature and inform you via this forum thread once there is an update available on it.