Installation i .NET Standard / Core

Startup.cs-fil

Inom Startup.cs-filen i ett .NET Core-projekt definierar dessa kodrader en middleware-pipelinekonfiguration med hjälp av MapWhen-metoden. Det angivna predikatet kontrollerar om den inkommande begärans sökväg slutar med DocImage.axd. Inuti detta block anropas UseDoconut-metoden med ett DoconutOptions-objekt som argument.

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

Lägg till Doconut-namespace

I ditt projekt, lägg till följande using-satser:

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

// TODO: implement licence loading, viewer registration, document opening, conversion using current API

Instansiera Viewer-objekt

Denna rad skapar en ny instans av Viewer-klassen, vilket gör att du kan skapa ett viewer-objekt i din kod.

Instantiation
// Viewer is injected; no explicit construction required in Doconut.NETStandard 26.9.0

Konfigurera ett DocOptions-objekt

Exempel på initiering av ett DocOptions-objekt med specifika konfigurationer:

DocOptions
var documentOptions = new DocOptions
{
    Password = "",
    // ImageResolution is obsolete; omitted.
    TimeOut = 30
};

Konfigurera ett konfigurationsobjekt

Initiera ett nytt PdfConfig-objekt med specifika konfigurationer för hantering av PDF-dokument:

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

Öppna ditt dokument

Denna rad anropar OpenDocument-metoden på viewer-objektet och skickar sökvägen till dokumentfilen som ligger på servern samt de tidigare konfigurerade PdfConfig- och DocOptions-objekten som parametrar.

OpenDocument
dynamic viewer = null;
var token = viewer.LoadDocument(path, null, null);

Lägg till viewer i din HTML

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

Lägg till CSS- och JavaScript-filer

Lägg till följande CSS- och JavaScript-filer på sidan. Dessa skript ingår i ZIP-filen med kodexempel.

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>

Lägg till licenser i ditt projekt

Automatisk licensdetektering

För att initiera docViewer med automatisk licensdetektering, använd följande rad:

C#
```csharp
// Create a Viewer instance using the current API (no constructor arguments required).
var docViewer = new Viewer();
```

Lägg till manuell standardlicens

Du kan manuellt ange sökvägen till en enda standardlicensfil när du initierar viewer.

C#
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>();

Lägg till licens via ström

Denna metod använder Viewer.DoconutLicense(Stream licenseStream)-överladdning för att lägga till en licens via en ström.

C#
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) };
}

Lägg till licens via XML-dokument

Denna metod använder Viewer.DoconutLicense(XMLDocument licence)-överladdning för att lägga till en licens via ett XML-dokument.

C#
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)

Manuell standard för flera plugin-licenser

Om du behöver ange flera licenser kan du tillhandahålla en lista med filsökvägar till viewer-initialiseringen.

C#
```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);
```

Lägg till plugin-licens via ström

Denna metod använder Viewer.SetLicensePlugin(Stream pluginLicence)-överladdning för att lägga till en plugin-licens via en ström.

C#
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.
}

Lägg till plugin-licens via XML-dokument

Denna metod använder Viewer.SetLicensePlugin(XMLDocument xmlPluginLicence)-överladdning för att lägga till en plugin-licens via ett XML-dokument.

C#
// 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);

Nödvändiga NuGet-paket

För att säkerställa att ditt projekt är fullt kompatibelt med .NET Core Standard måste följande NuGet-paket inkluderas, listade i alfabetisk ordning.

  • 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

Renderingskonfigurationsalternativ

Du kan konfigurera renderingsbeteendet med följande alternativ:

  • CadConfig: Används för DWG-, DXF- och DGN-filer.
  • EmailConfig: Används för EML-, EMLX- och MSG-filer.
  • EpubConfig: Används för EPUB-filer.
  • ExcelConfig: Används för Excel- och CSV-filer.
  • MhtConfig: Används för MHT-filer.
  • PptConfig: Används för PowerPoint-filer.
  • ProjectConfig: Används för MPP-filer.
  • PsdConfig: Används för PSD-filer.
  • TxtConfig: Används för TXT-filer.
  • TiffConfig: Används för TIFF-filer.

Renderingsalternativskonfiguration

Renderingsalternativen konfigureras med hjälp av egenskapen DefaultRender:

  • DefaultRender = true : Detta alternativ ger rendering av högre kvalitet men använder mer serverminne och processortid.
  • DefaultRender = false : Detta alternativ ger snabbare rendering på bekostnad av något reducerad kvalitet.

Hur man lägger till en bildannotation

I C#

1. Öppna dokumentet och hämta dimensionerna för den första sidan. 2. Initiera AnnotationManager med sidans dimensioner. 3. Skapa en ImageAnnotation, ange bildens sökväg i imgUrl, och specificera position och storlek inom Rectangle. 4. Lägg till annotationen i manager och ladda den i dokumentvisaren för att visa den.

C#
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.

I JavaScript

1. Säkerställ att YourController är tillgänglig. 2. Skapa en ny ImageAnnotation, ange bildens sökväg med SetNote. 3. Lägg till och måla annotationen för att rendera den på dokumentet.

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

Anpassade teckensnitt i Word-dokument

FontFolders-egenskapen i WordConfig-klassen specificerar anpassade kataloger där applikationen kommer att söka efter teckensnitt. Detta är särskilt användbart för att bearbeta Word-dokument som förlitar sig på teckensnitt som inte är installerade i systemets standardteckensnittskataloger.

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

Viktig uppdatering för Doconut 25.2.0

Det är viktigt att uppdatera System.Text.Json-paketet till version 8.0.5 i alla dina projekt. Denna uppdatering är avgörande för att säkerställa kompatibilitet med de senaste funktionerna och för att åtgärda viktiga säkerhetsförbättringar.

E-postkodning

EmailEncoding-egenskapen i EmailConfig-klassen specificerar den föredragna kodningen för både e-postens ämne och brödtext.

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

Ämnes- och brödtextkodning

SubjectEncoding- och BodyEncoding-egenskaperna i EmailConfig-klassen definierar den kodning som används för respektive e-postämne och brödtext.

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

Stöd för kultur i Word-visare

Egenskapen DocumentCulture i WordConfig-klassen låter dig explicit definiera kulturen för DOC- och DOCX-filer. Detta säkerställer att lokalt innehåll—såsom datum, valutor och siffror—renderas exakt som i Microsoft Word. Detta är särskilt användbart för språk med specifika formateringskrav.

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)