We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Inserting images

I'm trying to insert an image into a PDF file but I am struggling.

The sample suggests I can just supply the path to the image file to PdfBitmap however, PdfBitmap expects a Stream.

I have tried supplying a File Stream and a Memory stream but I end up with the same result, no image is displayed on the PDF (although a box is drawn for it), and if the PDF is opened in Adobe Reader there is a pop up box that says:

An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.

I have also tried to use PdfImage.FromStream(stream) however this has the same result. Upon debugging, the stream appears to be populated with the file information, however, PdfImage doesnot appear to be populated.

Code below.

public async Task CreateQuotationPDF(Quotation Quotation)
{
   // create document
   PdfDocument doc = new PdfDocument();
   PdfPage page = doc.Pages.Add();

   // set font
   PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

   // create Header
   float pageWidth = doc.Pages[0].GetClientSize().Width;
   doc.Template.Top = CreateHeader("Quotation", pageWidth);

   PdfGraphics imageGraphics = page.Graphics;
   string webrootPath = _hostingEnvironment.WebRootPath.ToString();
   string imagePath = "images\\documents\\logo.jpg";
   string fullPath = Path.Combine(webrootPath, imagePath);

   // get logo
   using (var fileStream = new FileStream(fullPath, FileMode.Open))
   {
      PdfImage image = PdfImage.FromStream(fileStream);
      headerImageGraphics.DrawImage(image, new PointF(10, 0), new SizeF(100, 40));
   }

   
   // create footer
   PdfPageTemplateElement footer = await CreateFooter("", pageWidth);


   return doc;
}


7 Replies

SK Surya Kumar Syncfusion Team September 22, 2017 11:47 AM UTC

Hi Chris, 

Thank you for using Syncfusion products. 

We do have support only for loading image as Stream in  Asp.net Core platform at present and we have tried to reproduce the issue which you have mentioned using the code snippet which you have provided in the last update, but we are unable to reproduce the issue. Can you please provide us the image file in which the issue is reproduced so that we can help you better. 

Please find the sample which we tried to reproduce the issue from below link: 


Let us know if you need any further information in this. 

Regards, 
Surya Kumar 



CL Chris Lowe September 22, 2017 11:52 AM UTC

Image attached


Attachment: logo_d8350244.zip


SK Surya Kumar Syncfusion Team September 25, 2017 07:12 AM UTC

Hi Chris,  

We are able to reproduce the issue which you have mentioned in earlier response and we have posted the response for this query in the support incident which you have created, Please log on to our support website to check for further updates  
  
Regards, 
Surya Kumar 



KK Kevin Kilburn October 11, 2017 01:46 AM UTC

What exactly was the issue? I have the exact same problem.  However, I have images that show the blank block. I also have jpeg images that throw System.IndexOutOfRangeException.  Is there someplace that lists the supported encodings?



SK Surya Kumar Syncfusion Team October 11, 2017 01:28 PM UTC

Hi Kevin, 
 
At present we have support for drawing only JPEG image with JPEG encoding in Asp.Net Core platform and the issue which you have mentioned may be image specific issue. So, can you please provide us the image in which the issue is reproduced so that we can help you better in this. 
 
Please let us know if you need any further information. 
 
Regards, 
Surya Kumar 



KK Kevin Kilburn October 11, 2017 01:33 PM UTC

i got my images to print this morning after opening them and exporing them again in jpeg format.  i'm dumbfounded as to why one works and the others fail. the 1493200727-69050095.jpg image displays a blank image. the 1499896354-8132571.jpg image usually throws an outofrangeexception.  the jpeg versions of those file, surprisingly, work just fine.

I just figured this out this morning and have started to compare the files.

Attachment: Archive_97d7c17a.zip


SK Surya Kumar Syncfusion Team October 12, 2017 07:34 AM UTC

Hi Kevin,   

We are able to reproduce the issue which you have mentioned in earlier response and we have posted the response for this query in the support incident which you have created, Please log on to our support website to check for further updates   
   

Regards,  
Surya Kumar  


Loader.
Up arrow icon