在 .NET Standard / Core 中设置

Startup.cs 文件

在 .NET Core 项目的 Startup.cs 文件中,这些代码行使用 MapWhen 方法定义了一个中间件管道配置。 提供的谓词检查传入的请求路径是否以 DocImage.axd 结尾。在此块中,调用 UseDoconut 方法并传入一个 DoconutOptions 对象作为参数。

Startup.cs
app.MapWhen(
    context => context.Request.Path.ToString().EndsWith("DocImage.axd"),
    appBranch =>
    {
        appBranch.UseDoconut(new DoconutOptions { UnSafeMode = false, ShowDoconutInfo = false });
    }
);

添加 Doconut 命名空间

在您的项目中,添加以下 using 语句:

Usings
using Doconut;
using Doconut.Configs;
using Doconut.Formats;
using Doconut.Clouds;

实例化 Viewer 对象

此行实例化 Viewer 类的新实例,允许您在代码中创建 viewer 对象。

Instantiation
var docViewer = new Viewer(_cache, _accessor, licenseFilePath);

设置 DocOptions 对象

使用特定配置初始化 DocOptions 对象的示例:

DocOptions
var documentOptions = new DocOptions
{
    Password = "",
    ImageResolution = 200,
    TimeOut = 30
};

设置配置对象

初始化一个新的 PdfConfig 对象,用于处理 PDF 文档的特定配置:

PdfConfig
var config = new PdfConfig { DefaultRender = true, ExtractHyperlinks = true };

打开文档

此行调用 viewer 对象的 OpenDocument 方法,传入服务器上文档文件的路径以及先前配置的 PdfConfig 和 DocOptions 对象作为参数。

OpenDocument
var token = docViewer.OpenDocument(pathToFile, config, documentOptions);

在 HTML 中添加 viewer

HTML
<div id="div_ctlDoc"></div>

添加 CSS 和 JavaScript 文件

在页面中添加以下 CSS 和 JavaScript 文件。这些脚本包含在带有代码示例的 ZIP 文件中。

HTML Headers
<link rel="stylesheet" href="css/viewer.css" />

<!-- Main Viewer Related Scripts -->
<script src="scripts/jquery.min.js"></script>
<script src="scripts/docViewer.js"></script>
<script src="scripts/splitter.js"></script>
<script src="scripts/search.js"></script>
<script src="scripts/documentLinks.js"></script>

<!-- Viewer functions -->
<script src="scripts/docViewer.UI.js?v=1"></script>

向项目添加许可证

自动许可证检测

要使用自动许可证检测初始化 docViewer,请使用以下行:

C#
var docViewer = new Viewer(_cache, _accessor);

添加手动默认许可证

在初始化 viewer 时,您可以手动指定单个默认许可证文件的路径。

C#
var licenseFilePath = Path.Combine(_hostingEnvironment.WebRootPath, "path/to/your/license");
var docViewer = new Viewer(_cache, _accessor, licenseFilePath);

通过流添加许可证

此方法使用 Viewer.DoconutLicense(Stream licenseStream) 重载,通过流添加许可证。

C#
using (var licenseStream = new FileStream("path/to/your/license", FileMode.Open))
{
    Viewer.DoconutLicense(licenseStream);
}

通过 XML 文档添加许可证

此方法使用 Viewer.DoconutLicense(XMLDocument licence) 重载,通过 XML 文档添加许可证。

C#
var xmlDoc = new XmlDocument();
xmlDoc.Load("path/to/your/license.xml");
Viewer.DoconutLicense(xmlDoc);

手动默认多个插件许可证

如果需要指定多个许可证,您可以在 viewer 初始化时提供文件路径列表。

C#
List<string> licenseFilePaths = new List<string>
{
    Path.Combine(_hostingEnvironment.WebRootPath, "Doconut.Viewer.lic"),
    Path.Combine(_hostingEnvironment.WebRootPath, "Doconut.Viewer.Annotation.lic"),
    Path.Combine(_hostingEnvironment.WebRootPath, "Doconut.Viewer.Search.lic")
};
var docViewer = new Viewer(_cache, _accessor, licenseFilePaths);

通过流添加插件许可证

此方法使用 Viewer.SetLicensePlugin(Stream pluginLicence) 重载,通过流添加插件许可证。

C#
using (var pluginLicenseStream = new FileStream("path/to/your/plugin-license.lic", FileMode.Open))
{
    Viewer.SetLicensePlugin(pluginLicenseStream);
}

