Click or drag to resize

The ASP.NET Core Tag Helper

In ASP.NET Core (.NET 8 or later) the viewer is the <expertpdf-viewer> tag helper, used in Razor Pages and MVC views. The UseExpertPdfViewer middleware serves the viewer files and the documents given to the viewer as bytes. The types are in the PdfViewer4AspNet namespace of the .NET 8 build of PdfViewerAspNet.dll.

Setting up the application
C#
using PdfViewer4AspNet;

var app = builder.Build();

// before UseRouting / MapRazorPages / MapControllerRoute
app.UseExpertPdfViewer(options =>
{
    options.LicenseKey = "your license key";
    // options.PathPrefix = "/pdf-viewer";   // the default is "/expertpdf-viewer"
});

PdfViewerOptions

Description

PathPrefix

The path the viewer files and documents are served under. The default value is "/expertpdf-viewer".

LicenseKey

The license key used by the viewers that do not set their own.

Register the tag helper in _ViewImports.cshtml:

@using PdfViewer4AspNet
@addTagHelper *, PdfViewerAspNet
The attributes of <expertpdf-viewer>

Attribute

Type

Description

src

string

The address of the document: a path of the application ("/files/report.pdf", "~/files/report.pdf") or an absolute URL. A document on another web site must allow the page to read it (CORS); otherwise download it on the server and use source-bytes.

source-bytes

byte[]

The document, from memory. When set, src is ignored.

page

int

The page the document opens at (the first page is 1).

zoom

float

The zoom percentage (100 = 100%). It takes precedence over fit.

fit

string

"Fit" (the whole page), "FitH" (the page width) or "FitV" (the page height). Without zoom and fit the viewer fits the page width.

layout

string

"SinglePage", "OneColumn", "TwoColumnLeft" or "TwoColumnRight".

display-mode

string

The panel next to the document: "thumbs", "bookmarks", "attachments" or "none". Not set: the document decides.

show-toolbar

bool

Show the toolbar. The default value is true.

show-navigation-panel

bool

Show the button that opens the navigation panel. The default value is true.

show-scrollbars

bool

Show the scrollbars. The default value is true.

allow-print

bool

Show the print button. The default value is true.

allow-download

bool

Show the download button. The default value is true.

theme

string

The colors of the viewer: "light", "dark" or "system" (as set in the browser of the user, the default). The pages keep their own colors.

show-open-button

bool

The open file command (tools menu, Ctrl+O): the user opens a PDF file of the computer in the viewer; it is not sent to the server. The default value is false.

show-sidebar-button

bool

The button that opens and closes the navigation panel. The default value is true.

show-find-button

bool

The find bar (Ctrl+F). The default value is true.

show-page-navigation-buttons

bool

Previous and next page, the page number box, first and last page. The default value is true.

show-zoom-buttons

bool

Zoom out, zoom in and the zoom list. The default value is true.

show-presentation-mode-button

bool

Presentation mode (full screen). The default value is true.

show-document-properties-button

bool

Document properties. The default value is true.

width, height

string

The size of the viewer (any CSS length). The defaults are "100%" and "600px".

viewer-preferences

PdfViewerPreferences

Viewer preferences written into the displayed document (page layout, page mode).

security-options

PdfSecurityOptions

Passwords and permissions written into the displayed document.

open-password

string

The password of a protected document, used to apply the viewer preferences and security options to it.

license-key

string

The license key of this viewer (otherwise the key set with UseExpertPdfViewer).

The element renders an iframe; the other attributes you add (id, class, style, data-...) are copied to it. Give the viewer an id to drive it with the JavaScript client API.

Examples
C#
// the page model: a protected document that opens as a book
public class ReportModel : PageModel
{
    public byte[] PdfBytes { get; private set; }
    public PdfSecurityOptions Security { get; private set; }
    public PdfViewerPreferences Preferences { get; private set; }

    public void OnGet()
    {
        PdfBytes = CreateReport();   // your document
        Security = new PdfSecurityOptions { UserPassword = "secret", CanPrint = false };
        Preferences = new PdfViewerPreferences { PageLayout = PdfViewerPageLayout.TwoColumnRight };
    }
}
XML
<!-- a file of the application, opened at page 3, two pages side by side, with the thumbnails -->
<expertpdf-viewer id="viewer" src="~/files/report.pdf" page="3" layout="TwoColumnLeft"
                  display-mode="thumbs" height="800px"></expertpdf-viewer>

<!-- a document created by the page model, without the download button -->
<expertpdf-viewer source-bytes="@Model.PdfBytes" allow-download="false" height="100%"></expertpdf-viewer>
<expertpdf-viewer src="~/files/report.pdf" theme="dark" show-find-button="false" show-open-button="true"></expertpdf-viewer>
XML
<expertpdf-viewer source-bytes="@Model.PdfBytes" security-options="@Model.Security"
                  viewer-preferences="@Model.Preferences" height="800px"></expertpdf-viewer>
See Also