Pages/Index.cshtml
@page
@model IndexModel
@{
ViewData["Title"] = "Viewer options";
ViewData["Page"] = "Index";
var zooms = new[] { ("FitH", "Fit width"), ("Fit", "Fit page"), ("FitV", "Fit height"), ("50", "50%"), ("75", "75%"), ("100", "100%"), ("150", "150%"), ("200", "200%") };
var layouts = new[] { ("OneColumn", "One column"), ("SinglePage", "Single page"), ("TwoColumnLeft", "Two columns"), ("TwoColumnRight", "Two columns, cover") };
var themes = new[] { ("system", "As set in the browser"), ("light", "Light"), ("dark", "Dark") };
var panels = new[] { ("", "The document's choice"), ("none", "No panel"), ("thumbs", "Thumbnails"), ("bookmarks", "Bookmarks"), ("attachments", "Attachments") };
}
<aside class="options">
<form method="get">
<input type="hidden" name="Submitted" value="true" />
<h2>Document</h2>
<p class="note">A file of this web site, displayed with the <code>src</code> attribute. See "Document from memory"
for documents you create in your code.</p>
<h2>Opening view</h2>
<label>Page <input type="number" name="OpenPage" min="1" value="@Model.OpenPage" /></label>
<label>
Zoom
<select name="ZoomOrFit">
@foreach (var (value, text) in zooms)
{
<option value="@value" selected="@(value == Model.ZoomOrFit)">@text</option>
}
</select>
</label>
<label>
Page layout
<select name="Layout">
@foreach (var (value, text) in layouts)
{
<option value="@value" selected="@(value == Model.Layout)">@text</option>
}
</select>
</label>
<label>
Panel
<select name="Panel">
@foreach (var (value, text) in panels)
{
<option value="@value" selected="@(value == (Model.Panel ?? ""))">@text</option>
}
</select>
</label>
<h2>Viewer user interface</h2>
<label><input type="checkbox" name="Toolbar" value="true" checked="@Model.Toolbar" /> Toolbar</label>
<label><input type="checkbox" name="NavigationPanel" value="true" checked="@Model.NavigationPanel" /> Navigation panel button</label>
<label><input type="checkbox" name="Scrollbars" value="true" checked="@Model.Scrollbars" /> Scrollbars</label>
<label><input type="checkbox" name="PrintButton" value="true" checked="@Model.PrintButton" /> Print button</label>
<label><input type="checkbox" name="DownloadButton" value="true" checked="@Model.DownloadButton" /> Download button</label>
<label><input type="checkbox" name="FindButton" value="true" checked="@Model.FindButton" /> Find button</label>
<label><input type="checkbox" name="ZoomButtons" value="true" checked="@Model.ZoomButtons" /> Zoom buttons</label>
<label><input type="checkbox" name="PresentationButton" value="true" checked="@Model.PresentationButton" /> Presentation mode command</label>
<label><input type="checkbox" name="OpenButton" value="true" checked="@Model.OpenButton" /> Open file command</label>
<label>
Theme
<select name="Theme">
@foreach (var (value, text) in themes)
{
<option value="@value" selected="@(value == Model.Theme)">@text</option>
}
</select>
</label>
<button type="submit" class="primary" id="showButton">Show the document</button>
</form>
<h3>The markup</h3>
<pre class="markup"><expertpdf-viewer src="~/files/sample.pdf"
page="@Model.OpenPage" @(Model.Zoom > 0 ? $"zoom=\"{Model.Zoom}\"" : $"fit=\"{Model.Fit}\"") layout="@Model.Layout"@(string.IsNullOrEmpty(Model.Panel) ? "" : $" display-mode=\"{Model.Panel}\"")
show-toolbar="@Model.Toolbar.ToString().ToLowerInvariant()" show-navigation-panel="@Model.NavigationPanel.ToString().ToLowerInvariant()"
show-scrollbars="@Model.Scrollbars.ToString().ToLowerInvariant()" allow-print="@Model.PrintButton.ToString().ToLowerInvariant()"
allow-download="@Model.DownloadButton.ToString().ToLowerInvariant()" show-find-button="@Model.FindButton.ToString().ToLowerInvariant()"
show-zoom-buttons="@Model.ZoomButtons.ToString().ToLowerInvariant()" show-presentation-mode-button="@Model.PresentationButton.ToString().ToLowerInvariant()"
show-open-button="@Model.OpenButton.ToString().ToLowerInvariant()" theme="@Model.Theme" height="100%">
</expertpdf-viewer></pre>
</aside>
<section class="viewer">
<expertpdf-viewer id="viewer" src="~/files/sample.pdf"
page="@Model.OpenPage" zoom="@Model.Zoom" fit="@Model.Fit"
layout="@Model.Layout" display-mode="@(string.IsNullOrEmpty(Model.Panel) ? null : Model.Panel)"
show-toolbar="@Model.Toolbar" show-navigation-panel="@Model.NavigationPanel"
show-scrollbars="@Model.Scrollbars" allow-print="@Model.PrintButton" allow-download="@Model.DownloadButton"
show-find-button="@Model.FindButton" show-zoom-buttons="@Model.ZoomButtons"
show-presentation-mode-button="@Model.PresentationButton" show-open-button="@Model.OpenButton" theme="@Model.Theme"
height="100%"></expertpdf-viewer>
</section>
Pages/Index.cshtml.cs
using System.Globalization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace PdfViewerDemo.Pages
{
// The viewer options come from the query string of the options form (GET), so every combination has its own address.
public class IndexModel : PageModel
{
[BindProperty(SupportsGet = true)] public int OpenPage { get; set; } = 1;
[BindProperty(SupportsGet = true)] public string ZoomOrFit { get; set; } = "FitH";
[BindProperty(SupportsGet = true)] public string Layout { get; set; } = "OneColumn";
[BindProperty(SupportsGet = true)] public string Panel { get; set; } = "";
[BindProperty(SupportsGet = true)] public bool Submitted { get; set; }
[BindProperty(SupportsGet = true)] public bool Toolbar { get; set; }
[BindProperty(SupportsGet = true)] public bool NavigationPanel { get; set; }
[BindProperty(SupportsGet = true)] public bool Scrollbars { get; set; }
[BindProperty(SupportsGet = true)] public bool PrintButton { get; set; }
[BindProperty(SupportsGet = true)] public bool DownloadButton { get; set; }
[BindProperty(SupportsGet = true)] public bool FindButton { get; set; }
[BindProperty(SupportsGet = true)] public bool ZoomButtons { get; set; }
[BindProperty(SupportsGet = true)] public bool PresentationButton { get; set; }
[BindProperty(SupportsGet = true)] public bool OpenButton { get; set; }
// "system" (as set in the browser), "light" or "dark"
[BindProperty(SupportsGet = true)] public string Theme { get; set; } = "system";
// the zoom list holds both fixed percentages ("150") and fit modes ("FitH")
public float Zoom
{
get { return float.TryParse(ZoomOrFit, NumberStyles.Float, CultureInfo.InvariantCulture, out float z) ? z : -1; }
}
public string Fit
{
get { return Zoom > 0 ? null : ZoomOrFit; }
}
public void OnGet()
{
// first visit: the whole user interface is shown
if (!Submitted)
Toolbar = NavigationPanel = Scrollbars = PrintButton = DownloadButton = FindButton = ZoomButtons = PresentationButton = true;
}
}
}