INSERTING IMAGE INTO A NEW PDF DOCUMENT RETURNS "NULL"

Hi Syncfusion team,
As mentioned in the subject,
I am trying to generate a PDF file using xamarin forms by following the guide in this link (https://help.syncfusion.com/file-formats/pdf/create-pdf-file-in-xamarin)
For the first part and second part of the guide I was able to do.
When it comes to inserting image into the Document, I keep on getting"System.ArgumentNullException:'Value cannot be null. Parameter name: stream'"
Image attached.

I have also attached thelinkto my solution file for your reference.
Please guide me where am I making mistakes.
Thank you
Best regards

3 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team May 25, 2021 03:11 PM UTC

Hi Jai,  
 
Thank you for contacting Syncfusion support. 
 
We have checked the provided sample on our end, we have provided the proper path for getting the input image file from Assets folders. We have attached the code snippet for your reference. Kindly please try the below code snippet in your end and let us know the result. 
 
Please find the code snippet for insert an image into PDF in Xamarin, 
// Create a new PDF document 
            PdfDocument document = new PdfDocument(); 
 
            //Add a page to the document 
            PdfPage page = document.Pages.Add(); 
 
            //Create PDF graphics for the page 
            PdfGraphics graphics = page.Graphics; 
 
            //Set the standard font 
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); 
            Stream imgStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("App5.Assets.Electricalengineering.jpg"); 
 
            //Draw the text 
            graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0)); 
 
            PdfImage image = PdfImage.FromStream(imgStream); 
            //Draws the image to the PDF page 
            RectangleF bounds = new RectangleF(176, 0, 390, 130); 
            page.Graphics.DrawImage(image, bounds); 
            //Save the document to the stream 
            MemoryStream stream = new MemoryStream(); 
            document.Save(stream); 
 
            //Close the document 
            document.Close(true); 
 
 
Please refer the below link for more information, 
 
Please let us know if you need any further assistance with this.  
 
Regards, 
Gowthamraj K  



JK Jai kumaran May 26, 2021 03:19 AM UTC

Hi Mr.Gowtham,

Thank you for the code snippet. Based on the code snippet, the difference was I did not include the "App5" before the "Assets".
After adding the "App5" and I have tried it again, the execution still gives me the same error.

Would you try it out my program by inserting any images of your choice and let me know if the code works and where is my mistake?

Thank you
Best regards


GK Gowthamraj Kumar Syncfusion Team May 26, 2021 11:06 AM UTC

Hi Jai, 
 
In the provided sample, you are mentioning the folder name as “Assests” and getting the input file with “Assets” folder path in your code. Due to this incorrect folder path name, the reported exception occurs. To overcome this, we need to provide the proper folder path name to get the file in your code sample. We have attached the modified workable sample for your reference. Kindly please try the sample on your end and let us know the result.   
 
 
Stream imgStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("App5.Assests.Electricalengineering.jpg"); 
 
 
 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 


Marked as answer
Loader.
Up arrow icon