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}");
         }
 
         pdfDocument.Save();
      }
      
      pdfBytes = stream.ToArray();
   } finally {
      pdfDocument.Close(true);
      stream.Close();
   }
 
   return File(pdfBytes, contentType, fileName);
}

Attachment: PDF_949ce2eb.zip

5 Replies

MN Meikanda Nayanar Syncfusion Team April 22, 2019 12:21 PM UTC

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. 
 
Regards, 
Meikandan 



SM Sorin Miron April 23, 2019 12:51 PM UTC

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 12:05 PM UTC

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. 
Screenshot: 
 
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, 
Screenshot:             
 
 
Kindly try the above sample in your end and let us know if you need any further assistance on this. 
Thanks 
Meikandan 



SM Sorin Miron April 25, 2019 09:17 AM UTC

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 12:28 PM UTC

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. 

Regards, 
Sowmiya L 


Loader.
Live Chat Icon For mobile
Up arrow icon