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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: Xamarin.Forms |
Control: SfRichTextEditor |
Published Date: November 8, 2019 |
Last Revised Date: August 27, 2021

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!

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile