Articles in this section
Category / Section

How to export a Rich Text Editor text to Word and PDF document in Xamarin.Forms

4 mins read

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:

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!

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied