ASP.NET Core PDF Viewer Web Control
How to open, view, edit PDF in browser in C# ASP.NET Core web applications


Complete how to example source code in C# to read, load, embed, view, annotate, convert, export pdf file in ASP.NET MVC, aspx on Visual Studio .NET using C# component control













Prerequisites







Create a ASP.NET Core emtpy web app with EdgePDF ASP.NET PDF Viewer web control installed using C#



Step 1: Create a new ASP.NET Core emtpy application in VS2022

  1. Start Visual Studio 2022 and select Create a new project.


  2. In the Create a new project dialog, select ASP.NET Core Empty, and click Next


  3. In the Configure your new project dialog, enter Project name EdgePDFDemo. Click button Next.


  4. In the Additional information dialog window:
    • Select .NET 6.0 (Long Term Support)


  5. Now, all auto-generated files could be found in the solution explorer







Step 2: Download and install Nuget packages

EdgePDF for ASP.NET requires the following Nuget packages:

  • System.Drawing.Common (Version 6.0.0 or later)
  • System.IO.Packaging (Version 6.0.0 or later)
  • Newtonsoft.Json (Version 11.0.2 or later)
  • WaterTrans.GlyphLoader (Version 1.0.0 or later)



  1. Right-click "Dependencies" in the Solution Explorer, and select "Manage NuGet Packages ...".


  2. Select "Browse" and use the search control to find the above four packages from the package source "nugget.org"


  3. Choose the minimum version or later to install the package


  4. Check "Packages" in the Solution Explorer to ensure the installation is success.





Step 3: Install EdgePDF DLLs and resource files



  1. Add all RasterEdge DLL references (add all DLLs with prefix "RasterEdge" in file name).

    Please go to download package "/Bin", and choose .NET Standard 2.0 DLLs.



  2. Copy all VC++ DLLs (both x86 and x64 versions) manually to the same folder as the assembly "RasterEdge.Imaging.Basic.dll".



  3. Manually copy the EdgePDF resource files from folder "/EdgePDFDemo/wwwroot" to the project



  4. Set the configuration file "appsettings.json"

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "AppSettings": {
        "reCacheFolder": "RasterEdge_Cache",
        "reCustomStampFolder": "RasterEdge_Resource_Files/images/stamp",
        //  Set the server folder if you want to open the file on server.
        "reDefaultFolder": "RasterEdge_Demo_Docs",
        //  Set the default file when open the web first time, if set "", this function is disabled. (reDefaultFolder needed)
        "reDefaultFilePath": "",
        //  Image quality in PDF document
        "rePageImageZoom": "2",
        "reImageZoom": "2",
        //  Image annotation in PDF document
        "reAnnotationImageZoom": "2",
        //  Write log 
        "reOutputLogData": "true",
        "reSDKLogerEnable": "true",
        //  DEBUG ,INFO ,WARNING,ERROR
        "reLogLevel": "DEBUG",
        "reWDPFileCacheMaxLimit": "0",
        "reWDPSinglePDFMaxLinkCount": "5000",
        //  If true, SDK will automatically convert many shape and path inside PDF page into image.
        "reIntelliDrawShapesToImage": "true",
        //  Valid values: "all", "firefox", "chrome", "ie", "edge", "salari", "others".
        //  If "all" included, ignore the rest;
        //  Default: "all"
        "reIntelliDrawShapesToImageSupportBrowsers": "all",
        //  Set the type of the show page.
        //  Valid values: "svg", "png", "html"
        //  Default: "html"
        "reDocRenderEngine": "svg",
        "reRestfulFolder": "c:/RasterEdge_Restful",
        "reI18NFolder": "/RasterEdge_Resource_Files/i18n/",
        //  Set the domain for remote client
        "reServerURL": "",
        //  If true, SDK will automatically all pages after file has been uploaded.
        //  Deafult: false.
        "autoProcessWholeDocument": "false",
        //  If true, SDK will automatically render extract text content after file has been uploaded.
        //  Default: false.
        "autoIndexTextSearchWholeDocument": "false",
        //  Set the js response max try time when save file or save as.
        "reJSResponseMaxTryTime": "120"
      }
    }
    


  5. Copy the two middleware files "UserCommandProcessMiddleware.cs" and "DummyMiddleware.cs" to the project. You can get them at {Download Package}/DemoProjects/EdgePDF for ASP.NET Core/


  6. Copy the two middleware files "UserCommandProcessMiddleware.cs" and "DummyMiddleware.cs" to the project. You can get them at {Download Package}/DemoProjects/EdgePDF for ASP.NET Core/


  7. Replace all contents in the project file "Program.cs" by the following codes

    using EdgePDFDemo;
    using RasterEdge.WDP;
    
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddRazorPages();
    builder.Services.AddMemoryCache();
    
    var app = builder.Build();
    
    //  Config RasterEdge.WDP
    RasterEdge.WDP.REHttpRuntime.AppDomainAppPath = app.Environment.WebRootPath;
    RasterEdge.WDP.Logger.initLog();
    //  
    Scheduler.ClearAllFolder();
    Scheduler.StartFileCacheCleanTask(FileCacheCleanType.FileLifeMaxTime, new IntervalTime(3, 0, 0));
    Scheduler.StartFileCacheCleanTask(FileCacheCleanType.FileCacheCleanTime, new IntervalTime(3, 0, 0));
    Scheduler.StartUserCacheCleanTask(UserCacheCleanType.UserRequestMaxTime, new IntervalTime(3, 0, 0));
    Scheduler.StartUserCacheCleanTask(UserCacheCleanType.UserFileRequestCacheCleanTime, new IntervalTime(3, 0, 0));
    Logger.LogSystem("Cache Auto clear Start", LogType.INFO);
    
    if (app.Environment.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();
    }
    
    //  This middleware would send request to the local host every 15 minutes.
    //  Must before UseDefaultFiles & UseStaticFiles
    app.UseDummyMiddleware();
    
    app.UseDefaultFiles();
    app.UseStaticFiles();
    
    //  Must use the middleware after UseStaticFiles.
    app.MapWhen(context => context.Request.Path.Value.StartsWith("/UserCommandProcessMiddleware"),
                appBuilder => { appBuilder.UseUserCommandProcessMiddleware(); });
    
    app.Run();
    






Step 4: Run the web app in Kestrel server



  1. It is done. Now press "Ctrl+F5" to run the project.



    It launches the Kestrel server and opens the default web browser.





  2. Integrate EdgePDF ASP.NET PDF Editor with a new ASP.NET Core emtpy web app using C#



    When the browser displays PDF file pages using EdgePDF, the library will convert PDF pages to web browser supported SVG files in the ASP.NET web server, then send to the client's broswer to display the PDF page content. SVG file format is perfect for rendering vector document content, such as PDF. However SVG is not good for editing text and image contents online in browser.

    To support PDF text and image editing function in browser, EdgePDF library will convert PDF pages to html files in the ASP.NET web server. To enable this feature in your ASP.NET Core web application, please follow the guides below.

    1. Create a new ASP.NET Core web app with EdgePDF integrated

    2. Open app config file appsettings.json, change property reDocRenderEngine value to html. By default it is "svg".

    3. Run the application.





    Publish, deploy ASP.NET Core PDF Viewer web application to Azure



    You can easily deploy the existing ASP.NET Core web app with EdgePDF integrated to Microsoft Azure Windows app service. Please view the details at How to enable ASP.NET Core PDF Viewer web application on Azure?