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

Generate PDF document from layout eg (Stacklayout, relativelayout, grid layout)

I am working on a xamarin forms project, and one of my requirements is to generate a pdf document from my layout, for example stacklayout. I want to print the controls inside my stacklayout into pdf document. If it is also possible to print my content page, that will also be helpful.

I attached my screenshot for more clarity on what i want to achieve.

Thank you

Attachment: screenshot_5854686e.zip

5 Replies

DB Dilli Babu Nandha Gopal Syncfusion Team December 6, 2018 12:57 PM UTC

Hi Ofentse 
Essential PDF doesn’t have a direct support for converting XAML element into PDF document. So, we have created a workaround sample for converting the XAML page to PDF in Xamarin platform. In this workaround sample, we will capture the XAML page screen by using dependency services and draw the captured screen image to the PDF document by using PdfBitmap object. The resultant PDF document will be preserved as a raster image and not searchable (not Vector quantity). Please refer the attached sample for more details.  
Dilli babu 

OF Ofentse December 7, 2018 08:25 PM UTC

Thank you for your response. but just one more question, how to i compute Pagination. Say i draw three images sequentially on the pdf page,
If the last image for example wont fit in the remaining space on a page, how do i move it to the next page, 

KK Karthik Krishnaraj Syncfusion Team December 10, 2018 09:07 AM UTC

Hi Ofentse,  
Thank you for your update.  
We can paginate to the next page, if the image is not fit the remaining space of the page. It can be performed by setting the PdfLayoutBreakType as FitElement and PdfLayoutType as Paginate in PdfLayoutFormat.  
Please find the code snippet below,  
Code snippet:  
//Create PdfImage object from image stream  
PdfImage image = PdfImage.FromStream(imageStream);  
//Create PdfLayoutFormat object  
PdfLayoutFormat format = new PdfLayoutFormat();  
format.Layout = PdfLayoutType.Paginate;  
format.Break = PdfLayoutBreakType.FitElement;  
PdfLayoutResult result = null;  
//Draw the image using PdfLayoutFromat  
result= image.Draw(page, new PointF(10, 10), format);  
for (int i = 0; i < 10; i++)  
//continuously draw the Image.  
result= image.Draw(result.Page, new PointF(result.Bounds.X,result.Bounds.Bottom+10), format);  
Using the above code snippet, we can achieve your requirement. Please let us know if you need further assistance in this.  

IB ibrahim February 27, 2019 01:03 PM UTC

SK Surya Kumar Syncfusion Team March 1, 2019 06:41 AM UTC

Hi Ibrahim, 

We suspect that you are facing the runtime permission issue in Android. 

From Android version 6.0(Marshamallow) and above, we need to grant runtime permission to save the PDF in the device. More details about this can be found in this link.  

We have modified the sample to generate runtime permission and attached the same below. So could you please try the same and let us know if it works. 

If you are facing any other issue. Please specify the exact issue with exception details, so that we can help you with that. 

Surya Kumar  

Live Chat Icon For mobile
Up arrow icon