VintaSoft Imaging .NET SDK 12.4: Documentation for Web developer
In This Topic
    Add web document viewer to an ASP.NET Core application with React.js
    In This Topic
    This tutorial shows how to create a blank ASP.NET Core Web application in Visual Studio .NET 2022 and add document viewer (with ability to open images and documents (PDF, DOCX, XLSX), extract/search text, annotate documents) to ASP.NET Core application with React.js.


    Here are steps, which must be done:
    1. Create a blank ASP.NET Core Web application.

      • Open Visual Studio .NET 2022 and create a new project, of ASP.NET Core Web application type:

      • Configure the project to use .NET 8.0:
    2. Server side: Add references to the Vintasoft assemblies to your ASP.NET Core Web application.

      Copy Vintasoft.Shared.dll, Vintasoft.Imaging.dll, Vintasoft.Imaging.Pdf.dll, Vintasoft.Imaging.Annotation.dll, Vintasoft.Imaging.Annotation.Pdf.dll, Vintasoft.Imaging.Office.OpenXml, Vintasoft.Shared.Web.dll, Vintasoft.Imaging.Web.Services.dll, Vintasoft.Imaging.Annotation.Web.Services.dll, Vintasoft.Imaging.AspNetCore.ApiControllers.dll and Vintasoft.Imaging.Annotation.AspNetCore.ApiControllers.dll assemblies from "<SdkInstallPath>\VintaSoft Imaging .NET v12.4\Bin\DotNet8\AnyCPU\" directory to the "Bin" directory of ASP.NET Core Web application and add references to assemblies in ASP.NET Core Web application.



    3. Server side: Specify drawing engine, which should be used by VintaSoft Imaging .NET SDK for drawing of 2D graphics.

      If ASP.NET application must be used in Windows or Linux, SkiaSharp drawing engine should be used.
      If ASP.NET application must be used in Windows only, System.Drawing or SkiaSharp drawing engine should be used.

      Here are steps, which should be made for using SkiaSharp engine:
      • Add reference to the Vintasoft.Imaging.Drawing.SkiaSharp.dll assembly.
      • Add reference to the SkiaSharp nuget-package version 2.88.6.
      • Open "Startup.cs" file, add code line "Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault();" at the beginning of ConfigureServices method - added code specifies that VintaSoft Imaging .NET SDK should use SkiaSharp library for drawing of 2D graphics.

      Here are steps, which should be made for using System.Drawing engine:
      • Add reference to the Vintasoft.Imaging.Gdi.dll assembly.
      • Open "Startup.cs" file, add code line "Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault();" at the beginning of ConfigureServices method - added code specifies that VintaSoft Imaging .NET SDK should use System.Drawing library for drawing of 2D graphics.

    4. Server side: Create web services, which allow to upload/download file, manage image collection, get information about images, get thumbnails, render image tiles, extract/search text and annotate document.

      • Create web service that allows to upload/download file

        • Press the right mouse button on the "Controllers" folder and select the "Add => Controller..." menu from context menu
        • Select Empty API controller template, set the controller name to the "MyVintasoftFileApiController" and press the "Add" button
        • Specify that MyVintasoftFileApiController class is derived from Vintasoft.Imaging.AspNetCore.ApiControllers.VintasoftFileApiController class

          Here are source codes of MyVintasoftFileApiController class:
          using Microsoft.AspNetCore.Hosting;
          
          namespace WebApplication1.Controllers
          {
              public class MyVintasoftFileApiController : Vintasoft.Imaging.AspNetCore.ApiControllers.VintasoftFileApiController
              {
          
                  public MyVintasoftFileApiController(IWebHostEnvironment hostingEnvironment)
                      : base(hostingEnvironment)
                  {
                  }
          
              }
          }
          
          
      • Create web service that allows to manage image collection

        • Press the right mouse button on the "Controllers" folder and select the "Add => Controller..." menu from context menu
        • Select Empty API controller template, set the controller name to the "MyVintasoftImageCollectionApiController" and press the "Add" button
        • Specify that MyVintasoftImageCollectionApiController class is derived from Vintasoft.Imaging.AspNetCore.ApiControllers.VintasoftImageCollectionApiController class

          Here are source codes of MyVintasoftImageCollectionApiController class:
          using Microsoft.AspNetCore.Hosting;
          
          namespace WebApplication1.Controllers
          {
              public class MyVintasoftImageCollectionApiController : Vintasoft.Imaging.AspNetCore.ApiControllers.VintasoftImageCollectionApiController
              {
          
                  public MyVintasoftImageCollectionApiController(IWebHostEnvironment hostingEnvironment)
                      : base(hostingEnvironment)
                  {
                  }
          
              }
          }
          
          
      • Create web service that allows to get information about images, get thumbnails, render image tiles, extract/search text

        • Press the right mouse button on the "Controllers" folder and select the "Add => Controller..." menu from context menu
        • Select Empty API controller template, set the controller name to the "MyVintasoftImageApiController" and press the "Add" button
        • Specify that MyVintasoftImageApiController class is derived from Vintasoft.Imaging.AspNetCore.ApiControllers.VintasoftImageApiController class

          Here are source codes of MyVintasoftImageApiController class:
          using Microsoft.AspNetCore.Hosting;
          
          namespace WebApplication1.Controllers
          {
              public class MyVintasoftImageApiController : Vintasoft.Imaging.AspNetCore.ApiControllers.VintasoftImageApiController
              {
          
                  public MyVintasoftImageApiController(IWebHostEnvironment hostingEnvironment)
                      : base(hostingEnvironment)
                  {
                  }
          
              }
          }
          
          
      • Create web service that allows to annotate images and documents

        • Press the right mouse button on the "Controllers" folder and select the "Add => Controller..." menu from context menu
        • Select Empty API controller template, set the controller name to the "MyVintasoftAnnotationCollectionApiController" and press the "Add" button
        • Specify that MyVintasoftAnnotationCollectionApiController class is derived from Vintasoft.Imaging.Annotation.AspNetCore.ApiControllers.VintasoftAnnotationCollectionApiController class

          Here are source codes of MyVintasoftAnnotationCollectionApiController class:
          using Microsoft.AspNetCore.Hosting;
          
          namespace WebApplication1.Controllers
          {
              public class MyVintasoftAnnotationCollectionApiController :
                  Vintasoft.Imaging.Annotation.AspNetCore.ApiControllers.VintasoftAnnotationCollectionApiController
              {
          
                  public MyVintasoftAnnotationCollectionApiController(IWebHostEnvironment hostingEnvironment)
                      : base(hostingEnvironment)
                  {
                  }
              }
          }
          
          
    5. Server side: Add Newtonsoft JSON for deserialization of JSON annotations.

      • Add reference to the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget-package.

      • Open the "Startup.cs" file and add code line "services.AddControllersWithViews().AddNewtonsoftJson();" at the beginning of ConfigureServices method. This is necessary for correct deserialization of JSON annotations.

        Here are source codes of Startup.ConfigureServices method:
        using Microsoft.AspNetCore.Builder;
        using Microsoft.AspNetCore.Hosting;
        using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
        using Microsoft.Extensions.Configuration;
        using Microsoft.Extensions.DependencyInjection;
        using Microsoft.Extensions.Hosting;
        
        namespace WebApplication1
        {
            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.
                public void ConfigureServices(IServiceCollection services)
                {
                    // specify that VintaSoft Imaging .NET SDK should use SkiaSharp library for drawing of 2D graphics
                    Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault();
        
                    // add Newtonsoft JSON for deserialization of JSON annotations
                    services.AddControllersWithViews().AddNewtonsoftJson();
        
                    // In production, the React files will be served from this directory
                    services.AddSpaStaticFiles(configuration =>
                    {
                        configuration.RootPath = "ClientApp/build";
                    });
                }
        
                // 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();
                    }
        
                    app.UseHttpsRedirection();
                    app.UseStaticFiles();
                    app.UseSpaStaticFiles();
        
                    app.UseRouting();
        
                    app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllerRoute(
                            name: "default",
                            pattern: "{controller}/{action=Index}/{id?}");
                    });
        
                    app.UseSpa(spa =>
                    {
                        spa.Options.SourcePath = "ClientApp";
        
                        if (env.IsDevelopment())
                        {
                            spa.UseReactDevelopmentServer(npmScript: "start");
                        }
                    });
                }
            }
        }
        
        
    6. Compile the project for restoring dependencies using 'npm'.

    7. Client side: Delete files, which are not necessary in this demo.

      • Delete files "ClientApp\src\components\Counter.js", "ClientApp\src\components\FetchData.js", "ClientApp\src\components\Home.js", "ClientApp\src\components\NavMenu.js", "ClientApp\src\components\NavMenu.css" - these React components are not necessary in this demo.

      • Delete files "WeatherForecast.cs" and "Controllers\WeatherForecastController.cs" - the WeatherForecast Web API controller is not necessary in this demo.

      • Open file "ClientApp\src\components\Layout.js" and delete lines "import { NavMenu } from './NavMenu';" and "<NavMenu />" - this demo does not need navigation menu.

      • Open file "ClientApp\src\App.js" and delete code that uses the following React components: Home, FetchData, Counter - these React components are not necessary in this demo.
        Here are source codes of "App.js" file after update:
        import React, { Component } from 'react';
        import { Route } from 'react-router';
        import { Layout } from './components/Layout';
        
        import './custom.css'
        
        export default class App extends Component {
          static displayName = App.name;
        
          render () {
            return (
                <Layout>
              </Layout>
            );
          }
        }
        
        
    8. Client side: Add JavaScript files to the ASP.NET Core Web application.

      • Copy Vintasoft.Shared.js, Vintasoft.Imaging.js, Vintasoft.Imaging.Annotation.js and Vintasoft.Imaging.DocumentViewer.js files from "<InstallPath>\VintaSoft Imaging .NET v12.4\Bin\JavaScript\" folder into "ClientApp\public\" folder.

      • Specify, which "standard" dialogs (password dialog, print dialog, etc) should be used by web document viewer
        • If web document viewer should use ready-to-use "standard" Bootstrap dialogs:
          • Copy Vintasoft.Imaging.Dialogs.Bootstrap.js and Vintasoft.Imaging.DocumentViewer.Dialogs.Bootstrap.js files from "<InstallPath>\VintaSoft Imaging .NET v12.4\Bin\JavaScript\" folder into "ClientApp\public\" folder.

        • If web document viewer should use ready-to-use "standard" jQuery UI dialogs:
          • Copy jQuery file into "ClientApp\public\" folder.
            You can use "jquery-3.3.1.min.js" file from "<InstallPath>\VintaSoft Imaging .NET v12.4\Examples\ASP.NET MVC\CSharp\AspNetMvcImagingDemo\Scripts\External\" folder or jQuery file from any other source.
          • Copy jQuery UI files and folders into "ClientApp\public\" folder.
            You can use "jquery-ui.min.js" file and "jquery-ui-css" folder from "<InstallPath>\VintaSoft Imaging .NET v12.4\Examples\ASP.NET MVC\CSharp\AspNetMvcImagingDemo\Scripts\External\" folder or jQuery UI files and folders from any other source.
          • Copy Vintasoft.Imaging.Dialogs.jQueryUI.js and Vintasoft.Imaging.DocumentViewer.Dialogs.jQueryUI.js files from "<InstallPath>\VintaSoft Imaging .NET v12.4\Bin\JavaScript\" folder into "ClientApp\public\" folder.

        • If web document viewer should use custom "standard" dialogs,please read how to create custom "standard" dialogs here.

      • Copy "Vintasoft.Imaging.DocumentViewer.css" file with CSS-styles of Vintasoft web document viewer from "<SdkInstallPath>\VintaSoft\Imaging .NET v12.4\Bin\JavaScript\" folder to the "ClientApp\public\" folder.

      • Add references to jQuery and Vintasoft JavaScript files to the header of "ClientApp\public\index.html" file, add link to the "Vintasoft.Imaging.DocumentViewer.css" file to the "ClientApp\public\index.html" file:
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
            <meta name="theme-color" content="#000000">
            <base href="%PUBLIC_URL%/" />
            <!--
              manifest.json provides metadata used when your web app is added to the
              homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
        -->
            <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
            <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
        
            <link rel="stylesheet" type="text/css" href="jquery-ui-css/jquery-ui.min.css">
            <link rel="stylesheet" type="text/css" href="Vintasoft.Imaging.DocumentViewer.css">
        
            <script src="jquery-3.3.1.min.js" type="text/javascript"></script>
            <script src="jquery-ui.min.js" type="text/javascript"></script>
        
            <script src="Vintasoft.Shared.js" type="text/javascript"></script>
            <script src="Vintasoft.Imaging.js" type="text/javascript"></script>
            <script src="Vintasoft.Imaging.Annotation.js" type="text/javascript"></script>
            <script src="Vintasoft.Imaging.DocumentViewer.js" type="text/javascript"></script>
            <script src="Vintasoft.Imaging.DocumentViewer.Dialogs.jQueryUI.js" type="text/javascript"></script>
        
            <!--
              Notice the use of %PUBLIC_URL% in the tags above.
              It will be replaced with the URL of the `public` folder during the build.
              Only files inside the `public` folder can be referenced from the HTML.
        
              Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
              work correctly both with client-side routing and a non-root public URL.
              Learn how to configure a non-root public URL by running `npm run build`.
        -->
            <title>VintaSoft Document Viewer (ASP.NET Core + React.js)</title>
        </head>
          <body>
            <noscript>
              You need to enable JavaScript to run this app.
            </noscript>
            <div id="root"></div>
            <!--
              This HTML file is a template.
              If you open it directly in the browser, you will see an empty page.
        
              You can add webfonts, meta tags, or analytics to this file.
              The build step will place the bundled scripts into the <body> tag.
        
              To begin the development, run `npm start` or `yarn start`.
              To create a production bundle, use `npm run build` or `yarn build`.
        -->
          </body>
        </html>
        
        

      • Add CSS-style "documentViewerContainer", which defines height of document viewer container, to the "ClientApp\src\custom.css" file:
        /* Provide sufficient contrast against white background */
        a {
          color: #0366d6;
        }
        
        code {
          color: #E01A76;
        }
        
        .btn-primary {
          color: #fff;
          background-color: #1b6ec2;
          border-color: #1861ac;
        }
        
        .documentViewerContainer {
            height: 800px;
        }
        
        

    9. Client side: Create React.js component that allows to display document, search and extract text from document, annotate document.

      • Create folder "wwwroot\UploadedImageFiles\SessionID" and copy test PDF document "<SdkInstallPath>\VintaSoft\Imaging .NET v12.4\Images\VintasoftImagingDemo.pdf" to the folder. This document will be displayed in document viewer.

      • Create "DocumentViewerDemo.js" file that will contain source codes of React.js component (DocumentViewerDemo class):
        • Select "Add => New Item..." context menu for folder "ClientApp\src\components\" => "Add new item" dialog will appear
        • Select "JavaScript File" type for new item
        • Set "DocumentViewerDemo.js" as element name
        • Click the "Add" button => dialog will be closed and file "DocumentViewerDemo.js" will be added into folder "ClientApp\src\components\"

        Add DocumentViewerDemo class declaration with 'render' function (renders demo header and div element that will display document viewer) to the "DocumentViewerDemo.js" file:


        Add 'componentDidMount' function (contains JavaScript code that initializes and created document viewer) to the DocumentViewerDemo class:


        Here is JavaScript code of React.js component (DocumentViewerDemo class):
        import React, { Component } from 'react';
        
        export class DocumentViewerDemo extends Component {
            static displayName = DocumentViewerDemo.name;
        
            render() {
                return (
                    <div>
                        <h1>VintaSoft Document Viewer Demo (ASP.NET Core + React.js)</h1>
        
                        <div id="documentViewerContainer" class="documentViewerContainer"></div>
                    </div>
                );
            }
        
            componentDidMount() {
                // declare reference to the Vintasoft namespace
                let Vintasoft = window.Vintasoft;
        
                // set the session identifier
                Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
        
                // specify web services, which should be used by Vintasoft Web Document Viewer
                Vintasoft.Shared.WebServiceJS.defaultFileService =
                    new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftFileApi");
                Vintasoft.Shared.WebServiceJS.defaultImageCollectionService =
                    new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftImageCollectionApi");
                Vintasoft.Shared.WebServiceJS.defaultImageService =
                    new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftImageApi");
                Vintasoft.Shared.WebServiceJS.defaultAnnotationService =
                    new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftAnnotationCollectionApi");
        
                // create settings for document viewer with annotation support
                let docViewerSettings =
                    new Vintasoft.Imaging.DocumentViewer.WebDocumentViewerSettingsJS("documentViewerContainer", { annotations: true });
        
                // get items of document viewer
                let items = docViewerSettings.get_Items();
        
                // get the main menu of document viewer
                let mainMenu = items.getItemByRegisteredId("mainMenu");
                // if main menu is found
                if (mainMenu != null) {
                    // get items of main menu
                    let mainMenuItems = mainMenu.get_Items();
        
                    // add "Annotation" menu panel to the main menu
                    mainMenuItems.addItem("annotationsMenuPanel");
                }
        
                // get side panel of document viewer
                let sidePanel = items.getItemByRegisteredId("sidePanel");
                // if side panel is found
                if (sidePanel != null) {
                    // get items of side panel
                    let sidePanelItems = sidePanel.get_PanelsCollection();
        
                    // add "Annotations" panel to the side panel
                    sidePanelItems.addItem("annotationsPanel");
        
                    // add "Text selection" panel to the side panel
                    sidePanelItems.addItem("textSelectionPanel");
        
                    // add "Text searh" panel to the side panel
                    sidePanelItems.addItem("textSearchPanel");
                }
        
                // create the document viewer
                let docViewer = new Vintasoft.Imaging.DocumentViewer.WebDocumentViewerJS(docViewerSettings);
        
                // create visual tool that allows to work with annotations, navigate document and select text
                let annotationNavigationTextSelectionTool =
                    docViewer.getVisualToolById("AnnotationVisualTool,DocumentNavigationTool,TextSelectionTool");
                // specify visual tool as mandatory visual tool of document viewer
                docViewer.set_MandatoryVisualTool(annotationNavigationTextSelectionTool);
                // specify visual tool as current visual tool of document viewer
                docViewer.set_CurrentVisualTool(annotationNavigationTextSelectionTool);
        
                // get image viewer of document viewer
                let imageViewer = docViewer.get_ImageViewer();
                // open file from session folder and add images from file to the image viewer
                imageViewer.get_Images().openFile("VintasoftImagingDemo.pdf");
            }
        
        }
        
        

      • Add created React.js component to the React.js application code - "ClientApp\src\App.js" file.


        Here are source codes of "App.js" file after update:
        import React, { Component } from 'react';
        import { Route } from 'react-router';
        import { Layout } from './components/Layout';
        import { DocumentViewerDemo } from './components/DocumentViewerDemo';
        
        import './custom.css'
        
        export default class App extends Component {
          static displayName = App.name;
        
          render () {
            return (
                <Layout>
                    <Route exact path='/' component={DocumentViewerDemo} />
              </Layout>
            );
          }
        }
        
        
    10. Run the ASP.NET Core Web application with React.js and see the result.