通过 XML 文档添加插件许可证

此方法使用 Viewer.SetLicensePlugin(XMLDocument xmlPluginLicence) 重载,通过 XML 文档添加插件许可证。

C#
var pluginXmlDoc = new XmlDocument();
pluginXmlDoc.Load("path/to/your/plugin-license.xml");
Viewer.SetLicensePlugin(pluginXmlDoc);

必需的 NuGet 包

为了确保您的项目完全兼容 .NET Core Standard,需要包含以下按字母顺序列出的 NuGet 包。

  • 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

渲染配置选项

您可以使用以下选项配置渲染行为:

  • CadConfig:用于 DWG、DXF 和 DGN 文件。
  • EmailConfig:用于 EML、EMLX 和 MSG 文件。
  • EpubConfig:用于 EPUB 文件。
  • ExcelConfig:用于 Excel 和 CSV 文件。
  • MhtConfig:用于 MHT 文件。
  • PptConfig:用于 PowerPoint 文件。
  • ProjectConfig:用于 MPP 文件。
  • PsdConfig:用于 PSD 文件。
  • TxtConfig:用于 TXT 文件。
  • TiffConfig:用于 TIFF 文件。

渲染选项配置

渲染选项使用 DefaultRender 属性进行配置:

  • DefaultRender = true :此选项提供更高质量的渲染,但会使用更多服务器内存和处理时间。
  • DefaultRender = false :此选项提供更快的渲染速度,代价是略微降低的质量。

如何添加图片批注

在 C# 中

1. 打开文档并获取第一页的尺寸。 2. 使用页面尺寸初始化 AnnotationManager。 3. 创建 ImageAnnotation,在 imgUrl 中设置图片路径,并在 Rectangle 中指定位置和大小。 4. 将批注添加到管理器并在文档查看器中加载以显示。

C#
var token = docViewer.OpenDocument(pathToFile, config, documentOptions);

var firstPage = docViewer.GetThumbnailDimensions(1, 0, 100, false);
var pageWidth = firstPage.Width;
var pageHeight = firstPage.Height;

using (var annMgr = docViewer.GetAnnotationManager(pageWidth, pageHeight))
{
    string imgUrl = "img/sample.png";
    var imgAnn = new ImageAnnotation(
        1,
        new Rectangle(600, 700, 750, 300),
        imgUrl
    );

    annMgr.Add(imgAnn);
    docViewer.LoadAnnotationXML(annMgr.GetAnnotationXml());
}

在 JavaScript 中

1. 确保 YourController 可用。 2. 创建新的 ImageAnnotation,使用 SetNote 设置图片路径。 3. 添加并绘制批注,以在文档上渲染它。

JavaScript
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();
    }
}

Word 文档中的自定义字体

WordConfig 类中的 FontFolders 属性指定了应用程序搜索字体的自定义目录。这对于处理系统默认字体目录未安装的字体的 Word 文档特别有用。

Usage
var config = new WordConfig
{
    FontFolders = new string[] { "C:\\CustomFonts", "D:\\SharedFonts" }
};

重要更新 Doconut 25.2.0

在所有项目中将 System.Text.Json 包更新到 8.0.5 版本是必需的。此更新对于确保兼容最新功能并解决重要的安全改进至关重要。

电子邮件编码

EmailConfig 类中的 EmailEncoding 属性指定了电子邮件主题和正文的首选编码。

Usage
var emailConfig = new EmailConfig {
    EmailEncoding = Encoding.UTF8 // Set preferred email encoding
};

主题 & 正文编码

EmailConfig 类中的 SubjectEncoding 和 BodyEncoding 属性分别定义了电子邮件主题和正文使用的编码。

Usage
var emailConfig = new EmailConfig {
    SubjectEncoding = Encoding.UTF8, // Set encoding for the subject
    BodyEncoding = Encoding.UTF8     // Set encoding for the body
};

Word 查看器区域设置支持

WordConfig 类中的 DocumentCulture 属性允许您显式定义 DOC 和 DOCX 文件的区域设置。这样可以确保本地化内容—如日期、货币和数字—的呈现方式与 Microsoft Word 完全一致,尤其适用于具有特定格式要求的语言。

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

下载所需的 CSS 和 JS 文件

为了确保 Doconut 正常工作,您需要在项目中包含特定的 CSS 和 JS 文件。您可以使用以下链接下载包含所有必要文件的 ZIP 包: