在 .NET 6 或更高版本中设置
Startup.cs 文件
在 .NET Core 项目的 Startup.cs 文件中,这些代码行使用 MapWhen 方法定义了中间件管道配置。 提供的谓词检查传入的请求路径是否以 DocImage.axd 结尾。在此块内,调用 UseDoconut 方法并传入一个 DoconutOptions 对象作为参数。
app.MapWhen(
context => context.Request.Path.ToString().EndsWith("DocImage.axd"),
appBranch =>
{
appBranch.UseDoconut(new DoconutOptions { UnSafeMode = false, ShowDoconutInfo = false });
}
);添加 Doconut 命名空间
在项目中,添加以下 using 声明:
using Doconut;
using Doconut.Configs;
using Doconut.Formats;
using Doconut.Clouds;实例化 Viewer 对象
此行实例化 Viewer 类的一个新实例,允许在代码中创建 viewer 对象。
var docViewer = new Viewer(_cache, _accessor, licenseFilePath);设置 DocOptions 对象
使用特定配置初始化 DocOptions 对象的示例:
var documentOptions = new DocOptions
{
Password = "",
ImageResolution = 200,
TimeOut = 30
};设置配置对象
初始化一个新的 PdfConfig 对象,以特定配置处理 PDF 文档:
var config = new PdfConfig { DefaultRender = true, ExtractHyperlinks = true };打开您的文档
此行调用 viewer 对象的 OpenDocument 方法,传入位于服务器上的文档文件路径以及先前配置的 PdfConfig 和 DocOptions 对象作为参数。
var token = docViewer.OpenDocument(pathToFile, config, documentOptions);在 HTML 中添加 viewer
<div id="div_ctlDoc"></div>添加 CSS 和 JavaScript 文件
在页面中添加以下 CSS 和 JavaScript 文件。这些脚本已随代码示例的 ZIP 文件一起提供。
<!-- 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>向项目添加许可证
自动许可证检测
要使用自动许可证检测初始化 docViewer,请使用以下行:
var docViewer = new Viewer(_cache, _accessor);添加手动默认许可证
初始化 viewer 时,您可以手动指定单个默认许可证文件的路径。
var licenseFilePath = Path.Combine(_hostingEnvironment.WebRootPath, "path/to/your/license");
var docViewer = new Viewer(_cache, _accessor, licenseFilePath);通过流添加许可证
此方法使用 Viewer.DoconutLicense(Stream licenseStream) 重载,通过流添加许可证。
using (var licenseStream = new FileStream("path/to/your/license", FileMode.Open))
{
Viewer.DoconutLicense(licenseStream);
}通过 XML 文档添加许可证
此方法使用 Viewer.DoconutLicense(XMLDocument licence) 重载,通过 XML 文档添加许可证。
var xmlDoc = new XmlDocument();
xmlDoc.Load("path/to/your/license.xml");
Viewer.DoconutLicense(xmlDoc);手动默认多个插件许可证
如果需要指定多个许可证,您可以在 viewer 初始化时提供文件路径列表。
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) 重载,通过流添加插件许可证。
using (var pluginLicenseStream = new FileStream("path/to/your/plugin-license.lic", FileMode.Open))
{
Viewer.SetLicensePlugin(pluginLicenseStream);
}通过 XML 文档添加插件许可证
此方法使用 Viewer.SetLicensePlugin(XMLDocument xmlPluginLicence) 重载,通过 XML 文档添加插件许可证。
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
Render Configuration Options
您可以使用以下选项配置渲染行为:
- 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 文件。
Render Options Configuration
渲染选项通过 DefaultRender 属性进行配置:
- DefaultRender = true:此选项提供更高质量的渲染,但会消耗更多服务器内存和处理时间。
- DefaultRender = false:此选项提供更快的渲染,但以略微降低的质量为代价。
如何添加图片注释
C# 示例
1. 打开文档并获取第一页的尺寸。 2. 使用页面尺寸初始化 AnnotationManager。 3. 创建 ImageAnnotation,在 imgUrl 中设置图片路径,并在 Rectangle 中指定位置和大小。 4. 将注释添加到管理器并在文档查看器中加载以显示。
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. 添加并绘制注释,以在文档上渲染它。
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 文档时特别有用。
var config = new WordConfig
{
FontFolders = new string[] { "C:\\CustomFonts", "D:\\SharedFonts" }
};Doconut 25.2.0 的重要更新
在所有项目中将 System.Text.Json 包更新到 8.0.5 版是必需的。此更新对于确保兼容最新功能以及解决重要的安全改进至关重要。
电子邮件编码
EmailConfig 类中的 EmailEncoding 属性指定电子邮件主题和正文的首选编码。
var emailConfig = new EmailConfig {
EmailEncoding = Encoding.UTF8 // Set preferred email encoding
};主题 & 正文编码
EmailConfig 类中的 SubjectEncoding 和 BodyEncoding 属性分别定义电子邮件主题和正文使用的编码。
var emailConfig = new EmailConfig {
SubjectEncoding = Encoding.UTF8, // Set encoding for the subject
BodyEncoding = Encoding.UTF8 // Set encoding for the body
};Word Viewer 语言支持
WordConfig 类中的 DocumentCulture 属性允许显式定义 DOC 和 DOCX 文件的语言区域设置。这确保本地化内容(如日期、货币和数字)如在 Microsoft Word 中一样呈现,尤其对具有特定格式要求的语言非常有用。
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 包: