@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"><expertpdf-viewer source-bytes="@@Model.PdfBytes"
viewer-preferences="@@Model.Preferences"
security-options="@@Model.Security"
height="100%"></expertpdf-viewer></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>
}
Pages/FromMemory.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PdfViewer4AspNet;
namespace PdfViewerDemo.Pages
{
public class FromMemoryModel : PageModel
{
private readonly IWebHostEnvironment environment;
public FromMemoryModel(IWebHostEnvironment environment)
{
this.environment = environment;
}
[BindProperty(SupportsGet = true)] public bool Protect { get; set; }
// the page gives the password of the protected document (the passwordrequired event of the client script)
[BindProperty(SupportsGet = true)] public bool PagePassword { get; set; }
// the document to display
public byte[] PdfBytes { get; private set; }
// viewer preferences and security options the viewer writes into the document before it is displayed
public PdfViewerPreferences Preferences { get; private set; }
public PdfSecurityOptions Security { get; private set; }
public void OnGet()
{
// A document created in memory - for example by the ExpertPdf HTML to PDF converter, or read from a
// database - is displayed without saving it to a file. Here the bytes are read from the sample file.
PdfBytes = System.IO.File.ReadAllBytes(Path.Combine(environment.WebRootPath, "files", "sample.pdf"));
// open as a book: two pages side by side, the first page alone (like a cover), with the outline
Preferences = new PdfViewerPreferences
{
PageLayout = PdfViewerPageLayout.TwoColumnRight,
PageMode = PdfViewerPageMode.UseOutlines
};
if (Protect)
{
// a password to open the document ("expertpdf"), and no printing or copying
Security = new PdfSecurityOptions
{
UserPassword = "expertpdf",
CanPrint = false,
CanCopyContent = false
};
}
}
}
}