Setup in .NET 6 or Higher
Startup.cs file
Within the Startup.cs file of a .NET Core project, these lines of code define a middleware pipeline configuration using the MapWhen method. The provided predicate checks if the incoming request path ends with DocImage.axd. Inside this block, the UseDoconut method is called with a DoconutOptions object as an argument.
app.MapWhen(
ctx => ctx.Request.Path.Value?.EndsWith("DocImage.axd", StringComparison.OrdinalIgnoreCase) == true,
branch => branch.UseDoconut());Add Doconut Namespaces
In your project, add the following using statements:
using Doconut;
using Doconut.Configs;
using Doconut.Clouds;Instantiate Viewer object
This line instantiates a new instance of the Viewer class, allowing you to create a viewer object within your code.
// License is configured via AddDoconut; obtain the viewer from DI.
var docViewer = app.Services.GetRequiredService<Viewer>();Setup a DocOptions object
Example initialization of a DocOptions object with specific configurations:
```csharp
// Document opening options – password and session timeout
var docOptions = new DocOptions
{
Password = string.Empty,
TimeOut = 30
};
// Per‑format rendering options – set image resolution via the format‑specific config
var pdfConfig = new PdfConfig
{
ImageResolution = 200
};
```Setup a config object
Initialize a new PdfConfig object with specific configurations for handling PDF documents:
var config = new PdfConfig { DefaultRender = true, ExtractHyperlinks = true };Open your document
This line invokes the OpenDocument method of the viewer object, passing the path of the document file located on the server and the previously configured PdfConfig and DocOptions objects as parameters.
var token = await viewer.OpenDocumentAsync(path, new PdfConfig
{
AllowSearch = true,
AllowCopy = true,
ImageResolution = 150
});Add the viewer in your HTML
<div id="div_ctlDoc"></div>Add CSS and JavaScript files
Add the following CSS and JavaScript files in the page. These scripts are included in the ZIP file with code samples.
<!-- Main Viewer Related Scripts -->
<script src="scripts/splitter.js"></script>
<script src="scripts/docViewer.js"></script>
<script src="scripts/documentLinks.js"></script>
<!-- Viewer functions -->
<script src="scripts/docViewer.UI.js?v=1"></script>Add Licenses to Your Project
Automatic License Detection
To initialize the docViewer with automatic license detection, use the following line:
app.MapPost("/api/open", async (Viewer viewer) =>
{
// The token is opaque — hand it to the widget, never log or persist it.
string token = await viewer.OpenDocumentAsync("wwwroot/files/Sample.pdf");
return Results.Ok(new { token });
});Add Manual Default License
You can manually specify the path to a single default license file when initializing the viewer.
builder.Services.AddDoconut(options =>
{
options.LicensePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "Doconut.Viewer.lic");
});Add License by Stream
This method uses the Viewer.DoconutLicense(Stream licenseStream) overload to add a license using a stream.
builder.Services.AddDoconut(options =>
{
options.LicensePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "Doconut.Viewer.lic");
});Add License by XML Document
This method uses the Viewer.DoconutLicense(XMLDocument licence) overload to add a license using an XML document.
// License XML from a database, environment variable, or secret manager —
// no file on disk. Beaten only by LicenseStream.
builder.Services.AddDoconut(options =>
{
options.LicenseContent = Environment.GetEnvironmentVariable("DOCONUT_LICENSE") ?? "";
});Manual Default Multiple Plugin Licenses
If you need to specify multiple licenses, you can provide a list of file paths to the viewer initialization.
builder.Services.AddDoconut(options =>
{
options.LicensePath = "Doconut.Viewer.lic";
});
builder.Services.AddDistributedMemoryCache(); // session state needs a cache (MVC/Razor Pages already add one)
builder.Services.AddSession(); // Doconut document security rides on ASP.NET session state
app.UseSession(); // call UseSession() before UseDoconut()
app.UseDoconutResources(); // must be registered before UseDoconut()
app.UseDoconut();Add Plugin License by Stream
This method uses the Viewer.SetLicensePlugin(Stream pluginLicence) overload to add a plugin license using a stream.
using var pluginLicenseStream = new FileStream("path/to/your/plugin-license.lic", FileMode.Open);
builder.Services.AddDoconut(options =>
{
// The stream takes precedence over LicenseContent, LicensePath, and auto‑search.
options.LicenseStream = pluginLicenseStream;
});Add Plugin License by XML Document
This method uses the Viewer.SetLicensePlugin(XMLDocument xmlPluginLicence) overload to add a plugin license using an XML document.
builder.Services.AddDoconut(options =>
{
options.LicensePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "Doconut.Viewer.lic");
});Required NuGet Packages
To ensure your project is fully compatible with .NET Core Standard, the following NuGet packages need to be included, listed in alphabetical order.
- MessagePack - Version 2.5.140
- Microsoft.Bcl.AsyncInterfaces - Version 8.0.0
- Microsoft.Extensions.Caching.Abstractions - Version 8.0.0
- Microsoft.Extensions.Configuration - Version 8.0.0
- Microsoft.Extensions.Configuration.Abstractions - Version 8.0.0
- Microsoft.Extensions.Configuration.FileExtensions - Version 8.0.0
- Microsoft.Extensions.Configuration.Json - Version 8.0.0
- Microsoft.Extensions.FileProviders.Abstractions - Version 8.0.0
- Microsoft.Extensions.FileProviders.Physical - Version 8.0.0
- Microsoft.Extensions.FileSystemGlobbing - Version 8.0.0
- Microsoft.Extensions.Primitives - Version 8.0.0
- System.Buffers - Version 4.5.1
- System.Formats.Asn1 - Version 8.0.0
- System.Numerics.Vectors - Version 4.5.0
- System.Security.Cryptography.Cng - Version 5.0.0
- System.Security.Cryptography.Pkcs - Version 8.0.0
- System.Text.Encoding.CodePages - Version 8.0.0
- System.Text.Encodings.Web - Version 8.0.0
- System.Text.Json - Version 8.0.3
- System.Threading.Tasks.Extensions - Version 4.5.4
Render Configuration Options
You can configure the rendering behavior using the following options:
- CadConfig: Used for DWG, DXF, and DGN files.
- EmailConfig: Used for EML, EMLX, and MSG files.
- EpubConfig: Used for EPUB files.
- ExcelConfig: Used for Excel and CSV files.
- MhtConfig: Used for MHT files.
- PptConfig: Used for PowerPoint files.
- ProjectConfig: Used for MPP files.
- PsdConfig: Used for PSD files.
- TxtConfig: Used for TXT files.
- TiffConfig: Used for TIFF files.
Render Options Configuration
The render options are configured using the DefaultRender property:
- DefaultRender = true : This option offers higher quality rendering but uses more server memory and processing time.
- DefaultRender = false : This option provides faster rendering at the expense of slightly reduced quality.
How To Add An Image Annotation
In C#
1. Open the document and get the dimensions of the first page. 2. Initialize the AnnotationManager with the page dimensions. 3. Create an ImageAnnotation, setting the image path in imgUrl, and specify the position and size within Rectangle. 4. Add the annotation to the manager and load it in the document viewer to display.
var token = await viewer.OpenDocumentAsync(pathToFile, config, documentOptions);
// The annotation manager is automatically bound to the opened document's page dimensions.
using var annMgr = viewer.GetAnnotationManager(token);
var imgAnn = new ImageAnnotation(
1,
new Rectangle(600, 700, 750, 300),
"img/sample.png"
);
annMgr.Add(imgAnn);
viewer.LoadAnnotationData(token, annMgr);In JavaScript
1. Ensure YourController is available. 2. Create a new ImageAnnotation, setting the image path with SetNote. 3. Add and paint the annotation to render it on the document.
function ImageStamp() {
if (null != objctlDoc.YourController()) {
var objStampImage = new ImageAnnotation({ left: 200, top: 200, width: 260, height: 55 });
objctlDoc.YourController().AddAnnotation(null, objStampImage, null);
objStampImage.SetNote('/images/Approved_Stamp.png');
objStampImage.Paint();
}
}Custom Fonts in Word documents
The FontFolders property in the WordConfig class specifies custom directories where the application will search for fonts. This is particularly useful for processing Word documents that rely on fonts not installed in the system's default font directories.
var config = new WordConfig
{
FontFolders = new string[] { "C:\\CustomFonts", "D:\\SharedFonts" }
};Important Update for Doconut 25.2.0
It is essential to update the System.Text.Json package to version 8.0.5 in all your projects. This update is crucial to ensure compatibility with the latest features and to address important security improvements.
Email Encoding
The EmailEncoding property in the EmailConfig class specifies the preferred encoding for both the email's subject and body.
var emailConfig = new EmailConfig {
EmailEncoding = Encoding.UTF8 // Set preferred email encoding
};Subject & Body Encoding
The SubjectEncoding and BodyEncoding properties in the EmailConfig class define the encoding used for the subject and body of an email, respectively.
var emailConfig = new EmailConfig {
SubjectEncoding = Encoding.UTF8, // Set encoding for the subject
BodyEncoding = Encoding.UTF8 // Set encoding for the body
};Word Viewer Culture Support
The DocumentCulture property in the WordConfig class allows you to explicitly define the culture for DOC and DOCX files. This ensures that localized content—such as dates, currencies, and numbers—renders exactly as it does in Microsoft Word. This is particularly useful for languages with specific formatting requirements.
var config = new WordConfig { DocumentCulture = "he-IL" }; // Hebrew (Israel)
var config = new WordConfig { DocumentCulture = "ar-SA" }; // Arabic (Saudi Arabia)
var config = new WordConfig { DocumentCulture = "th-TH" }; // Thai (Thailand)
var config = new WordConfig { DocumentCulture = "ja-JP" }; // Japanese (Japan)