Use PDF.js to Render PDF Documents within the Document Viewer
With the release of the latest release candidate of the Document Viewer, we released the ability to use PDF.js to render PDF documents. This article shows how to use this new feature.

PDF.js is a JavaScript library maintained by Mozilla and other individual contributors. In the latest release candidate of the TX Text Control Document Viewer, we introduced the ability to enable external renderers such as PDF.js to provide document rendering information.
This tutorial shows how to create a new ASP.NET Core Web App that uses the Document
Creating the Application
Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK or .NET 7 SDK.
-
In Visual Studio 2022, create a new project by choosing Create a new project.
-
Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.
-
Choose a name for your project and confirm with Next.
-
In the next dialog, choose .NET 6 (Long Term Support) as the Framework and confirm with Create.
Adding the NuGet Packages
-
In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.
Select Text Control Offline Packages from the Package source drop-down.
Package Source
Select either Text Control Offline Packages or nuget.org as the Package source. Packages in the official Text Control NuGet profile are frequently updated.
In case you are using a Trial Access Token, please choose nuget.org.
Install the latest versions of the following package:
- TXTextControl.TextControl.ASP.SDK
- TXTextControl.Web.DocumentViewer
-
Switch the package source to nuget.org and choose Installed to check for available updates. Make sure to check "Include prerelease" to get the release candidate listed. Update the installed packages to the latest version.
Adding PDF.js
-
Select the project in the Solution Explorer and choose Add ->Client-Side Library... from the right-click context menu.
-
In the opened dialog box, search for pdf.js by typing it into the Library: text box. Click Install to add the required files to your project.
Adding a PDF Document
-
Select the project in the Solution Explorer and add a folder named App_Data by clicking Project -> New Folder from the main menu. Add any PDF document to this folder and name it test.pdf.
Adding the Control to the View
-
Find the Index.cshtml file in the Views -> Home folder. Replace the complete content with the following code:
@using TXTextControl.Web.MVC.DocumentViewer <div style="width: 800px; height: 600px;"> @Html.TXTextControl().DocumentViewer(settings => { settings.DocumentPath = "App_Data/test.pdf"; settings.Dock = DocumentViewerSettings.DockStyle.Fill; settings.DocumentLoadSettings.PDFJS.BasePath = "lib/pdf.js/"; }).Render() </div>
Using CDN Package Sources
In the example above, the PDF.js files are imported directly into the project. If you want to link to a CDN-hosted version, simply add the full path to the BasePath property.
using TXTextControl.Web.MVC.DocumentViewer <div style="width: 800px; height: 600px;"> @Html.TXTextControl().DocumentViewer(settings => { settings.DocumentPath = "App_Data/test.pdf"; settings.Dock = DocumentViewerSettings.DockStyle.Fill; settings.DocumentLoadSettings.PDFJS.BasePath = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.2.146"; }).Render() </div>
-
Compile and start the application. In the F12 developer tools console, you can see whether PDF.js is used to render the loaded PDF document.
Loading PDF Documents using JavaScript
PDF documents can be loaded using JavaScript into the Document Viewer as a Base64-encoded string. The DocumentLoadSettings must be passed as a JSON object to the loadDocument method:
var loadSettings = { pdfjs: { basePath: "lib/pdf.js/" } }
TXDocumentViewer.loadDocument(data, "loaded.pdf", null, loadSettings);
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
ASP.NETASP.NET CoreDocument Viewer
Feature Announcement: Enabling External PDF Renderer PDF.js in TX Text…
We are working on a feature that supports the enabling of external document renderers such as PDF.js to render PDF documents. This article gives a quick overview of the possibilities.
ASP.NETASP.NET CoreDocument Viewer
High-Performance Text Replacement in Large DOCX Files using C# .NET
Learn how to efficiently replace text in large DOCX files using C# .NET and the ServerTextControl component from Text Control. This article demonstrates the performance benefits of using the…
ASP.NETASP.NET CoreDocument Viewer
Document Viewer 33.2.1 Released: New Event and Bug Fixes
This service pack includes important bug fixes and improvements to enhance the stability and performance of the Document Viewer. In addition, a new event has been introduced to provide developers…
ASP.NETASP.NET CoreDocument Viewer
Document Viewer: Long Polling Support for Loading Documents
The Document Viewer now supports long polling for loading documents. This allows an asynchronous loading of documents and is especially useful for large documents or slow connections.
ASP.NETASP.NET CoreDocument Viewer
Document Viewer 31.2.2 RC1 Released with New Features
We have just released the first release candidate (RC) of the next version of the Document Viewer. This article lists the new features and improvements in this major release.