Reference Guide
Tutorials / ASP.NET Core / DocuVieware integration in .NET Core 3.0 web application
In This Topic
    DocuVieware integration in .NET Core 3.0 web application
    In This Topic
     Overview

    This tutorial will show you how to integrate DocuVieware™ in a newly created C# .NET Core MVC Web Application project with Razor view engine.

     Requirements

    Here are the necessary requirements:

    This tutorial has been done using .NET Core 3.0 v3.0.0-preview6.

    In Visual Studio 2019, you need to enable Preview Features:

    Open Tools > Options > Environment > Preview Features and select "Use previews of the .NET Core SDK" checkbox.

     Empty project creation

    Start with the File > New > Project... menu, then in Visual C# choose ASP.NET Core Web Application, please refer to the screenshot below.

    On the next page choose .NET Core with ASP.NET Core 3.0 and select Web Application MVC, please refer to the screenshot below.

    Now that the project structure is ready, the next step will be to add project references and set up the project configuration.

     Setting up the project configuration

    Step 1: The first is to add the reference to GdPicture.NET.14.WEB.DocuVieware.Core.dll that is found in:

    [INSTALLATION FOLDER]\Redist\DocuVieware (.NET Core 3.0)

    Step 2: It is required to also add Microsoft.AspNetCore.Mvc.NewtonsoftJson with NuGet manager:

     

    Step 3: You also need to add DocuVieware™ own JavaScript and CSS to the project, docuvieware-min.js and docuvieware-min.css, both found in:

    [INSTALLATION FOLDER]\Redist\DocuVieware (Resources)\

    Simply add them to the css and js folders of the project.


    Step 4: Finally, you also need to download jquery.min.js, for example from https://jquery.com/download/ or you can use also one attached to our samples. It is simply because this file will not be added automatically when creating the project.

     Setting up licensing

    Now that the references are properly set, you need to go to the Startup.cs file of the project to add some mandatory imports and handle the licensing part of DocuVieware™.

    You will probably need to import some dlls, please refer below.

    Importing the dll.
    Copy Code
    using System.IO;
    using GdPicture14.WEB;

    To properly unlock DocuVieware™ you are going to add a call to the RegisterKEY() method in the Startup method that is called on runtime. Then please enter your license key in the method.

    Unlocking the DocuVieware™.
    Copy Code
    DocuViewareLicensing.RegisterKEY("XXXX"); // Unlocking DocuVieware
    // You need to replace "XXXX" by your actual license key.
     Configuring DocuVieware

    Next step is to properly set up the core configuration of DocuVieware™ through the DocuViewareManager.SetupConfiguration() method. Add this call right after unlocking DocuVieware™.

    Configuring the DocuVieware™.
    Copy Code
    // DocuVieware Core Configuration
    DocuViewareManager.SetupConfiguration(true, DocuViewareSessionStateMode.File, Path.Combine(Directory.GetCurrentDirectory(), "Cache"), "/", "api/docuvieware3");

    Finally, you need to add this code to the ConfigureServices method in order to get the DocuVieware™ reference to work in the Razor views it will be integrated in.

    Add this code to the ConfigureServices method.
    Copy Code
    services.AddMvc(option => option.EnableEndpointRouting = false).AddNewtonsoftJson();

    Here is what the Startup.cs file should look like at this point:

    Startup.cs
    Copy Code
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using GdPicture14.WEB;
    using System.IO;
    
    namespace DocuViewareNetCore
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                // Unlocking DocuVieware
                DocuViewareLicensing.RegisterKEY("");
    
                // DocuVieware Core Configuration
                Configuration = configuration;
                DocuViewareManager.SetupConfiguration(true, DocuViewareSessionStateMode.File, Path.Combine(Directory.GetCurrentDirectory(), "Cache"), "/", "api/docuvieware3"); 
            }
    
            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)
            {
                services.Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                });
    
                services.AddMvc(option => option.EnableEndpointRouting = false).AddNewtonsoftJson();
    
                services.AddControllersWithViews();
                services.AddRazorPages();
            }
    
            // 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("/Home/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.UseCookiePolicy();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                    endpoints.MapRazorPages();
                });
            }
        }
    }
     DocuVieware integration

    First, you need an additional controller to handle and route the DocuVieware™ actions. Simply create a new controller called DocuVieware3Controller.cs in the project's Controllers folder as follow:

    Creating a new controller.
    Copy Code
    usingMicrosoft.AspNetCore.Mvc;
    usingGdPicture14.WEB;
    usingSystem.Net.Http;
    usingSystem;
    
    namespaceDocuViewareNetCore.Controllers
    {
        [Route("api/docuvieware3")]
        publicclassDocuVieware3Controller: Controller
        {
            [HttpPost("init")]
            publicstringInit([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.init(jsonString);
            }
            [HttpPost("close")]
            publicstringClose([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.close(jsonString);
            }
            [HttpPost("selectpage")]
            publicstringSelectPage([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.selectpage(jsonString);
            }
            [HttpPost("rotate")]
            publicstringRotate([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.rotate(jsonString);
            }
            [HttpPost("bookmarks")]
            publicstringBookmarks([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.bookmarks(jsonString);
            }
            [HttpPost("thumbnails")]
            publicstringThumbnails([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.thumbnails(jsonString);
            }
            [HttpPost("pageview")]
            publicstringPageView([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.pageview(jsonString);
            }
            [HttpPost("rundestination")]
            publicstringRunDestination([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.rundestination(jsonString);
            }
            [HttpPost("textsearch")]
            publicstringTextSearch([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.textsearch(jsonString);
            }
            [HttpPost("zoommode")]
            publicstringZoomMode([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.zoommode(jsonString);
            }
            [HttpPost("zoomset")]
            publicstringZoomSet([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.zoomset(jsonString);
            }
            [HttpPost("singlepageviewmode")]
            publicstringSinglePageViewMode([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.singlepageviewmode(jsonString);
            }
            [HttpPost("zoomin")]
            publicstringZoomIn([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.zoomin(jsonString);
            }
            [HttpPost("zoomout")]
            publicstringZoomOut([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.zoomout(jsonString);
            }
            [HttpPost("zoomrect")]
            publicstringZoomRect([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.zoomrect(jsonString);
            }
            [HttpPost("newannotation")]
            publicstringNewAnnotation([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.newannotation(jsonString);
            }
            [HttpPost("newannotationcomment")]
            publicstringNewAnnotationComment([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.newannotationcomment(jsonString);
            }
            [HttpPost("print")]
            publicstringPrint([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.print(jsonString);
            }
            [HttpPost("customaction")]
            publicstringCustomAction([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.customaction(jsonString);
            }
            [HttpGet("print")]
            publicHttpResponseMessage Print(stringsessionID, stringpageRange, boolprintAnnotations)
            {
                returnDocuViewareControllerActionsHandler.print(sessionID, pageRange, printAnnotations);
            }
            [HttpGet("save")]
            publicHttpResponseMessage Save(stringsessionID, stringfileName, stringformat, stringpageRange)
            {
                returnDocuViewareControllerActionsHandler.save(sessionID, fileName, format, pageRange);
            }
            [HttpGet("twainservicesetupdownload")]
            publicHttpResponseMessage TwainServiceSetupDownload(stringsessionID)
            {
                returnDocuViewareControllerActionsHandler.twainservicesetupdownload(sessionID);
            }
            [HttpPost("formfieldupdate")]
            publicstringFormfieldUpdate([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.formfieldupdate(jsonString);
            }
            [HttpPost("annotupdate")]
            publicstringAnnotUpdate([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.annotupdate(jsonString);
            }
            [HttpPost("loadfromuri")]
            publicstringLoadFromUri([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.loadfromuri(jsonString);
            }
            [HttpPost("loadfromfile")]
            publicstringLoadFromFile([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.loadfromfile(jsonString);
            }
            [HttpPost("setpassword")]
            publicstringSetPassword([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.setpassword(jsonString);
            }
            [HttpPost("movepage")]
            publicstringMovePage([FromBody]objectjsonString)
            {
                returnDocuViewareControllerActionsHandler.movepage(jsonString);
            }
        }
    } 

     You are going to integrate a DocuVieware™ instance in the Index view, but of course the exact same applies to any other view or content.

    For the sake of clarity, replace the entire content of the Index view, remove other views and the shared layouts as well, you won't use them in this tutorial.

    Create a div in Index.cshtml and insert the DocuVieware™ control in it. Then create a new DocuVieware™ object in the div and set its properties.

    Here is what the Index.cshtml finally looks like after the integration is complete:

    Integrating a DocuVieware™ instance in the Index view.
    Copy Code
    @usingSystem.IO; 
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    <html style="height: 100%">
    <head>
        <meta name="viewport"content="width=device-width"/>
        <title>DocuVieware - Annotations Demo.</title>
        <meta name="viewport"content="width=device-width, initial-scale=1"/>
        <link href="~/css/docuvieware-min.css"rel="stylesheet"type="text/css"/>
        <script src="~/js/jquery-3.1.0.min.js"></script>
        <script src="~/js/docuvieware-min.js"></script>
    </head>
    <body style="overflow: hidden; margin: 0; height: 100%;">
        <div style="width: 100%; height: 100%;">
            @{
                GdPicture14.WEB.DocuViewareControl docuVieware = new GdPicture14.WEB.DocuViewareControl("myControlSessionid", "DocuVieware1", 20)
                {
                    Height = "100%",
                    Width = "100%",
                    DisableAnnotationDrawingModePanel = true,
                    EnableMultipleThumbnailSelection = false,
                    EnableFormFieldsEdition = true
                };
                docuVieware.RenderControl(Output);
                docuVieware.Dispose();
            }
        </div>
    </body>
    </html> 
    You can find the sample and mentioned source codes in this folder: [INSTALLATION FOLDER]\Samples\ASP.NET\DocuVieware (.NET Core)\aspnetcore-mvc_razor_app

    That’s it! You can start the project and load documents in your brand new DocuVieware™.