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

What is the best way to create complex pdf in Xamarin.Forms

Hello,

I want to create a pdf from my UI fields which contains some texts, some grids and also pictures (something like the image that I've attached), is it possible to do so? and could you please tell me what's the best way to do so? should I create my own template or is there any other way? could you please help me?

Thanks in advance,
Reihaneh

19 Replies

KC Karthikeyan Chandrasekar Syncfusion Team November 20, 2018 06:34 AM UTC

Hi Reihaneh, 
The mentioned image is not attached, can you please attach it once again. So that we could provide you the solution. 

Regards, 
Karthikeyan  



RK Reihaneh Khaksaran November 20, 2018 04:13 PM UTC

Hi,

Oh, right, sorry it wasn't in the correct format, it is attached now

Thank you

Attachment: image_113fa512.zip


KK Karthik Krishnaraj Syncfusion Team November 21, 2018 11:57 AM UTC

Hi Reihaneh,  
  
Thank you for your update.  
  
We have created the simple sample to create PDF as like the shared image template. In the sample we have using PdfGrid and PdfTextElement to draw the text and table format in PDF.  Please find the sample and output PDF document in below link.  
 
Sample link: 
 
Output PDF :   
 
Kindly refer the below UG link for Table creation in PDF: 
  
UG link 
 
Please check the sample and let us know if you need further assistance in this.  
 
Regards, 
Karthik. 



RK Reihaneh Khaksaran November 24, 2018 05:20 PM UTC

Hi,

Thank you so much for providing the sample, just one more question, I am trying to give backgroundBrush to one of the cells in my pdfGrid using this line of code: 
           
pdfGrid.Rows[0].Cells[0].Style.BackgroundBrush = new PdfSolidBrush(Syncfusion.Drawing.Color.LightGray);

but applying this style makes the whole row gray, I want only cell[0] in row[0] gets the style, am I doing something wrong?

Thanks in advance



KC Karthikeyan Chandrasekar Syncfusion Team November 26, 2018 10:53 AM UTC

Hi Reihaneh, 
Thank you for your update. 
The BackgroundBrush is property to apply the background color of a cell. We have ensured code snippet shared from your side. The first row first cell (i.e. Rows[0].Cells[0]) background color only changed as light gray. Please find the sample and output PDF below for your reference. 


We suspect that the background brush style may applied in other PdfGridCells in a row. Please check the above sample and let us know, if you need further assistance in this. 

Regards, 
Karthikeyan  



RK Reihaneh Khaksaran November 27, 2018 03:19 PM UTC

Hi,

Thank you for your reply, problem solved after removing the following line:

   pdfGridRow1.ApplyStyle(testObjectStyle);

but it's kinda weird, because in testObjectStyle, I had just defined a Font property like this

PdfGridCellStyle testObjectStyle = new PdfGridCellStyle
            {
               Font = new PdfStandardFont(PdfFontFamily.Helvetica, 15)
            };
no backgroundBrush, so why does it effect BackgroundBrush property as well?


KC Karthikeyan Chandrasekar Syncfusion Team November 28, 2018 09:01 AM UTC

Hi Reihaneh, 
Thanks for your reply. I have tried reproducing the reported issue, but with the sample we tried it is working fine. Please find the code snippet which we have tried from our side. Can you help us to reproduce this issue by modifying the code snippet, so that we could analyze in this and provide you a solution. 
 
Regards, 
Karthikeyan  



RK Reihaneh Khaksaran May 7, 2019 12:02 PM UTC

Hi,

In the sample you have provided for inserting an image to a pdf, you've used this code snippet:

            Stream imagestream = typeof(MainPage).Assembly.GetManifestResourceStream("GettingStarted.Xamarin_JPEG.jpg");
            PdfImage image = PdfImage.FromStream(imagestream);

so the Xamarin_JPEG.jpg is in PCL with build action set to embedded resources, Is there any way to use an image that is in drawable folder? and not pcl?


SK Surya Kumar Syncfusion Team May 8, 2019 01:50 PM UTC

Hi Reihaneh, 
 
The image from the device can be loaded as Stream to PdfImage constructor. Please refer the below mentioned Microsoft UG documentation for Xamarin to read or write files to internal storage. 
 
Let us know if you need any further information. 
 
Regards, 
Surya Kumar 



RK Reihaneh Khaksaran May 9, 2019 12:14 PM UTC

Hi,

Thanks for the reply, but I don't want to load the image from device, I wanna load it form drawable folder with build action set to Android Resources not embedded resources

Regards,
Reihaneh


SL Sowmiya Loganathan Syncfusion Team May 10, 2019 01:13 PM UTC

Hi Reihanesh, 

We can achieve your requirement of load an image from drawable folder with build action set to Android Resources and insert it to PDF document in Xamarin Android platform . Please find the below code snippet for more details, 

// Create a new PDF document 
PdfDocument document = new PdfDocument(); 
 
//Add a page to the document 
PdfPage page = document.Pages.Add(); 
 
//Create PDF graphics for a page 
PdfGraphics graphics = page.Graphics;           
 
//Get image  
var icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.logo); 
var imageStream = new MemoryStream(); 
 
