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

Free Support Forum - aspose.com

Image to PDF conversion is not correct

Hi Support,
I am facing an issue with Aspose.PDF 21.6.0.0 while converting image to PDF. In my scenario I have a PNG image which is converted to PDF after removing image transparency but in resultant PDF the image is very small.
I have attached the working sample along with the document converted via Adobe:


Could you please guide me in this scenario.
Regards,

@Wahaj_Khan

We tried to download the project from shared link but it seems like the access is restricted. Please upload the project to Dropbox or Google Drive and share the link with us so that we can proceed with it further.

Hi @asad.ali
Please find the project on following link:


Regards,

@Wahaj_Khan

We are still unable to download from the link. onedriveaccess.png (31.4 KB)

You can simply share sample code snippet and sample file for our reference while attaching them to your post in case you cannot upload to Google Drive or Dropbox.

Hi @asad.ali
Please find the below code snippet:
byte[] OutputFileBytes = null;
string tempfile = Path.GetRandomFileName();
Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document();
Aspose.Pdf.Page Sec = PdfDocument.Pages.Add();

        Bitmap TempImage = null;

        byte[] bitmapData = File.ReadAllBytes(@"..\..\..\Files\Image.png");
        Image _image = System.Drawing.Image.FromStream(new MemoryStream(bitmapData));
        TempImage = new Bitmap(_image);

        if (TempImage.Width > TempImage.Height)
        {
            Sec.PageInfo.IsLandscape = true;
        }
        else
        {
            Sec.PageInfo.IsLandscape = false;
        }

        Aspose.Pdf.Image Image = new Aspose.Pdf.Image
        {
            FixWidth = TempImage.Width - Sec.PageInfo.Margin.Left - Sec.PageInfo.Margin.Right,
            FixHeight = TempImage.Height - Sec.PageInfo.Margin.Top - Sec.PageInfo.Margin.Bottom
        };
        Sec.Paragraphs.Add(Image);

        Image.ImageStream =RemoveTransparency(new MemoryStream(bitmapData));

        using (FileStream CompressedFileStream = new FileStream(tempfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
        {
            PdfDocument.Save(CompressedFileStream);
            OutputFileBytes = new byte[CompressedFileStream.Length];
            CompressedFileStream.Seek(0, System.IO.SeekOrigin.Begin);
            CompressedFileStream.Read(OutputFileBytes, 0, (int)CompressedFileStream.Length);
        }

private static Stream RemoveTransparency(Stream fileStream)
       {
           Stream stream = new MemoryStream();
           fileStream.Seek(0, System.IO.SeekOrigin.Begin);
           using (Bitmap src = new Bitmap(fileStream))
           {
               Bitmap target = new Bitmap(src.Size.Width, src.Size.Height);
               Graphics g = Graphics.FromImage(target);
               g.Clear(System.Drawing.Color.White);
               g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
               g.DrawImage(src, 0, 0);
               target.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
           }
           return stream;
       }

Please find the image, resultant file and document converted from PDF attached with this ticket.
Converted from adobe.pdf (213.5 KB)
AM—EpiPen-1623170766-47713 (1).pdf (223.6 KB)
AM—EpiPen-1623170766-47713 (1).png (66.2 KB)

Regards,

@Wahaj_Khan

We have been able to reproduce the similar issue in our environment while testing the scenario. Therefore, we have logged it as PDFNET-50115 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi @asad.ali
Any update on the said issue?
Regards,

@Wahaj_Khan

The issue has recently been logged in our issue management system and it is pending for a review. We will investigate and resolve it on first come first serve basis and inform you as soon as we have some updates regarding its fix. Please be patient and spare us some time.

We are sorry for the inconvenience.