Render text documents as HTML, PDF, and image files

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

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, 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 System.IO;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

Stream stream = File.OpenRead("terms_of_service.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);
}
Imports System.IO
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
            Dim stream As Stream = File.OpenRead("terms_of_service.md")
        
            ' Specify the file encoding. 
            Dim loadOptions As LoadOptions = New LoadOptions(FileType.MD)
        
            ' Convert the document to PDF.
            Using viewer = New Viewer(stream, loadOptions)
                Dim viewOptions = New PdfViewOptions("output.pdf")
                viewer.View(viewOptions)
            End Using
    End Sub
End Module

terms_of_service.md is the sample file used in this example. Click here to download it.

Binary file (PDF, 85 KB)

Download full output

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("terms_of_service.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);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.txt")
            ' Convert the text file to HTML.
            ' {0} is replaced with the current page number in the output file names.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html")
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

page_1.html (2 KB)
page_2.html (3 KB)
page_3.html (2 KB)
page_4.html (1 KB)

Download full output

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("terms_of_service.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);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.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.
            Dim viewOptions = HtmlViewOptions.ForExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}")
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

page_1.html (2 KB)
page_1/resource_1_s.css (109 bytes)
page_2.html (2 KB)
page_2/resource_2_s.css (109 bytes)
page_3.html (2 KB)
page_3/resource_3_s.css (109 bytes)
page_4.html (1 KB)
page_4/resource_4_s.css (109 bytes)

Download full output

External resources are placed 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("terms_of_service.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);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.txt")
            ' Convert the text file to HTML.
            ' Specify the output file name.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html")
            ' Render the file to a single page. 
            viewOptions.RenderToSinglePage = True
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

HTML file (7 KB)

Download full output

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("terms_of_service.txt"))
{
    // Convert the text file to PDF.
    var viewOptions = new PdfViewOptions("output.pdf");
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.txt")
            ' Convert the text file to PDF.
            Dim viewOptions = New PdfViewOptions("output.pdf")
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

Binary file (PDF, 109 KB)

Download full output

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("terms_of_service.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);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.txt")
            ' Convert the text file to PNG.
            ' {0} is replaced with the current page number in the output image names.
            Dim viewOptions = New PngViewOptions("output_{0}.png")
            ' Set width and height.
            viewOptions.Width = 800
            viewOptions.Height = 900
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

output_1.png (125 KB)
output_2.png (138 KB)
output_3.png (122 KB)
output_4.png (65 KB)

Download full output

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("terms_of_service.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);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.txt")
            ' Convert the text file to JPEG.
            ' {0} is replaced with the current page number in the output image names.
            Dim viewOptions = New JpgViewOptions("output_{0}.jpg")
            ' Set width and height.
            viewOptions.Width = 800
            viewOptions.Height = 1000
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

output_1.jpg (116 KB)
output_2.jpg (135 KB)
output_3.jpg (114 KB)
output_4.jpg (66 KB)

Download full output

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("terms_of_service.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);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("terms_of_service.txt")
            ' Convert the text file to HTML.
            ' {0} is replaced with the current page number in the output file names.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html")
            ' Set the maximum number of rows per page.
            viewOptions.TextOptions.MaxRowsPerPage = 30
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

terms_of_service.txt is the sample file used in this example. Click here to download it.

page_1.html (1 KB)
page_2.html (1 KB)
page_3.html (2 KB)
page_4.html (2 KB)
page_5.html (1 KB)
page_6.html (1 KB)
page_7.html (1 KB)

Download full output