查看器

主要文档查看器类

Viewer(命名空间 Doconut)是用于从 Razor 页面、MVC 控制器、Blazor 组件或最小 API 打开文档的公共入口点。它是 sealed,通过 AddDoconut() 注册为 transient 服务,并通过构造函数注入解析——绝不要直接实例化它。

Viewer 不持有每请求的状态,并且有意 实现 IDisposable:文档会话独立存在于会话缓存中,销毁服务永远无法关闭已打开的文档(参见 Core Concepts → How the Viewer Works)。

OpenDocumentAsync(打开文档)

打开文档并返回客户端小部件在所有后续请求中使用的会话令牌。

重载使用场景
Task<string> OpenDocumentAsync(string filePath, DocOptions? options = null, CancellationToken ct = default)从磁盘打开,自动检测格式并使用该格式的默认配置
Task<string> OpenDocumentAsync(string filePath, BaseConfig? config, DocOptions? options = null, CancellationToken ct = default)需要每种格式的渲染选项 (PdfConfigWordConfig …)
Task<string> OpenDocumentAsync(Stream stream, FileInfo fileInfo, BaseConfig? config = null, DocOptions? options = null, CancellationToken ct = default)文档不是磁盘上的文件(上传、数据库、Blob)。fileInfo 必须携带正确的扩展名——它用于格式检测
csharp
// Simple open
string token = await viewer.OpenDocumentAsync(path);

// With per-format config and options
token = await viewer.OpenDocumentAsync(
    path,
    new PdfConfig { AllowSearch = true, AllowCopy = true },
    new DocOptions { TimeOut = 30 });

// From an upload
await using var ms = new MemoryStream();
await file.CopyToAsync(ms);
ms.Position = 0;
token = await viewer.OpenDocumentAsync(ms, new FileInfo(file.FileName));

需要捕获的异常:

  • LicenseException — 找到的许可证被拒绝(消息中包含拒绝原因),或格式需要的插件功能已不再授权。日历过期且没有拒绝信息时,会降级为带水印的渲染而不是抛异常。
  • FormatNotSupportedExceptionDocument format '<extension>' is not supported.
  • InvalidDataException — 文件内容损坏或与其扩展名不匹配。

CloseDocument(关闭文档)

text
void CloseDocument(string token)

从缓存中移除会话(立即释放文档引擎),删除安全标记,并撤销访问授权。可选——滑动过期会执行相同的清理——但对大文档仍建议显式调用。

GetPageCount(获取页数)

text
int GetPageCount(string token)

打开会话的总页数。如果令牌未知或已过期则抛异常。

DocOptions(文档选项)

每次打开时的、与格式无关的选项(命名空间 Doconut):

类型属性默认值描述
stringPassword""受保护文档的密码(自动复制到格式配置中)。
intImageResolution0已废弃。 仅为兼容性保留——请在格式配置上设置 ImageResolution
stringWatermark""在渲染页面上绘制的自定义水印文本。格式字符串:"^Text~Color~FontSize~FontName~Opacity~Angle",例如 "^Sample Copy~Red~24~Verdana~80~-45"
intTimeOut60会话滑动过期时间(分钟)。
boolIsSecuredtrue当前未强制执行 — 预留。令牌绑定由全局的 DoconutOptions.UnsafeMode 控制(参见 Core Concepts → Sessions & Security)。

该类还公开了一些有意超出普通单机查看流程的专用属性:

类型属性默认值描述
boolIsWebFarmfalse将打开操作标记为 Web 农场场景。仅在配套的共享存储/会话架构下使用。
stringWebFarmPath""专用 Web 农场工作流使用的共享路径。普通单机查看器中为空。
boolEditModefalse为单独分发的编辑器工作流预留;标准查看器请保持 false

自定义水印

DocOptions.Watermark 使用六个波浪号分隔的字段。可选的前导 ^ 请求全角布局:

text
^Text~Color~FontSize~FontName~Opacity~Angle
csharp
string token = await viewer.OpenDocumentAsync(
    path,
    new PdfConfig(),
    new DocOptions
    {
        Watermark = "^Confidential~Red~24~Verdana~80~-45",
        TimeOut = 30
    });
