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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Graphics DrawImage does not work on some PDFs

Thread ID:

Created:

Updated:

Platform:

Replies:

144118 Apr 19,2019 09:37 AM UTC Apr 29,2019 12:28 PM UTC ASP.NET MVC - EJ 2 5
loading
Tags: PDF
Sorin Miron
Asked On April 19, 2019 09:37 AM UTC

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

Meikanda Nayanar [Syncfusion]
Replied On 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 


Sorin Miron
Replied On 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

Meikanda Nayanar [Syncfusion]
Replied On 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 


Sorin Miron
Replied On 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

Sowmiya Loganathan [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon