查看器

主要文档查看器类

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

Viewer 不持有每请求的状态,并且有意 不 实现 IDisposable:文档会话在会话缓存中独立存在,因此释放服务永远不会拆除已打开的文档(参见 核心概念 → 查看器工作原理)。

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)需要每种格式的渲染选项(PdfConfig、WordConfig,…)
Task<string> OpenDocumentAsync(Stream stream, FileInfo fileInfo, BaseConfig? config = null, DocOptions? options = null, CancellationToken ct = default)文档不是磁盘上的文件(上传、数据库、Blob)。fileInfo 必须携带正确的扩展名——它决定格式检测
csharp
// 简单打开
string token = await viewer.OpenDocumentAsync(path);

// 带有每种格式的配置和选项
token = await viewer.OpenDocumentAsync(
    path,
    new PdfConfig { AllowSearch = true, AllowCopy = true },
    new DocOptions { TimeOut = 30 });

// 来自上传
await using var ms = new MemoryStream();
await file.CopyToAsync(ms);
ms.Position = 0;
token = await viewer.OpenDocumentAsync(ms, new FileInfo(file.FileName));

需要处理的异常:

  • LicenseException — 找到的许可证被拒绝(消息包含拒绝原因),或该格式需要已不再授予的插件功能。日历到期且没有拒绝消息时,会降级为带水印的渲染而不是抛出异常。
  • FormatNotSupportedException — Document 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 全局控制(参见 核心概念 → 会话与安全)。

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

类型属性默认值描述
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
    });
字段示例含义
前置 ^^可选的全角布局。若未使用,则使用普通水印放置方式。
文本Confidential在每页上渲染的文本。不能为空。
颜色Red绘图层可识别的命名颜色。
字体大小24字体大小;无效的数值输入将回退到渲染器默认值。
字体名称Verdana请求的字体族。确保已在部署环境中安装。
不透明度800 到 255 的字节值。必须能够成功解析。
角度-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 + 查看器 + 搜索 + 注释
string ReferenceScripts(ScriptConfig? config = null)

CssConfig 标志: IncludeBootstrapCss, IncludeViewerCss, IncludeSearchCss(受搜索限制),IncludeAnnotationCss(受批注限制)。

ScriptConfig 标志: IncludeJQuery(所有其他标志所需),IncludeBootstrap, IncludeViewerScripts(核心:docViewer.js + splitter + links),IncludeSearchScripts 和 IncludeSearchBar(受搜索限制),IncludeAnnotationScripts 和 IncludeAnnotationBar(受批注限制)。

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 }))

GetWorksheetNames

text
IReadOnlyList<string> GetWorksheetNames(string token)

返回打开的电子表格的工作表名称,按工作簿顺序排列;如果打开的文档不是电子表格,则返回空列表。

已添加于 26.9.0.

此页面对您有帮助吗?