How to: Export a Document to HTML

  • 3 minutes to read

The following example demonstrates how to export a Printing System document to HTML format. To do this, the PrintingSystemBase.ExportToHtml method should be used. It also demonstrates what specific HtmlExportOptions may be used when a document is exported to HTML.

For this example to work, you first need to add a PrintingSystem component (named printingSystem1) to your project, and then execute the code below (for instance, in the Button.Click event handler of any button on a form). Also note that if you want the resulting file to be automatically opened in the default program which is used to open *.html files in your system, you can call the StartProcess method, which is also shown in this example.

using System.Drawing;
using System.Diagnostics;
using DevExpress.XtraPrinting;
// ...

private void button1_Click(object sender, EventArgs e)
{
    // A path to export a document.
    string documentPath = "c:\\Test.html";

    // Get HTML export options.
    HtmlExportOptions htmlOptions = printingSystem1.ExportOptions.Html;

    // Set HTML-specific export options.
    htmlOptions.CharacterSet = "UTF-8";
    htmlOptions.RemoveSecondarySymbols = false;
    htmlOptions.Title = "Test Title";

    // Set the pages to be exported, and page-by-page options.
    htmlOptions.ExportMode = HtmlExportMode.SingleFilePageByPage;
    htmlOptions.PageRange = "1, 3-5";
    htmlOptions.PageBorderColor = Color.Blue;
    htmlOptions.PageBorderWidth = 3;

    // Export the document to HTML.
    printingSystem1.ExportToHtml(documentPath);

    // Show the result.
    StartProcess(documentPath);
}

// Use this method if you want to automaically open
// the created HTML file in the default program.
public void StartProcess(string path)
{
    Process process = new Process();
    try
    {
        process.StartInfo.FileName = path;
        process.Start();
        process.WaitForInputIdle();
    }
    catch { }
}
See Also