DocxWorkshop .NET · 0.1.13

ASP.NET Core quickstart

Embed the editor in a .NET 10 Razor Pages app. Start with the file picker and downloads, then connect your app’s document storage or integrate through Blazor.

Install the NuGet package

Install the .NET 10 SDK and create an app with Razor Pages.

PowerShell
dotnet new webapp -n DocxWorkshopDemo -f net10.0
Set-Location DocxWorkshopDemo
New-Item -ItemType Directory -Path packages

Put DocxWorkshop.AspNetCore.0.1.13.nupkg in packages, then install it from that directory.

PowerShell
dotnet add package DocxWorkshop.AspNetCore --version 0.1.13 --source ./packages

Add the editor to a Razor page

Map static assets so the app serves the editor files bundled in NuGet. In this demonstration project, replace Program.cs with the following.

Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddAntiforgery(options => options.HeaderName = "X-DocxWorkshop-Antiforgery");

var app = builder.Build();
app.UseRouting();
app.MapStaticAssets();
app.UseAntiforgery();
app.MapRazorPages();
app.Run();

Add the DocxWorkshop Tag Helper to Pages/_ViewImports.cshtml. In an MVC app, add it to the applicable Views/_ViewImports.cshtml instead.

Pages/_ViewImports.cshtml · Add this line
@addTagHelper *, DocxWorkshop.AspNetCore

Create a page with a Tag Helper and a defined editor height.

Pages/Editor.cshtml
@page

<docxworkshop-editor id="document-editor"
                  document-name="Document.docx"
                  style="--docxworkshop-height: 75vh"></docxworkshop-editor>
PowerShell
dotnet run --no-launch-profile -- --environment Development --urls http://127.0.0.1:5080

The Development environment loads the package’s static assets when you run from the project directory. For a published app, dotnet publish copies the assets into the deployment output.

Open http://127.0.0.1:5080/Editor. From File, choose Open… and select a DOCX. Edit a line, choose Save DOCX from the same menu, and reopen the saved file. Select Download PDF and search for the new text.

Connect your application’s storage

For a stored document, configure the Tag Helper with a same-origin load URL and optional save URL.

Pages/Editor.cshtml · Editor with storage URLs
@page
@{
    var url = Url.Content("~/documents/42");
}

<docxworkshop-editor id="contract-editor"
                  document-url="@url"
                  save-url="@url"
                  document-name="Contract.docx"></docxworkshop-editor>
GET /documents/42
Return the DOCX bytes. Include an ETag so the editor can identify the version it opened.
PUT /documents/42
Accept the DOCX request body after validating user access and the configured antiforgery header. When the editor sends If-Match, reject a stale version with 412. Return the new ETag after a successful save.

Implement authorization, file-size limits, and storage in your application. For templates with additional fonts, use the editor’s font registration API before PDF export.

Use the Blazor component

In an app configured for interactive server rendering, add the component to an interactive page. The C# methods call the live browser editor.

Interactive Razor component
@using DocxWorkshop.AspNetCore

<DocxWorkshopEditor @ref="editor" DocumentName="Contract.docx" />

@code {
    private DocxWorkshopEditor? editor;
}
OpenAsync(stream, fileName)
Open a stream as a document in the editor.
GetStateAsync()
Read the document title and the live editor’s revision and dirty state.
SaveAsync()
Send the edited DOCX to the configured save URL. Supply AntiforgeryHeader and AntiforgeryToken when saving through the Blazor component.
ExportDocxAsync(destination) ExportPdfAsync(destination)
Write the exported file to a .NET stream.
RegisterFontAsync(family, url)
Register font bytes served by the app. Supply the optional weight and style for bold and italic faces.

Edit the open document from C#

Call the component after its Ready event. Open a DOCX that contains Draft, then use this button to replace the first match.

Interactive Blazor page
@page "/live-editor"
@using DocxWorkshop.AspNetCore
@rendermode InteractiveServer

<button disabled="@(!ready)" @onclick="ReplaceDraftAsync">Replace Draft</button>
<p role="status">@status</p>
<DocxWorkshopEditor @ref="editor" Ready="OnEditorReady" />

@code {
    private DocxWorkshopEditor? editor;
    private bool ready;
    private string status = "";

    private void OnEditorReady() => ready = true;

    private async Task ReplaceDraftAsync()
    {
        var match = await editor!.FindTextAsync("Draft");
        if (match is null)
        {
            status = "Draft was not found.";
            return;
        }

        var updated = await editor.ReplaceTextAsync(match, "Approved");
        status = updated ? "The open document was updated." : "The document is unchanged.";
    }
}
GetTextAsync()
Read the text of the main document story. Pass story to read a different story.
FindTextAsync(text, story, offset)
Find a literal, case-sensitive match. The default search starts at the beginning of the main story. The result is a revision-bound range or null.
SelectTextAsync(range)
Select the matched text in the editor.
ReplaceTextAsync(range, text)
Replace the range with plain text. Use a fresh match after editing the document.
FormatTextAsync(range, properties)
Apply supported CSS text properties, such as font-weight or color, supplied as a dictionary.

Save through the editor or export to a .NET stream after editing. For jobs that run without a user-facing editor, see the Server SDK quickstart.