.NET 6 以上でのセットアップ
Startup.cs ファイル
.NET Core プロジェクトの Startup.cs ファイル内で、これらのコード行は MapWhen メソッドを使用したミドルウェア パイプライン構成を定義します。 提供された述語は、受信リクエストのパスが DocImage.axd で終わるかどうかをチェックします。このブロック内では、UseDoconut メソッドが DoconutOptions オブジェクトを引数として呼び出されます。
app.MapWhen(
ctx => ctx.Request.Path.Value?.EndsWith("DocImage.axd", StringComparison.OrdinalIgnoreCase) == true,
branch => branch.UseDoconut());Doconut 名前空間を追加
プロジェクトに、以下の using 文を追加します:
using Doconut;
using Doconut.Configs;
using Doconut.Clouds;Viewer オブジェクトをインスタンス化
この行は Viewer クラスの新しいインスタンスを生成し、コード内で viewer オブジェクトを作成できるようにします。
// License is configured via AddDoconut; obtain the viewer from DI.
var docViewer = app.Services.GetRequiredService<Viewer>();DocOptions オブジェクトを設定
特定の構成で DocOptions オブジェクトを初期化する例:
```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
};
```config オブジェクトを設定
PDF ドキュメントの処理のための特定の構成で新しい PdfConfig オブジェクトを初期化します:
var config = new PdfConfig { DefaultRender = true, ExtractHyperlinks = true };ドキュメントを開く
この行は viewer オブジェクトの OpenDocument メソッドを呼び出し、サーバー上にあるドキュメントファイルのパスと、事前に構成された PdfConfig および DocOptions オブジェクトをパラメータとして渡します。
var token = await viewer.OpenDocumentAsync(path, new PdfConfig
{
AllowSearch = true,
AllowCopy = true,
ImageResolution = 150
});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 を初期化するには、次の行を使用します:
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 });
});手動デフォルトライセンスを追加
viewer を初期化する際に、単一のデフォルトライセンスファイルへのパスを手動で指定できます。
builder.Services.AddDoconut(options =>
{
options.LicensePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "Doconut.Viewer.lic");
});ストリームでライセンスを追加
このメソッドは Viewer.DoconutLicense(Stream licenseStream) のオーバーロードを使用して、ストリームでライセンスを追加します。
builder.Services.AddDoconut(options =>
{
options.LicensePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "Doconut.Viewer.lic");
});XML ドキュメントでライセンスを追加
このメソッドは Viewer.DoconutLicense(XMLDocument licence) のオーバーロードを使用して、XML ドキュメントでライセンスを追加します。
// 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") ?? "";
});手動デフォルト複数プラグインライセンス
複数のライセンスを指定する必要がある場合、viewer の初期化時にファイルパスのリストを提供できます。
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();ストリームでプラグインライセンスを追加
このメソッドは Viewer.SetLicensePlugin(Stream pluginLicence) のオーバーロードを使用して、ストリームでプラグインライセンスを追加します。
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;
});XML ドキュメントでプラグインライセンスを追加
このメソッドは Viewer.SetLicensePlugin(XMLDocument xmlPluginLicence) のオーバーロードを使用して、XML ドキュメントでプラグインライセンスを追加します。
builder.Services.AddDoconut(options =>
{
options.LicensePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "Doconut.Viewer.lic");
});必要な 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. アノテーションをマネージャに追加し、ドキュメントビューアでロードして表示します。
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);JavaScript の場合
1. YourController が利用可能であることを確認します。 2. SetNote で画像パスを設定し、新しい ImageAnnotation を作成します。 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)