Some test text!
Web / Guides
Platform
Documentation
Welcome to Apryse. You can start working with WebViewer using our Blazor sample from scratch, or integrate it into an existing application.
This guide will help you integrate a free trial of WebViewer into existing Blazor applications. Your free trial includes unlimited trial usage and support from solution engineers.
PROJ_ROOT
:PROJ_ROOT = path/to/your/blazor/project/
WebViewer.zip
file you downloaded, copy the lib
folder into the static folder of your application, which is usually wwwroot
.PROJ_ROOT/wwwroot/index.html
or PROJ_ROOT/Pages/_Host.cshtml
). Add the WebViewer library as a script tag in the <head>
element of that file:<script src="lib/webviewer.min.js"></script>
Next to call WebViewer APIs, we need to wrap them in a JavaScript object. Make a js
folder in your static directory if it does not exist already, and make a new file in it called webviewerScripts.js
.
Inside that file, we define a new object called webviewerFunctions
and expose that to window
:
window.webviewerFunctions = {
initWebViewer: function () {
const viewerElement = document.getElementById('viewer');
WebViewer({
path: 'lib',
initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf', // replace with your own PDF file
}, viewerElement).then(instance => {
// call apis here
})
}
};
Then to make sure Blazor can find this file, again add a script tag to the main html file of your project, but this time inside the <body>
element:
<script src="js/webviewerScripts.js"></script>
Now that we have wrapped the WebViewer JavaScript instantiation function, we need to define the actual page for WebViewer to run in. Make a new page under PROJ_ROOT/Pages
and call it WebViewer.razor
.
Inside this page make a div element called viewer
for the instantiation to attach to:
<h1>WebViewer</h1>
<div id='viewer' style='width: 1024px; height: 600px; margin: 0 auto;'></div>
Then override the OnAfterRender()
method of the page and invoke the initWebViewer
function we defined earlier:
await JSRuntime.InvokeAsync<object>("webviewerFunctions.initWebViewer");
Your final WebViewer.razor
file should look something like this:
@page "/webviewer"
@inject IJSRuntime JSRuntime
<h1>WebViewer</h1>
<div id='viewer' style='width: 1024px; height: 600px; margin: 0 auto;'></div>
@code {
protected override async void OnAfterRender(bool firstRender)
{
if (firstRender) {
+ await JSRuntime.InvokeAsync<object>("webviewerFunctions.initWebViewer");
}
}
}
startup.cs
:using System.IO;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".res"] = "application/octet-stream";
provider.Mappings[".pexe"] = "application/x-pnacl";
provider.Mappings[".nmf"] = "application/octet-stream";
provider.Mappings[".mem"] = "application/octet-stream";
provider.Mappings[".wasm"] = "application/wasm";
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
ContentTypeProvider = provider
});
Your final startup.cs
should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorWebViewerServer.Data;
+using System.IO;
+using Microsoft.AspNetCore.StaticFiles;
+using Microsoft.Extensions.FileProviders;
namespace BlazorWebViewerServer
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
+ var provider = new FileExtensionContentTypeProvider();
+ provider.Mappings[".res"] = "application/octet-stream";
+ provider.Mappings[".pexe"] = "application/x-pnacl";
+ provider.Mappings[".nmf"] = "application/octet-stream";
+ provider.Mappings[".mem"] = "application/octet-stream";
+ provider.Mappings[".wasm"] = "application/wasm";
+ app.UseStaticFiles(new StaticFileOptions
+ {
+ FileProvider = new PhysicalFileProvider(
+ Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
+ ContentTypeProvider = provider
+ });
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
dotnet run
In your browser, go to https://localhost:5001/webviewer
to see the WebViewer in action.
MIME issues
Enable MIME type mappings for WebViewer to load documents.
Server-side document operations with .NET Core SDK
Handle document operations through the JavaScript interop by passing its base64 data.
Get the answers you need: Support