PDF Viewer for ASP.NET Core Razor Pages demo · .NET 10 and .NET 11

Code of this sample

Pages/FromMemory.cshtml
@page
@model FromMemoryModel
@{
    ViewData["Title"] = "Document from memory";
    ViewData["Page"] = "FromMemory";
}
<aside class="options">
    <h2>A document from memory</h2>
    <p>The page gives the viewer the bytes of a document (<code>source-bytes</code>). Use it for documents you create in
        your code - for example with ExpertPdf HTML to PDF Converter - or read from a database: they are displayed
        without saving them to a file.</p>

    <h2>Viewer preferences</h2>
    <p>The viewer writes viewer preferences into the document before it is displayed (<code>viewer-preferences</code>):
        here it opens as a book, two pages side by side, with the outline.</p>

    <h2>Security</h2>
    <p>The viewer can also protect the document (<code>security-options</code>): require a password to open it, and
        allow or deny printing and copying its content.</p>
    @if (Model.Protect)
    {
        <p class="note">The document is protected now: the password is <strong>expertpdf</strong>, and printing and
            copying are not allowed.</p>
        <a class="primary" id="toggleProtection" asp-page="/FromMemory">Show the document without protection</a>
    }
    else
    {
        <a class="primary" id="toggleProtection" asp-page="/FromMemory" asp-route-protect="true">Protect the document</a>
    }
    @if (Model.Protect && Model.PagePassword)
    {
        <p class="note">The page gives the password when the viewer asks for it (the <code>passwordrequired</code> event
            of the client script): the user is not asked.</p>
    }
    else
    {
        <p><a id="protectPagePassword" asp-page="/FromMemory" asp-route-protect="true" asp-route-pagePassword="true">Protect the
            document, the page gives the password</a></p>
    }

    <h3>The markup</h3>
    <pre class="markup">&lt;expertpdf-viewer source-bytes="@@Model.PdfBytes"
    viewer-preferences="@@Model.Preferences"
    security-options="@@Model.Security"
    height="100%"&gt;&lt;/expertpdf-viewer&gt;</pre>
</aside>

<section class="viewer">
    <expertpdf-viewer id="viewer" source-bytes="@Model.PdfBytes"
                      viewer-preferences="@Model.Preferences" security-options="@Model.Security"
                      height="100%"></expertpdf-viewer>
</section>

@section Scripts {
    @* The client script of the viewer: the page gives the password of the protected document when the viewer asks for
       it (for example a password the page gets from your server). Without a handler, or when the handler returns
       nothing, the viewer asks the user. *@
    <script src="~/expertpdf-viewer/expertpdf-viewer-client.js"></script>
    <script>
        var pageGivesPassword = @(Model.Protect && Model.PagePassword ? "true" : "false");
        ExpertPdfViewer.get("viewer").on("passwordrequired", function (e) {
            if (!pageGivesPassword || e.incorrect)
                return;   // the viewer asks the user
            return "expertpdf";
        });
    </script>
}