View PDF files using PDF Viewer Component in the Blazor Server app

29 Jan 20255 minutes to read

In this section, we’ll guide you through the process of adding Syncfusion® Blazor PDF Viewer component to your Blazor Server app. We’ll break it down into simple steps to make it easy to follow. Additionally, you can find a fully functional example project on our GitHub repository.

Prerequisites

Create a new Blazor App in Visual Studio

You can create a Blazor Server App using Visual Studio via Microsoft Templates or the Syncfusion® Blazor Extension.

NOTE

The PDF Viewer component is supported from .NET 6.0 onwards.

Install Syncfusion® Blazor SfPdfViewer and Themes NuGet in the App

To add Blazor PDFViewer component in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search and install Syncfusion.Blazor.SfPdfViewer and Syncfusion.Blazor.Themes. Alternatively, you can utilize the following package manager command to achieve the same.

Install-Package Syncfusion.Blazor.SfPdfViewer -Version 28.2.3
Install-Package Syncfusion.Blazor.Themes -Version 28.2.3

NOTE

Syncfusion® Blazor components are available in nuget.org. Refer to NuGet packages topic for available NuGet packages list with component details.

Create a new Blazor App in Visual Studio Code

You can create a Blazor Server App using Visual Studio Code via Microsoft Templates or the Syncfusion® Blazor Extension.

Alternatively, you can create a Server application using the following command in the terminal(Ctrl+`).

dotnet new blazorserver -o BlazorApp
cd BlazorApp

NOTE

The PDF Viewer component is supported from .NET 6.0 onwards.

Install Syncfusion® Blazor SfPdfViewer and Themes NuGet in the App

  • Press Ctrl+` to open the integrated terminal in Visual Studio Code.
  • Ensure you’re in the project root directory where your .csproj file is located.
  • Run the following command to install a Syncfusion.Blazor.SfPdfViewer and Syncfusion.Blazor.Themes NuGet package and ensure all dependencies are installed.
dotnet add package Syncfusion.Blazor.SfPdfViewer -v 28.2.3
dotnet add package Syncfusion.Blazor.Themes -v 28.2.3
dotnet restore

NOTE

Syncfusion® Blazor components are available in nuget.org. Refer to NuGet packages topic for available NuGet packages list with component details.

Register Syncfusion® Blazor Service

  • In the ~/_Imports.razor file, add the following namespaces:
@using Syncfusion.Blazor;
@using Syncfusion.Blazor.SfPdfViewer;
  • Register the Syncfusion® Blazor Service in the ~/Program.cs file.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Syncfusion.Blazor;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

builder.Services.AddSignalR(o => { o.MaximumReceiveMessageSize = 102400000; });

// Add Syncfusion Blazor service to the container.
builder.Services.AddSyncfusionBlazor();

var app = builder.Build();

NOTE

Processing Large Files Without Increasing Maximum Message Size in SfPdfViewer Component

Adding stylesheet and script

Add the following stylesheet and script to the head section of the ~/Pages/_Host.cshtml file.

<head>
    <!-- Syncfusion Blazor PDF Viewer control's theme style sheet -->
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
    <!-- Syncfusion Blazor PDF Viewer control's scripts -->
    <script src="_content/Syncfusion.Blazor.SfPdfViewer/scripts/syncfusion-blazor-sfpdfviewer.min.js" type="text/javascript"></script>
</head>

Adding Blazor PDF Viewer Component

Add the Syncfusion® PDF Viewer (Next Gen) component in the ~/Pages/Index.razor file

@page "/"

<SfPdfViewer2 DocumentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
              Height="100%"
              Width="100%">
</SfPdfViewer2>

NOTE

If you don’t provide the DocumentPath property value, the PDF Viewer component will be rendered without loading the PDF document. Users can then use the open option from the toolbar to browse and open the PDF as required.

Run the application

Run the application, and the PDF file will be displayed using Syncfusion® Blazor PDF Viewer component in your browser.

Blazor SfPdfViewer Component

See also