How to export a Rich Text Editor text to Word and PDF document in Xamarin.Forms
Export Rich Text Editor Text to Word/PDF document
You can export Rich Text Editor’s text to Word and PDF document with its formatting in Xamarin.Forms platform. This can be achieved using Syncfusion.Xamarin. DocIORenderer and Syncfusion.Xamarin.SfRichTextEditor NuGet packages.
Step 1: Create a Xamarin Forms application for .NET Standard Xamarin application, the target framework must be .NET Standard 1.4 or higher version.
Step 2: Add the following NuGet packages from nuget.org as reference to your Xamarin application. Refer here to install NuGet from NuGet Package Manager.
- Syncfusion.Xamarin.DocIORenderer
- Syncfusion.Xamarin.SfRichTextEditor
- SkiaSharp
You can also install these packages through package manager console using the following commands:
- Install-package Syncfusion.Xamarin.DocIORenderer -source https://www.nuget.org/api/v2
- Install-package Syncfusion.Xamarin.SfRichTextEditor -source https://nuget.org/api/v2
- Install-Package SkiaSharp -Version 1.59.3 -source https://nuget.org/api/v2
Step 3: Add two buttons in your XAML page.
XAML
<Button x:Name="btnGenerateWord" Clicked ="OnWordButtonClicked" Text ="Export as Word document" HorizontalOptions="Center" VerticalOptions="Center"></Button> <Button x:Name="btnGeneratePDF" Clicked ="OnPDFButtonClicked" Text ="Export as PDF document" HorizontalOptions="Center" VerticalOptions="Center"></Button>
Add the following code example in the button click event handler to export Rich Text Editor text to Word and PDF document.
C#:
private void OnPDFButtonClicked(object sender, EventArgs e) { //Creates an instance of WordDocument Instance (Empty Word Document) WordDocument document = new WordDocument(); //Add a section and paragraph in the empty document document.EnsureMinimal(); //Append HTML string to Word document paragraph document.LastParagraph.AppendHTML(rte.GetHtmlString()); //Instantiation of DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document into PDF document PdfDocument pdf = render.ConvertToPDF(document); //Releases all resources used by the Word document and DocIO Renderer objects render.Dispose(); document.Close(); //Saves the PDF file MemoryStream outputStream = new MemoryStream(); pdf.Save(outputStream); //Closes the instance of PDF document object pdf.Close(); } private void OnWordButtonClicked(object sender, EventArgs e) { //Creates an instance of WordDocument Instance (Empty Word Document) WordDocument document = new WordDocument(); //Add a section and paragraph in the empty document document.EnsureMinimal(); //Append HTML string to Word document paragraph document.LastParagraph.AppendHTML(rte.GetHtmlString()); MemoryStream outputStream = new MemoryStream(); //Saves the Word document document.Save(outputStream,Syncfusion.DocIO.FormatType.Docx); //Closes the instance of Word document object document.Close(); }
The sample that explains how to export a Rich Text Editor text to a Word and PDF document with its formatting in Xamarin.Forms can be downloaded here.
If you have any questions or require clarification about this support, please let us know in the comments below. You can also contact us through our support forum or Direct Trac. We are happy to assist you!