New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Load HTML file in RadEditor and Export it to PDF with RadPdfProcessing

Environment

Product Progress® Telerik® UI for ASP.NET AJAX

Description

The article shows how to automatically import on Page_Load HTML content in RadEditor from an external HTML file and export it to PDF with the help of HtmlFormatProvider and PdfFormatProvider providers part of the Progress RadPdfProcessing library.

Solution

Here is the solution:

<telerik:RadEditor ID="RadEditor1" runat="server"></telerik:RadEditor>
using Telerik.Windows.Documents.Flow.Model;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RadEditor1.Content = File.ReadAllText(Server.MapPath("~/HTML_content_for_RadEditor.html"));
        string htmlContent = RadEditor1.Content;
        string path = Server.MapPath("~/Converted_to_PDF_content.pdf");
        string filename = Path.GetTempFileName();
        Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
        // Create a document instance from the content. 
        RadFlowDocument document = htmlProvider.Import(htmlContent);

        Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
        using (Stream output = File.OpenWrite(path))
        {
            // Export the document. The different overloads enables you to export to a byte[] or to a Stream. 
            pdfProvider.Export(document, output);
        }
    }
}
Imports Telerik.Windows.Documents.Flow.Model

Public Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        RadEditor1.Content = File.ReadAllText(Server.MapPath("~/HTML_content_for_RadEditor.html"))
        Dim htmlContent As String = RadEditor1.Content
        Dim path As String = Server.MapPath("~/Converted_to_PDF_content.pdf")
        Dim filename As String = Path.GetTempFileName()
        Dim htmlProvider As Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider = New Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider()
        Dim document As RadFlowDocument = htmlProvider.Import(htmlContent)
        Dim pdfProvider As Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider = New Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider()

        Using output As Stream = File.OpenWrite(path)
            pdfProvider.Export(document, output)
        End Using
    End Sub

See Also

In this article