Save a small jpg file to PDF consuming tons of memory

@jcsupport88,

EDIT:

Can you remove this lines from your coded?

page.PageInfo.Width = image.Width + 100
page.PageInfo.Height = image.Height + 100

I tried without those lines and it works fine

Thanks.

Removing the 2 lines of code works, even with version 20.4, but we had those 2 lines of codes for a long time and it only encounter the issue with this particular images. Will your developer be able to determine the actual cause?

Is there issue with the width to height ratio of the image file. I took a HD (6110x3680) photo, NGTAPRIL16_NB23.jpg that I shared in the earlier thread and cropped it to 456x42 (see attached) NGTAPRIL16_NB23-Edited.jpg (7.1 KB)
and it has the same memory issue.

Simple test with images created from Paint:
455x41.jpg (1.3 KB)
500x50.jpg (3.7 KB)
455x41 has issue, 500x50 works fine.

@jcsupport88,

With what version of the API are you seen this? I used both and I do not see the memory problem.

Sorry for delayed response, have been tied up with other works. Were your tests with or without the following lines?

page.PageInfo.Width = image.Width + 100
page.PageInfo.Height = image.Height + 100

@jcsupport88

We are checking it and will get back to you shortly.

Any updates on my question from 5/23 on whether your tests included the 2 lines of code?

thanks

@jcsupport88

Thanks for your patience. We have run tests using 23.5 version of the API and the process took maximum of 440MB memory to generate PDF from the original image that you shared earlier. However, we have made some modifications to your code.
[C#]

Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = PdfDocument.Pages.Add();

Bitmap TempImage = null;

byte[] bitmapData = File.ReadAllBytes(dataDir + @"NGTAPRIL16_NB23.jpg");
System.Drawing.Image _image = System.Drawing.Image.FromStream(new MemoryStream(bitmapData));
TempImage = new Bitmap(_image);

page.PageInfo.Width = TempImage.Width + 100;
page.PageInfo.Height = TempImage.Height + 100;

Aspose.Pdf.Image Image = new Aspose.Pdf.Image();
Image.ImageStream = new MemoryStream(bitmapData);
page.Paragraphs.Add(Image);
PdfDocument.Save(dataDir + @"outputimage.pdf");

In the above code, you can ignore the part how Image is initialized as you are doing it fine already at your side. However, you are assigning image stream after adding image to the page paragraph. We shifted that work before adding image into paragraph as that is correct way. Please try to use the above-shared code snippet and feel free to let us know if memory consumption issue still occurs. We will further proceed to assist you accordingly.

Thanks for the reply, the image file NGTAPRIL16_NB23.jpg did not have the issue.

I attached several images here, can you test on your end:

  1. Paint created 455x41, has issue 455x41.jpg (1.3 KB)

  2. Paint created 500x50, no issue 500x50.jpg (3.7 KB)

  3. 455x41 file from my client, has issue steing.jpg (7.7 KB)

@jcsupport88

We tested using these 3 images as well:

In case you want to set page height and width as per image dimensions, you need to consider default margin value of PDF page i.e. 72Points. Either remove it or set page dimensions by adding this value (if you want to keep margins):

Use

page.PageInfo.Width = TempImage.Width + 100;
page.PageInfo.Height = TempImage.Height + 100;
// and
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

500x50X0Margins.pdf (5.3 KB)
steingX0Margins.pdf (10.7 KB)
455x41X0Margins.pdf (3.3 KB)

If you simply remove above code, you will get:
455x41.pdf (3.3 KB)
500x50.pdf (5.3 KB)
steing.pdf (10.7 KB)

I tested with the suggested changes and it works, even with our current version (20.4) of the Aspose.PDF. But due to having to set the margin, I get little bit of difference result:
AsposePDF Compare.jpg (369.7 KB)
The content is done by converting a HD image to PDF, it did NOT have the memory issue.

Left side is the one with margin setting, right side is our original code, note that left and top margin in the original are not there in the modified result.

I have to check our other stakeholder if this change is fine, but would like to know if Aspose was able to determine why image with large aspect ratio like the “411x41.jpg” I attached was causing the memory issue without having the margin defined for the page.

Thanks

@jcsupport88

Can you please attach this image again as we were unable to locate it within this forum thread.

455x41.jpg (1.3 KB)

@jcsupport88

With the latest version, it did not cause memory issue but raised below exception in our environment:
image.png (4.9 KB)

can you test this 500x50 one to see if exception also occur, if not, then seems like some aspect ratio would cause the exception
500x50.jpg (3.7 KB)

@jcsupport88

With this image, we did not face any exception nor the memory issue. Attached is the output and the code snippet used:

Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = PdfDocument.Pages.Add();

Bitmap TempImage = null;

byte[] bitmapData = File.ReadAllBytes(dataDir + @"500x50.jpg");
System.Drawing.Image _image = System.Drawing.Image.FromStream(new MemoryStream(bitmapData));
TempImage = new Bitmap(_image);

page.PageInfo.Width = TempImage.Width + 100;
page.PageInfo.Height = TempImage.Height + 100;

Aspose.Pdf.Image Image = new Aspose.Pdf.Image();
Image.ImageStream = new MemoryStream(bitmapData);
page.Paragraphs.Add(Image);
PdfDocument.Save(dataDir + @"outputimage.pdf");

outputimage.pdf (5.3 KB)

Thanks, I didn’t expect any issue with the 500x50, but why is the 455x41 would cause the exception, it is also a very small image.

@jcsupport88

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-54793

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you for all the efforts seeing this thru