Add JPEG image from Stream to PDF in Aspose.PDF for .NET - ArgumentNullException thrown

I am getting ArgumentNullException when calling Pdf.Save() after adding a JPEG image using UnmanagedMemoryStream object to the Pdf document. See sample code below. I am using Aspose.Pdf v4.8.0.0.

var pdf = new Pdf();
Section section = pdf.Sections.Add();
var image = new Image(section);
using (var bmp = new Bitmap(sourceStream))
{
// Set to page orientation landscape if image width exceeds page width
section.IsLandscape = (bmp.Width > section.PageInfo.PageWidth);
sourceStream.Seek(0, SeekOrigin.Begin);
// Set image scaling if image is larger than page size
if (image.ImageWidth > section.PageInfo.PageWidth || image.ImageHeight > Section.PageInfo.PageHeight)
image.ImageScale = 100;
}
section.Paragraphs.Add(image);
image.ImageInfo.ImageStream = sourceStream;
image.ImageInfo.ImageFileType = ImageFileType.Jpeg;
pdf.Save(targetStream); // <-- ArgumentNullException is thrown

The sourceStream is of type UnmanagedMemoryStream that is loaded by retreiving the JPEG from the assembly's resource manifest. Interestingly if I use a File path for the exact same jpeg image instead the error does not occur!!

Hello Ranjeet,

Thanks for using our products.

I have tested the scenario while adding the Image from memory stream object to the PDF document and I am unable to notice any problem. I have used the code snippet shared over Add Image to Existing PDF using Stream. Can you please try using the code snippet shared over above link and see if it can resolve your problem.

In case you still face any problem, please feel free to contact. We apologize for your inconvenience.

Most of my code is based on those samples you mention. I am setting the scaling of the image in cases where the image is larger than the page size to avoid image clipping. As I pointed out in my earlier post, the same code works fine if I provide the image file path instead of the image UnmanagedMemoryStream that is loaded from the Assembly's manifest.

Please test with this case and you will be able to reproduce to the error (I have attached the sample project for this).

Hi,

Thanks for sharing the code snippet. I have gone through it and have observed that you are using UnmanagedMemoryStream to load contents from Assembly manifest which ultimately is holding the Image stream. Is there any particular requirement of using Assembly Manifest to load the image file contents ?

Nevertheless, I have tried loading the contents of Image file into FileStream object and have tried placing that object in PDF document and the resultant PDF is properly being generated. I have used the following code snippet to test the scenario. The resultant PDF that I have generated is also in attachment. Please take a look.

[C#]

Pdf pdf = new Pdf();
Aspose.Pdf.Section section = pdf.Sections.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image(section);

// Create a FileStream object to read the imag file
FileStream fs = File.OpenRead(@"D:\pdftest\AsposePdfJpegSample\sample.jpg");
// Read the image into Byte array
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
// Create a MemoryStream object from image Byte array
MemoryStream ms = new MemoryStream(data);

section.Paragraphs.Add(image);
image.ImageInfo.ImageStream = ms;
image.ImageInfo.ImageFileType = ImageFileType.Jpeg;
pdf.Save(@"D:\pdftest\AsposePdfJpegSample\sample_resultant.pdf");
ms.Close();

Yes there is a requirement where certain images are included as part of the assembly that contains the logic to create the Pdf. Things like header logos and footer images are stored as embedded resources within the assembly rather than additional files.

The fact that the Image.ImageInfo.ImageStream property is of type System.IO.Stream it should operate seamlessly with any Stream class that inherits from System.IO.Stream... and the System.IO.UnmanagedMemoryStream that I am using inherits from System.IO.Stream.

Is there any ETA on a fix for this issue?

Nayyer, any response/update on this?

Hi,

Sorry for replying you late.

I have again tested the scenario using following code snippet and I am unable to notice any problem. In fact, in my scenario, the resultant PDF is being generated but it does not contain and image object. Can you please take a look over the following code that I have tested with Aspose.Pdf for .NET 4.8.0

[C#]

Pdf pdf = new Pdf();
Aspose.Pdf.Section section = pdf.Sections.Add();

Aspose.Pdf.Image image = new Aspose.Pdf.Image(section);
section.Paragraphs.Add(image);

Assembly assembly = Assembly.GetExecutingAssembly();
UnmanagedMemoryStream imageStream = (UnmanagedMemoryStream)assembly.GetManifestResourceStream("barcode.jpg");

//imageStream.Seek(0, SeekOrigin.Begin);
image.ImageInfo.ImageStream = imageStream;
image.ImageInfo.ImageFileType = ImageFileType.Jpeg;
pdf.Save(@"d:/pdftest/PdfWithBackGround.pdf");

We apologize for the delay and inconvenience.

Nayyar, the reason the resultant PDF is blank is because you are probably not getting the stream from the assembly manifest for the jpeg. The GetManifestResourceStream() method returns a null if an attempt is made to retreive the stream for a embedded resource that could not be located in the assembly manifest. The name supplied as a parameter (barcode.jpg) is most probably incorrect or incomplete or you have not marked this as a Embedded Resource in the item properties window. Also, the resource names are fully qualified and include the default namespace of the project and optionally any subfolder path names if the resource was added under a folder hierarchy.

E.g. Let's assume that the project is called jpegtest and the project properties state that the default namespace for the project is jpegtest. When the image is added as a Embedded Resource to this project at the root with name barcode.jpg then the resource at runtime will be identified with name "jpegtest.barcode.jpg". If the image was added to a subfolder lets say "Resources" then resource at runtime will be identified with name "jpegtest.Resources.barcode.jpg"

So, firstly the resource needs to be added as an Embedded Resource (set in item properties windows). Secondly, the resource name is the concatenation of the default namespace + any project folder structure + the resource name. Lastly, the fully qualified names are case sensitive.

You can confirm that you have the correct manifest resource name by reviewing the manifest resource names through the Assembly.GetManifestResourceNames() method.

Hope this helps you figure out why you are not able to duplicate the issue. You can also review the sample code I had provided in my earlier reply.

Hi,

We are working over this query and will get back to you soon. We are sorry for the delay and inconvenience.

Nayyer, we are coming close to completion on my project and I would hate to to see it get delayed due to this issue alone. Hoping you can give some good news. If not, maybe at least an ETA for the fix so that I can plan accordingly.

Hi,

Thanks for your patience and sorry for replying you late.

I have been able to reproduce the same problem and for the sake of correction, I have logged it as PDFNET-23236 in our issue tracking system. The development team is looking into the details of this problem and soon you will be updated with the status of correction. I have also requested them to share the ETA regarding its resolution.

Your patience and comprehension is greatly appreciated in this regard. Once again, we are really sorry for your inconvenience.

The issues you have found earlier (filed as 23236) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.