字段示例含义
前导 ^^可选的全角布局。若省略,则使用普通水印位置。
TextConfidential渲染在每页上的文字。不能为空。
ColorRed绘图层可识别的命名颜色。
FontSize24字体大小;数值无效时回退到渲染器默认值。
FontNameVerdana请求的字体族。请确保部署环境已安装该字体。
Opacity800 到 255 的字节值。必须能够成功解析。
Angle-45以度为单位的旋转角度;数值无效时回退到默认值。

解析器在可选 ^ 之后恰好期待六个字段。定义无效时会被 SDK 的可见 Invalid Watermark 替代,而不是静默消失。

许可证决策

许可证状态提供的自定义值渲染结果
有效的付费查看器许可证干净页面
有效的付费查看器许可证自定义水印
活跃的临时/演示基础查看器干净的基础查看器页面
活跃的临时/演示基础查看器当使用干净的基础查看器路径时显示自定义水印
缺失、被拒绝、已过期、版本错误或域名无效的许可证任意强制/评估水印;自定义值不会覆盖它
插件渲染处于评估规则下任意评估水印

相同的决策同样适用于提供的页面图像和注释导出。动画 GIF 输出会逐帧加盖水印。因此,自定义水印是受许可证约束的功能,而不是用来替代或抑制评估水印的手段。

注释 API

服务器端注释加载与导出。完整教程位于 Guides → Annotations;接口如下:

成员目的
AnnotationManager GetAnnotationManager(string token)绑定到打开会话页面尺寸的管理器
AnnotationManager GetAnnotationManager(string token, int pageWidth, int pageHeight)具有显式页面尺寸的管理器
AnnotationManager GetAnnotationManager(int pageWidth, int pageHeight)与会话无关的管理器
void LoadAnnotationData(string token, AnnotationManager manager)将 C# 中构建的注释加载到会话中
void LoadAnnotationData(string token, string annotationData)AnnotationManager.GetAnnotationData() 返回的编码页面/Base64 包中加载注释
void LoadAnnotationXML(string token, XmlDocument annotationXml)从 XML 加载注释
XmlDocument GetAnnotationXML(string token)将会话的注释导出为 XML
Task<byte[]> ExportAnnotationsToPdfAsync(string token, int zoom = 100, CancellationToken ct = default)带有注释的 PDF(已嵌入)
Task<int> ExportAnnotationsToPngAsync(…)带有注释的 PNG 文件(已嵌入)
Task<byte[]> ExportAnnotationsToPngZipAsync(string token, int zoom = 100, CancellationToken ct = default)每页 PNG(带注释)压缩为 ZIP

DICOM 元数据

text
Task<DicomMetadata?> GetDicomMetadataAsync(string token, CancellationToken ct = default)

返回通过 DICOM 插件打开的会话的 DICOM 标记元数据;非 DICOM 文档返回 null

资源助手 — ReferenceCss / ReferenceScripts

UseDoconutResources() 提供的嵌入资源生成 <link>/<script> 标签,按正确的依赖顺序输出。搜索、注释等受许可证限制的功能仅在许可证启用时才会打包输出,保持客户端 UI 与服务器行为一致。

text
string ReferenceCss(CssConfig? config = null)      // null → Bootstrap + viewer + search + annotation
string ReferenceScripts(ScriptConfig? config = null)

CssConfig 标志: IncludeBootstrapCssIncludeViewerCssIncludeSearchCss(受搜索许可证限制)、IncludeAnnotationCss(受注释许可证限制)。

ScriptConfig 标志: IncludeJQuery(所有其他脚本均依赖)、IncludeBootstrapIncludeViewerScripts(核心:docViewer.js + splitter + links)、IncludeSearchScriptsIncludeSearchBar(受搜索许可证限制)、IncludeAnnotationScriptsIncludeAnnotationBar(受注释许可证限制)。

html
@inject Doconut.Viewer Viewer

@Html.Raw(Viewer.ReferenceCss(new CssConfig { IncludeBootstrapCss = true, IncludeViewerCss = true }))
@Html.Raw(Viewer.ReferenceScripts(new ScriptConfig { IncludeJQuery = true, IncludeViewerScripts = true }))

此页面有帮助吗?