Render text documents as HTML, PDF, and image files

GroupDocs.Viewer for .NET allows you to convert text documents to HTML, PDF, PNG, and JPEG formats so you can view document content in a web browser, PDF or image viewer application.

To start with the GroupDocs.Viewer API, create a Viewer class instance. Pass a text document you want to view to the class constructor. You can load the document from a file or stream. Call one of the Viewer.View method overloads to convert the image to HTML, PDF, PNG, or JPEG format. These methods allow you to render the entire document or specific pages.

View text files online View demos and examples on GitHub

Supported text file formats

GroupDocs.Viewer supports the following text and programming file formats:

When you load a text document from a file or FileStream, GroupDocs.Viewer detects the document’s format based on the filename extension. If you use other stream types to load your document (such as MemoryStream or NetworkStream), GroupDocs.Viewer can determine the file format only for the following document types: TXT, XML, CS, and VB. For other file types, you should explicitly specify their format. To do this, create a LoadOptions class instance with the FileType parameter and pass this instance to the Viewer class constructor.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

// Implement a method that returns a stream with document data.
Stream stream = GetFileStream("markdown-file.md");

// Specify the file encoding. 
LoadOptions loadOptions = new LoadOptions(FileType.MD);

// Convert the document to PDF.
using (var viewer = new Viewer(stream, loadOptions))
{
    var viewOptions = new PdfViewOptions("output.pdf");
    viewer.View(viewOptions);
}

Render text files as HTML

Create an HtmlViewOptions class instance and pass it to the Viewer.View method to convert a text file to HTML. The HtmlViewOptions class properties allow you to control the conversion process. For instance, you can embed all external resources in the generated HTML file, minify the output file, and optimize it for printing. Refer to the following documentation section for details: Rendering to HTML.

Create HTML files with embedded resources

To save all elements of an HTML page (including text, graphics, and stylesheets) into a single file, call the HtmlViewOptions.ForEmbeddedResources method and specify the output file name.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to HTML.
    // {0} is replaced with the current page number in the output file names.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html");
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render a text file to HTML

Create HTML files with external resources

If you want to store output HTML files and additional resource files (such as fonts, images, and style sheets) separately, call the HtmlViewOptions.ForExternalResources method and pass the following parameters:

  • The output file path format
  • The path format for the folder with external resources
  • The resource URL format
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to HTML.
    // Specify the output file names and location of external resources.
    // {0} and {1} are replaced with the current page number and resource name, respectively.
    var viewOptions = HtmlViewOptions.ForExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}");
    viewer.View(viewOptions);
}

The image below demonstrates the result. External resources are placed in a separate folder.

Place HTML resources in a separate folder

Create a single HTML page

If you need to display the entire document content on a single HTML page, enable the HtmlViewOptions.RenderToSinglePage option, as shown below:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to HTML.
    // Specify the output file name.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html");
    // Render the file to a single page. 
    viewOptions.RenderToSinglePage = true;
    viewer.View(viewOptions);
}

Render text files as PDF

Create a PdfViewOptions class instance and pass it to the Viewer.View method to convert a text file to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file, reorder its pages, and specify the quality of document images. Refer to the following documentation section for details: Rendering to PDF.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to PDF.
    var viewOptions = new PdfViewOptions("output.pdf");
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render a text file to PDF

Render text files as PNG

Create a PngViewOptions class instance and pass it to the Viewer.View method to convert a text file to PNG. Use the PngViewOptions.Height and PngViewOptions.Width properties to specify the output image size in pixels.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to PNG.
    // {0} is replaced with the current page number in the output image names.
    var viewOptions = new PngViewOptions("output_{0}.png");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 900;
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render a text file to PNG

Render text files as JPEG

Create a JpgViewOptions class instance and pass it to the Viewer.View method to convert a text file to JPEG. Use the JpgViewOptions.Height and JpgViewOptions.Width properties to specify the output image size in pixels.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to JPEG.
    // {0} is replaced with the current page number in the output image names.
    var viewOptions = new JpgViewOptions("output_{0}.jpg");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 1000;
    viewer.View(viewOptions);
}

Detect text files encoding

To detect text file encoding automatically, enable the LoadOptions.DetectEncoding property. When Viewer fails to detect encoding it falls back to LoadOptions.Encoding property value.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
var loadOptions = new LoadOptions();
loadOptions.DetectEncoding = true;
using (var viewer = new Viewer("message.txt", loadOptions))
{
    var viewOptions = HtmlViewOptions.ForEmbeddedResources();
    viewer.View(viewOptions);
}          

Specify rendering options

GroupDocs.Viewer supports the TextOptions class that allows you to specify different options for rendering text files. To access these options, use the TextOptions property for one of the following classes (depending on the output file format):

The TextOptions class contains the following properties:

  • MaxRowsPerPage—Specifies the maximum number of rows per page. The default value is 55.

  • MaxCharsPerRow—Specifies the maximum number of characters per row. The default value is 85.

The following example demonstrates how to change the number of rows displayed on each HTML page:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("TermsOfService.txt"))
{
    // Convert the text file to HTML.
    // {0} is replaced with the current page number in the output file names.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html");
    // Set the maximum number of rows per page.
    viewOptions.TextOptions.MaxRowsPerPage = 30;
    viewer.View(viewOptions);
}

The image below illustrates the result:

Specify the number of rows per page