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

Graphics DrawImage does not work on some PDFs

Hi all,
I try to put an image from stream on some PDF and the picture does't appears. The problem is that no error is thrown and I cannot figure out the problem.
I put in attachments one PDF that does't work good. I also tried to put in this an image from path(using PdfImage.FromFile) or to draw on document some figures(like DrawString, DrawRectangle, DrawLine and doesn't work at all.
I have one more file that has the same behavior(the orientation is portrait), but i don't have rights to put here. Thanks.
There is the code:
private FileResult JoinPdfwithAnnotations(string imageData, MemoryStream stream, string contentType, string fileName)
   byte[] pdfBytes;
   //Returns the pdf document without annotations
   if (string.IsNullOrEmpty(imageData)) {
      pdfBytes = stream.ToArray();
      return File(pdfBytes, contentType, fileName);
   List<string> base64AnnotationsList = imageData.Split(',').ToList();
   PdfLoadedDocument pdfDocument = new PdfLoadedDocument(stream);
   try {
      foreach (string base64Annotation in base64AnnotationsList) {
         PdfImage image;
         byte[] data = Convert.FromBase64String(base64Annotation);
         using (MemoryStream imageStream = new MemoryStream(data, 0, data.Length)) {
            image = PdfImage.FromStream(imageStream);
         PdfLoadedPage pdfPage = (PdfLoadedPage) pdfDocument.Pages[base64AnnotationsList.IndexOf(base64Annotation)];
         if (pdfPage != null) {
            PdfGraphics gfx = pdfPage.Graphics;
            gfx.DrawImage(image, 00);
         } else {
            LogManager.Instance.Error($"The image cannot be draw on pdf {fileName}");
      pdfBytes = stream.ToArray();
   } finally {
   return File(pdfBytes, contentType, fileName);

Attachment: PDF_949ce2eb.zip

5 Replies

MN Meikanda Nayanar Syncfusion Team April 22, 2019 07:21 AM

Hi Sorin, 
Greetings from Syncfusion. 
We have tried the provided code snippet to draw the image (Dummy image) in PDF document and it works fine in our end. We suspect that the issue may related to the image file which you have used. Kindly create the new incident using the below link and share the image file. It will helpful for us to provide the precise solution on this. 
Please let us know if you have any concerns on this. 

SM Sorin Miron April 23, 2019 07:51 AM

I uploaded the imageData string in a .txt, the image saved as PNG and the expected result. I hope that helps. Thanks! :D

Attachment: attachments_b62879f9.zip

MN Meikanda Nayanar Syncfusion Team April 24, 2019 07:05 AM

Hi Sorin, 
Thank you for the attachment. 
We have tried the provided code snippet with the given image data (string format in a .txt file) and image file (PNG). While drawing image in PDF document using image data, we are getting the below exception. 
We suspect that there might be problem in the image data which you have used. So we have created the sample to draw an image in PDF document with the provided image (converting the image to base64 string and drawn the image from base64 string) and the image is preserved properly in resultant PDF. Please find the sample for the same from below link, 
Kindly try the above sample in your end and let us know if you need any further assistance on this. 

SM Sorin Miron April 25, 2019 04:17 AM

I attached the correct imageData string. With this string, all works properly? Thank you for support! 

Attachment: ImageData_3632bf89.zip

SL Sowmiya Loganathan Syncfusion Team April 29, 2019 07:28 AM

Hi Sorin, 

Thanks for the update. 

The given image data (string format) works fine with the code snippet provided by you in our end and the resultant PDF is generated as like expected. Please let us know if you need any further assitance on this. 

Sowmiya L 

Live Chat Icon For mobile
Up arrow icon