//Compress image  
icon.Compress(Bitmap.CompressFormat.Png, 0, imageStream); 
 
//Create PdfBitmap image from stream  
PdfBitmap image = new PdfBitmap(imageStream);         
 
//Draw image  
graphics.DrawImage(image, new Rectangle(0, 0, 200, 100)); 
 
//Save the document to the stream 
MemoryStream stream = new MemoryStream(); 
document.Save(stream); 
 
//Close the document 
document.Close(true); 


Kindly try with the above code snippet in your end and let us know it satisfies your requirement. 

Regards, 
Sowmiya L 



RK Reihaneh Khaksaran May 11, 2019 02:09 PM UTC

Hi,

Thank you so much but I'm using Xamarin Forms not Xamarin Android, however I want my images to load to a pdfGrid from drawable folder with build action set to Android Resources, because I already have the images in my drawable folder for another purpose, so I don't wanna add them to PCL again, it makes my .apk size huge, oh and I don't want to use DependencyService either, so could you please provide me a code snippet which adds an image to a pdfGrid from drawable folder with build action set to Android Resources in Xamarin Forms, thanks in advance.

Best regards,
Reihaneh


SL Sowmiya Loganathan Syncfusion Team May 13, 2019 12:55 PM UTC

Hi Reihanesh, 
 
Currently we are working to achieve your requirement of “Load an image from drawable folder and insert it to PdfGrid” in Xamarin Forms platform. We will update the further details by 14th May, 2019. 
 
Regards, 
Sowmiya L  



RK Reihaneh Khaksaran May 13, 2019 01:31 PM UTC

Hi,

Thank you so much for your great support

Best regards,
Reihaneh


SL Sowmiya Loganathan Syncfusion Team May 14, 2019 12:38 PM UTC

Hi Reihanesh, 

We have created the sample to achieve your requirement of “Load an image from drawable folder and insert into PdfGrid”. Please find the sample for the same from below link, 


Kindly try with the above sample in your end and let us know if it solves the issue. 

Regards, 
Sowmiya L 



RK Reihaneh Khaksaran May 18, 2019 12:28 PM UTC

Hi,

Thank you this worked but in my app I have more than 50 images, so I should write this line for all the images?

 var icon = BitmapFactory.DecodeResource(activity.Resources, Resource.Drawable.logo);

Best regards,
Reihaneh


SL Sowmiya Loganathan Syncfusion Team May 20, 2019 12:50 PM UTC

Hi Reihanesh, 
 
We have checked with your requirement of “Getting more than one images from drawable folder”. We can achieve this using GetIdentifier method by providing the filename in the drawable folder. Please refer the below link for more details, 
 
 
Please try the above solution in your end and let us know if it solves the issue. 
 
Regards, 
Sowmiya L 



RK Reihaneh Khaksaran May 22, 2019 02:22 PM UTC

Hi,

yes thank you it gave me a hint and using this code snippet helped me achieve what I wanted

            int resourceId = (int)typeof(Resource.Drawable).GetField(imageName).GetValue(null);
            image = BitmapFactory.DecodeResource(activity.Resources, resourceId);

Thanks for your help

Best regards,
Reihaneh


SL Sowmiya Loganathan Syncfusion Team May 23, 2019 08:27 AM UTC

Hi Reihanes, 
 
We glad to know that the issue is resolved. 
 
Regards, 
Sowmiya L 



Loader.
Up arrow icon