ASP.NET Core PDF Editor
How to read, view, edit a scanned PDF file in C# ASP.NET MVC web application?


How to open, convert a scanned image PDF file to text editable, searchable PDF, view, edit PDF online in ASP.NET Core, MVC, framework using C#





Open, read, view and edit an existing scanned PDF file in ASP.NET using C# is very simple. EdgePDF provides a simple C# demo code to accomplish the task.

  • Read a scanned PDF file from server side file system
  • Convert the scanned PDF file to a editable PDF document, which you can do text searching on it.
  • Load the editable PDF file into EdgePDF, where you can read, view, edit PDF in web browser

How to view, edit a scanned PDF file in asp.net using C#

  1. Download EdgePDF asp.net PDF viewer web control
  2. Install EdgePDF demo project in IIS
  3. Follow step by step tutorial






Preparation



To run the following tutorial successfully, we need the following setup ready.



  1. For ASP.NET Core web app: Setup EdgePDF


  2. For ASP.NET Core MVC web app: enable PDF viewer function using EdgePDF


  3. For ASP.NET (.net framework): Setup EdgePDF on IIS


  4. Prepare OCR resource files. Copy all contents from downloaded package /OCR Files/Source/ to {EdgePDF demo project folder}/OCRSource/


  5. A demo scanned PDF file from C:\temp\pdf-scanned-1.pdf






How to read, view, edit a scanned PDF file online in ASP.NET C#?



The following steps and C# source code will help to setup a demo project, which allows you to open, view, edit a scanned PDF file in web browser using ASP.NET C# code.

After you have completed the following guide, you can open a scanned PDF file for editing through url (a sample url http://localhost:56643/?yourtarget=pdf-scanned-1.pdf)

  • For ASP.NET Core web app, open file UserCommandProcessMiddleware.cs from /DemoProjects/EdgePDF for ASP.NET Core/

  • For ASP.NET (.net framework) project, open file UserCommandProcessHandler.ashx from {EdgePDF demo project}/RasterEdge_Resource_Files/

  • Go to method FileProcess()
  • Get target scanned PDF file id from url parameter "yourtarget"
  • Create a new PDFDocument object with scanned PDF file loaded
  • Get the scanned PDF file pages count
  • Create a new MemoryStream object for converted editable PDF document
  • Set OCR resource files location
  • For each scanned PDF page, using OCR engine to convert to a editable PDF page, and save editable PDF page to a Stream object
  • Combine all editable PDF pages into the new editable PDF document
  • Call method REProcessControl.PageLoadFile() to load editable PDF document into EdgePDF for viewing, editing


    public override PDFWebDocument FileProcess()
    {
        HttpRequest request = this.Context.Request;

        if (!String.IsNullOrEmpty(request.QueryString["yourtarget"]))
        {
            // load file
            String docid = request.QueryString["yourtarget"];
            if (docid == null) docid = "0";
            String fileName = docid;

            PDFDocument scannedDoc = new PDFDocument(@"C:\\temp\" + docid);
            int pageCount = scannedDoc.GetPageCount();

            MemoryStream editableDoc = new MemoryStream();

            // The folder that contains OCR '.traineddata' files.
            OCRHandler.SetTrainResourcePath(@"C:\Sites\EdgePDF.Sample\OCRSource");

            MemoryStream[] streams = new MemoryStream[pageCount];
            for (int i = 0; i < scannedDoc.GetPageCount(); i++)
            {
                streams[i] = new MemoryStream();
                OCRPage page = OCRHandler.Import(scannedDoc.GetPage(i));
                page.Recognize();
                page.SaveTo(MIMEType.PDF, streams[i]);
            }
            PDFDocument.CombineDocument(streams, editableDoc);

            return REProcessControl.PageLoadFile(request, this.Manager, editableDoc, fileName);
        }
        else
        {
            Logger.Log(">>> Unknown load file mode. Load default file defined in Web.config.", LogType.DEBUG);

            // load default file. defined in Web.config
            return REProcessControl.PageLoadFile(request, this.Manager, "", LoadType.Server);
        }
    }


After complete the ASP.NET web application, you could easily publish the ASP.NET Core web app to Azure cloud service. View details at How to deploy ASP.NET Core PDF viewer web application to Azure?