ตั้งค่าใน .NET Standard / Core
ไฟล์ Startup.cs
ภายในไฟล์ Startup.cs ของโครงการ .NET Core นี้ บรรทัดโค้ดเหล่านี้กำหนดการกำหนดค่าท่อกลางการทำงานโดยใช้เมธอด MapWhen. เงื่อนไขที่ให้มาตรวจสอบว่าพาธของคำขอที่เข้ามาจบด้วย DocImage.axd หรือไม่ ภายในบล็อกนี้ เมธอด UseDoconut จะถูกเรียกด้วยอ็อบเจกต์ DoconutOptions เป็นอาร์กิวเมนต์.
app.MapWhen(
context => context.Request.Path.ToString().EndsWith("DocImage.axd"),
appBranch =>
{
appBranch.UseDoconut(new DoconutOptions { UnsafeMode = false, ShowDoconutInfo = false });
}
);เพิ่ม Namespaces ของ Doconut
ในโครงการของคุณ ให้เพิ่มคำสั่ง using ต่อไปนี้:
using Doconut;
using Doconut.Configs;
using Doconut.Clouds;
// TODO: implement licence loading, viewer registration, document opening, conversion using current APIสร้างอ็อบเจกต์ Viewer
บรรทัดนี้สร้างอินสแตนซ์ใหม่ของคลาส Viewer ทำให้คุณสามารถสร้างอ็อบเจกต์ viewer ภายในโค้ดของคุณได้.
// Viewer is injected; no explicit construction required in Doconut.NETStandard 26.9.0ตั้งค่าอ็อบเจกต์ DocOptions
ตัวอย่างการเริ่มต้นอ็อบเจกต์ DocOptions ด้วยการกำหนดค่าที่เฉพาะเจาะจง:
var documentOptions = new DocOptions
{
Password = "",
// ImageResolution is obsolete; omitted.
TimeOut = 30
};ตั้งค่าอ็อบเจกต์ config
เริ่มต้นอ็อบเจกต์ PdfConfig ใหม่ด้วยการกำหนดค่าที่เฉพาะเจาะจงสำหรับการจัดการเอกสาร PDF:
var config = new PdfConfig { DefaultRender = true, ExtractHyperlinks = true };เปิดเอกสารของคุณ
บรรทัดนี้เรียกเมธอด OpenDocument ของอ็อบเจกต์ viewer โดยส่งพาธของไฟล์เอกสารที่อยู่บนเซิร์ฟเวอร์และอ็อบเจกต์ PdfConfig และ DocOptions ที่กำหนดค่าก่อนหน้านี้เป็นพารามิเตอร์.
dynamic viewer = null;
var token = viewer.LoadDocument(path, null, null);เพิ่ม viewer ใน HTML ของคุณ
<div id="div_ctlDoc"></div>เพิ่มไฟล์ CSS และ JavaScript
เพิ่มไฟล์ CSS และ JavaScript ต่อไปนี้ในหน้าไฟล์ สคริปต์เหล่านี้รวมอยู่ในไฟล์ ZIP พร้อมตัวอย่างโค้ด.
<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 ด้วยการตรวจจับใบอนุญาตอัตโนมัติ ให้ใช้บรรทัดต่อไปนี้:
```csharp
// Create a Viewer instance using the current API (no constructor arguments required).
var docViewer = new Viewer();
```เพิ่มใบอนุญาตเริ่มต้นแบบแมนนวล
คุณสามารถระบุพาธไปยังไฟล์ใบอนุญาตเริ่มต้นเดียวแบบแมนนวลเมื่อเริ่มต้น viewer.
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Doconut;
var licenseFilePath = Path.Combine(builder.Environment.WebRootPath, "path/to/your/license");
var docViewer = app.Services.GetRequiredService<Viewer>();เพิ่มใบอนุญาตโดย Stream
เมธอดนี้ใช้ Viewer.DoconutLicense(Stream licenseStream) overload เพื่อเพิ่มใบอนุญาตโดยใช้สตรีม.
using System.IO;
using Doconut.Infrastructure.Licensing;
using (var licenseStream = new FileStream("path/to/your/license", FileMode.Open, FileAccess.Read))
{
var license = new DoconutLicense { DocBytes = new BinaryReader(licenseStream).ReadBytes((int)licenseStream.Length) };
}เพิ่มใบอนุญาตโดยเอกสาร XML
เมธอดนี้ใช้ Viewer.DoconutLicense(XMLDocument licence) overload เพื่อเพิ่มใบอนุญาตโดยใช้เอกสาร XML.
using System.Xml;
using Doconut.Infrastructure.Licensing;
var xmlDoc = new XmlDocument(); // load the license XML
xmlDoc.Load("path/to/your/license.xml");
var license = new DoconutLicense(); // instantiate license object (populate properties as needed)หลายใบอนุญาตปลั๊กอินเริ่มต้นแบบแมนนวล
หากคุณต้องการระบุหลายใบอนุญาต คุณสามารถให้รายการพาธของไฟล์แก่การเริ่มต้น viewer.
```csharp
// Prepare the paths to the viewer licence files (kept for reference; the current API loads them automatically)
var 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")
};
// The Viewer class no longer requires the licences in its constructor.
// Create an instance with the default constructor.
var docViewer = new Viewer();
// Open a document (e.g., a PDF) and obtain a token that represents the opened file.
string token = await docViewer.OpenDocumentAsync(
filePath: Path.Combine(_hostingEnvironment.WebRootPath, "sample.pdf"),
options: new DocOptions(),
ct: CancellationToken.None);
```เพิ่มใบอนุญาตปลั๊กอินโดย Stream
เมธอดนี้ใช้ Viewer.SetLicensePlugin(Stream pluginLicence) overload เพื่อเพิ่มใบอนุญาตปลั๊กอินโดยใช้สตรีม.
using (var pluginLicenseStream = new FileStream("path/to/your/plugin-license.lic", FileMode.Open))
{
// In Doconut.NETStandard 26.9.0 the license is supplied via the configuration
// passed to OpenDocumentAsync (BaseConfig), so the explicit SetLicensePlugin call
// is no longer required. Keep the license file accessible to the viewer or
// include it in your BaseConfig as shown in other samples.
}เพิ่มใบอนุญาตปลั๊กอินโดยเอกสาร XML
เมธอดนี้ใช้ Viewer.SetLicensePlugin(XMLDocument xmlPluginLicence) overload เพื่อเพิ่มใบอนุญาตปลั๊กอินโดยใช้เอกสาร XML.
// The license XML is discovered automatically by the viewer in Doconut.NETStandard 26.9.0.
// No explicit API call is required to register it.
string token = await Viewer.OpenDocumentAsync(
"path/to/your/document.pdf",
null, // DocOptions – use default settings
CancellationToken.None);แพคเกจ 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 : ตัวเลือกนี้ให้การเรนเดอร์ที่เร็วขึ้นโดยเสียคุณภาพเล็กน้อย.
วิธีเพิ่ม Image Annotation
ใน C#
1. เปิดเอกสารและรับขนาดของหน้าที่หนึ่ง. 2. เริ่มต้น AnnotationManager ด้วยขนาดหน้าดังกล่าว. 3. สร้าง ImageAnnotation โดยตั้งค่าพาธของรูปภาพใน imgUrl และระบุตำแหน่งและขนาดภายใน Rectangle. 4. เพิ่ม annotation ไปยัง manager และโหลดใน document viewer เพื่อแสดงผล.
using Doconut.Annotations;
using System.Drawing;
// Placeholder: open the document and obtain page dimensions using the current Doconut.NETStandard API.
// Placeholder: create an annotation manager for the page size.
string imgUrl = "img/sample.png";
var imgAnn = new ImageAnnotation(
1,
new Rectangle(600, 700, 750, 300),
imgUrl
);
// Placeholder: add the annotation to the manager and load the annotation XML back into the viewer using the current API.ใน JavaScript
1. ตรวจสอบให้แน่ใจว่า YourController พร้อมใช้งาน. 2. สร้าง ImageAnnotation ใหม่โดยตั้งค่าพาธของรูปภาพด้วย SetNote. 3. เพิ่มและวาด annotation เพื่อเรนเดอร์บนเอกสาร.
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
คุณสมบัติ FontFolders ในคลาส WordConfig ระบุไดเรกทอรีแบบกำหนดเองที่แอปพลิเคชันจะค้นหาแบบอักษร ซึ่งมีประโยชน์อย่างยิ่งสำหรับการประมวลผลเอกสาร Word ที่พึ่งพาแบบอักษรที่ไม่ได้ติดตั้งในไดเรกทอรีแบบอักษรเริ่มต้นของระบบ.
var config = new WordConfig
{
FontFolders = new string[] { "C:\\CustomFonts", "D:\\SharedFonts" }
};อัปเดตสำคัญสำหรับ Doconut 25.2.0
จำเป็นอย่างยิ่งที่จะอัปเดตแพคเกจ System.Text.Json เป็นเวอร์ชัน 8.0.5 ในทุกโครงการของคุณ การอัปเดตนี้สำคัญเพื่อให้เข้ากันได้กับฟีเจอร์ล่าสุดและแก้ไขการปรับปรุงด้านความปลอดภัยที่สำคัญ.
การเข้ารหัส Email
คุณสมบัติ EmailEncoding ในคลาส EmailConfig ระบุการเข้ารหัสที่ต้องการสำหรับหัวเรื่องและเนื้อหาของอีเมล.
var emailConfig = new EmailConfig {
EmailEncoding = Encoding.UTF8 // Set preferred email encoding
};การเข้ารหัสหัวเรื่องและเนื้อหา
คุณสมบัติ SubjectEncoding และ BodyEncoding ในคลาส EmailConfig กำหนดการเข้ารหัสที่ใช้สำหรับหัวเรื่องและเนื้อหาของอีเมลตามลำดับ.
var emailConfig = new EmailConfig {
SubjectEncoding = Encoding.UTF8, // Set encoding for the subject
BodyEncoding = Encoding.UTF8 // Set encoding for the body
};การสนับสนุนวัฒนธรรมของ Word Viewer
คุณสมบัติ DocumentCulture ในคลาส WordConfig ช่วยให้คุณกำหนดวัฒนธรรมสำหรับไฟล์ 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)