This guide builds a small ASP.NET Core web application that shows documents in the browser. The viewer itself is GroupDocs.Viewer.UI, and GroupDocs.Viewer for .NET renders the documents for it. The steps use the .NET CLI; in Visual Studio they are the same.
The application has two endpoints:
/viewer serves the viewer, an Angular app that runs in the browser.
/viewer-api renders the documents for it. It reads the documents from the Files folder and stores the rendered pages in the Cache folder, so each page is rendered only once.
The viewer app and the middleware that serves it at /viewer
GroupDocs.Viewer.UI.SelfHost.Api
The API at /viewer-api, which renders documents with GroupDocs.Viewer for .NET. It runs on Windows, Linux and macOS; the former GroupDocs.Viewer.UI.SelfHost.Api.CrossPlatform package is deprecated
GroupDocs.Viewer.UI.Api.Local.Storage
A file storage on the local disk: the API reads documents from it and saves the files users upload
GroupDocs.Viewer.UI.Api.Local.Cache
A cache on the local disk for the rendered pages, thumbnails and PDF files
Configure the application
Replace the contents of Program.cs with the following code:
AddGroupDocsViewerUI registers the viewer app, and MapGroupDocsViewerUI serves it at /viewer. ApiEndpoint tells the app where the API is.
AddGroupDocsViewerSelfHostApi registers the API with its rendering engine, and MapGroupDocsViewerApi maps it at /viewer-api.
AddLocalStorage("./Files") and AddLocalCache("./Cache") set the folders for the documents and the rendered pages.
Set the license
Without a license, GroupDocs.Viewer works in evaluation mode, with the evaluation limitations. To apply a license, set the GROUPDOCS_LIC_PATH environment variable to the path of the license file, or set the path in code:
The viewer does not authenticate users. To let only signed-in users open documents, apply your authorization policy to both endpoints. Applying it to /viewer alone leaves the API, and with it every document, open to anyone:
Create the folders for the documents and the cache, then copy a few documents into Files:
mkdir Files
mkdir Cache
Run the application
Run the application with the dotnet run command in the my-viewer-app folder. The output looks like this:
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5081
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\my-viewer-app
In the browser, open the address from the output with the /viewer path, for example http://localhost:5081/viewer:
Click Open File to choose a document from the Files folder or to upload one.
To open a document directly, add its name to the address: http://localhost:5081/viewer?file=annual-report.docx.
The first time you open a document, its pages are rendered, so it takes longer. After that, the viewer reads them from the Cache folder.
Customize the viewer
Options of AddGroupDocsViewerUI set how the viewer renders documents (HTML or images), which file it opens first, its zoom, its theme, and the buttons it shows. For example, the following code hides the rotate buttons and the button that switches between the light and dark themes: