Blog Post

How to Build an ASP.NET Image Viewer with PSPDFKit

Illustration: How to Build an ASP.NET Image Viewer with PSPDFKit

In this post, we provide you with a step-by-step guide outlining how to deploy PSPDFKit’s ASP.NET image viewer.

ASP.NET is an open source web framework used to build modern web apps and services with .NET, and it was created by Microsoft. ASP.NET Core is the open source version of ASP.NET, and it runs on macOS, Windows, Linux, and Docker.

ASP.NET Core MVC is a framework used to build web applications and APIs using the Model-View-Controller (MVC) design pattern, which will be the focus of this tutorial.

What Is an ASP.NET Image Viewer?

An ASP.NET image viewer lets you render and view image documents in a web browser without the need to download it to your hard drive or use an external application like a PDF reader.

PSPDFKit ASP.NET Image Viewer

We offer a commercial ASP.NET image viewer library that can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.

  • A prebuilt and polished UI
  • 15+ annotation tools
  • Support for multiple file types
  • Dedicated support from engineers

Example of Our ASP.NET Image Viewer

To see our image viewer in action, upload a JPG, PNG, or TIFF file by selecting Choose Example > Open Document (if you don’t see this, switch to the Standalone option). Once your image is displayed in the viewer, you can try drawing freehand, adding a note, or applying a crop or an e-signature.

Requirements to Get Started

To get started, you’ll need:

  • .NET 6.0, which is the current Long Term Support (LTS) version of .NET.

You can download it from Microsoft’s website.

Creating a New ASP.NET Project

  1. For this tutorial, you’ll install the ASP.NET web application (Model-View-Controller) template using the .NET CLI (command-line interface). Open your terminal, navigate to the directory where you want to create your project, and run the following command:

dotnet new mvc -o pspdfkit-aspnet-example

This command creates your new ASP.NET MVC project and places it in a new directory called pspdfkit-aspnet-example inside your current location.

  1. Change your directory to the newly created project:

cd pspdfkit-aspnet-example

mvc is a template that creates the initial files and directory structure for an ASP.NET MVC project. Here’s the file structure of the project:

asp.net mvc file structure shown in VS Code

  • Views/Home — Contains the routable components/pages. You’ll work on the Home page, which is the Index.cshtml file.

  • Views/Shared — Contains the shared components and stylesheets. The _Layout.cshtml file defines the app’s layout component.

  • Properties/launchSettings.json — Located in the Properties folder and holds the development environment configuration.

  • wwwroot — Contains the public static assets. You’ll store the PDF file and PSPDFKit artifacts in this folder.

  • Program.cs — Contains the app startup code and middleware configuration.

  • pspdfkit-aspnet-example.csproj — Defines the app project and its dependencies.

Adding PSPDFKit to Your Project

PSPDFKit for Web library files are distributed as an archive that can be extracted manually.

  1. Download the framework here. The download will start immediately and will save a .tar.gz archive like PSPDFKit-Web-binary-2022.2.1.tar.gz to your computer.

  2. Once the download is complete, extract the archive and copy the entire contents of its dist folder to your project’s wwwroot/lib folder.

  3. Make sure your wwwroot/lib folder contains the pspdfkit.js file and a pspdfkit-lib directory with the library assets.

Displaying an Image Document

  1. Add the image you want to display to the public directory. You can use our demo image as an example.

  2. Navigate to the Views/Shared/_Layout.cshtml file. During rendering, the @RenderBody() property will be replaced with the content of the layout:

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>@ViewData["Title"] - pspdfkit_aspnet_example</title>
		@* ... *@
	</head>
	<body>
		@RenderBody()
	</body>
</html>
  1. Now, you’ll start working on the Home route. Navigate to your project’s Views/Home/Index.cshtml file, and load the PSPDFKit SDK before the </body> tag:

<div id="pspdfkit" style="width: 100%; height: 100vh;"></div>

<script src="/lib/pspdfkit.js"></script>

<script>
	PSPDFKit.load({
		container: "#pspdfkit",
		document: "image.png",
	})
		.then(function(instance) {
			console.log("PSPDFKit loaded", instance);
		})
		.catch(function(error) {
			console.error(error.message);
		});
</script>

The div element is used to display the PDF document.

Serving the Application

  1. Start the app in the root directory of your project:

dotenv watch run

The server will start and automatically restart the application when changes are made to the project.

Information

You can access the source code for this tutorial on GitHub. If you're using Visual Studio, follow our getting started guide for Visual Studio.

Adding Even More Capabilities

Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular ASP.NET guides:

Conclusion

You should now have our ASP.NET image viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our support team for help.

You can also integrate our JavaScript image viewer using web frameworks like Angular and React.js. To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
WEB  |  Web • Vue.js • JavaScript • How to • PDF Generation

Generate a PDF from HTML with Vue.js

PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2022.3 Introduces New PDF Form Creator UI

TUTORIALS  |  Web • SharePoint • How To • PDF Viewer

SharePoint PDF Viewer: Integrating On-Premises with PSPDFKit