Image in Pdf

Hello I need Help !!

How to add image to pdf from path(string)????

3 Replies

SL Sowmiya Loganathan Syncfusion Team October 11, 2017 04:43 PM UTC

Hi Przemyslaw, 

At present we did not have the support to add the image to PDF from path(string). However we can get the image as stream using DependencyService for various platform in Xamarin Froms by giving the path of the image.  

Please find the below link for more details :  

Please try the above mentioned details to load the images from path using DependencyService

Then you can load the image stream into the PdfBitmap API as given in the below code snippet. 

            //Create a new PDF document. 
            PdfDocument doc = new PdfDocument(); 
 
            //Add a page to the document. 
            PdfPage page = doc.Pages.Add(); 
 
            //Create PDF graphics for the page 
            PdfGraphics graphics = page.Graphics; 
 
            //Load the image 
            Stream imageStream = File.OpenRead("Autumn Leaves.jpg"); 
 
            //Load the image from the stream 
            PdfBitmap image = new PdfBitmap(imageStream); 
 
            //Draw the image 
            graphics.DrawImage(image, 0, 0); 
 
            //Save the document. 
            doc.Save("Output.pdf"); 
 
            //Close the document. 
            doc.Close(true); 

Please let us know if you need further assistance on this. 

Regards, 
Sowmiya L 



PK Przemyslaw Kolodziejski October 12, 2017 01:21 PM UTC

If it work you saved my life :P (3 days) 


Thanks



SK Sasi Kumar Sekar Syncfusion Team October 12, 2017 04:49 PM UTC

Hi Przemyslaw, 
 
In Xamarin.Form we can get stream from file path using DependencyService. Please follow below procedure to get stream from file path. 
For dependency service we should implement dependency methods in Android,iOS and UWP platforms and implementation in below for your reference.  
Android: 
 public Stream LoadFromFile(string fileName) 
   { 
         string root = null; 
         if (Android.OS.Environment.IsExternalStorageEmulated) 
           { 
               root = Android.OS.Environment.ExternalStorageDirectory.ToString(); 
           } 
           else 
              root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
            
         return System.IO.File.OpenRead(root+"/Syncfusion/"+fileName.ToString()); 
    } 
Note: The input image file should be in Syncfusion folder. 
iOS: 
public Stream LoadFromFile(string filename) 
 { 
   string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
   string filePath = Path.Combine(path, filename);    
   return System.IO.File.OpenRead(filePath) 
 } 
Note: The input image file should be in MyDocument folder. 
UWP: 
 
public stream LoadFromFile(string fileName) 
 { 
        LoadFile1(filename).Result; 
 } 
 
public async Task<Stream> LoadFile1(string filename) 
{ 
 
  StorageFolder storageFolder = ApplicationData.Current.LocalFolder; 
  StorageFile sampleFile = await storageFolder.GetFileAsync(filename); 
 
  Stream outStream = await sampleFile.OpenStreamForReadAsync(); 
 
  return outStream; 
           
} 
Note: The input image file should be in Current.LocalFolder location. 
Created a simple sample based on above instruction. Please find the sample below for your reference. 
Sample link: 
Please let us know if you need further assistance on this.  
Regards, 
Sasi Kumar S. 


Loader.
Up arrow icon