View and render documents with GroupDocs.Viewer

GroupDocs.Viewer renders a document into something a browser can display — HTML, PDF, or one image per page — without Microsoft Office or any other external software. It is the API to reach for when you want to show a document rather than change it.

Every example below opens contract.docx and is ready to copy into a console app once you have installed the package.

Render a document to HTML

Rendering to HTML gives you one .html file per page. ForEmbeddedResources inlines images, fonts and stylesheets into each page, so a page is self-contained and needs no companion files.

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

using (Viewer viewer = new Viewer("contract.docx"))
{
    // {0} is replaced with the page number, so page 1 becomes page_1.html
    HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources(
        "viewer-render-to-html/page_{0}.html");

    viewer.View(viewOptions);
}

contract.docx is the sample file used in this example. Click here to download it.

viewer-render-to-html/page_1.html (147 KB)
viewer-render-to-html/page_2.html (74 KB)
viewer-render-to-html/page_3.html (828 bytes)

Download full output

To keep resources as separate files next to the HTML instead of inlining them, use HtmlViewOptions.ForExternalResources — useful when several pages share the same images and you want them cached once.

Render a document to PDF

Rendering to PDF produces a single file, which is the usual choice for printing or archiving.

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

using (Viewer viewer = new Viewer("contract.docx"))
{
    PdfViewOptions viewOptions = new PdfViewOptions("viewer-render-to-pdf.pdf");

    viewer.View(viewOptions);
}

contract.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 127 KB)

Download full output

Render a document to PNG

Rendering to images gives you one PNG per page — the right output for thumbnails and previews. PngViewOptions also accepts a width and height if you need a fixed size.

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

using (Viewer viewer = new Viewer("contract.docx"))
{
    PngViewOptions viewOptions = new PngViewOptions(
        "viewer-render-to-png/page_{0}.png");

    // Render only the first two pages
    viewer.View(viewOptions, 1, 2);
}

contract.docx is the sample file used in this example. Click here to download it.

viewer-render-to-png/page_1.png (68 KB)
viewer-render-to-png/page_2.png (67 KB)

Download full output

Read document information

Before rendering you often want to know what you are dealing with — how many pages, what format, whether it is encrypted. GetViewInfo answers that without rendering anything.

using System;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Results;

using (Viewer viewer = new Viewer("contract.docx"))
{
    ViewInfoOptions viewInfoOptions = ViewInfoOptions.ForHtmlView();
    ViewInfo viewInfo = viewer.GetViewInfo(viewInfoOptions);

    Console.WriteLine("File type: " + viewInfo.FileType);
    Console.WriteLine("Pages: " + viewInfo.Pages.Count);

    foreach (Page page in viewInfo.Pages)
    {
        Console.WriteLine("Page " + page.Number + ": " + page.Width + "x" + page.Height);
    }
}

contract.docx is the sample file used in this example. Click here to download it.

File type: Microsoft Word Open XML Document (.docx)
Pages: 3
Page 1: 612x792
Page 2: 612x792
Page 3: 612x792

Download full output

Learn more

The examples above cover the common cases. GroupDocs.Viewer also renders archives, CAD drawings, email messages, Outlook data files and Visio diagrams, caches rendered output, and processes attachments.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.