ByteScout Barcode Reader SDK – ASP.NET MVC C# – Decode page by page from PDF OR TIFF

  • Home
  • /
  • Articles
  • /
  • ByteScout Barcode Reader SDK – ASP.NET MVC C# – Decode page by page from PDF OR TIFF

ByteScout Barcode Reader SDK – ASP.NET MVC C# – Decode page by page from PDF OR TIFF

HomeController.cs

using Bytescout.BarCodeReader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace BarcodeReaderFromDoc.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Process uploaded file and try to read barcodes from it
        /// </summary>
        [HttpPost]
        public ActionResult ProcessFile(HttpPostedFileBase UploadedFile, int PageNo)
        {
            List<string> lstResult = new List<string>();

            try
            {
                // Save uploaded file
                if (UploadedFile.ContentLength > 0)
                {

                    // Barcode reader
                    Reader barcodeReader = new Reader();

                    // Limit search to 1D barcodes only (exclude 2D barcodes to speed up the search).
                    // Change to barcodeReader.BarcodeTypesToFind.SetAll() to scan for all supported 1D and 2D barcodes 
                    // or select specific type, e.g. barcodeReader.BarcodeTypesToFind.PDF417 = True
                    barcodeReader.BarcodeTypesToFind.SetAll1D();

                    // Reading barcode from document on page-by-page basic
                    barcodeReader.ReadFromStream(UploadedFile.InputStream, (PageNo - 1), 1);

                    // Get all found barcodes for that page
                    FoundBarcode[] barcodes = barcodeReader.FoundBarcodes;

                    lstResult.Add($"Reading barcode(s) from file {Path.GetFileName(UploadedFile.FileName)}, Page No: {PageNo}");

                    if (barcodes.Length == 0)
                    {
                        lstResult.Add("No barcodes found");
                    }
                    else
                    {
                        foreach (FoundBarcode barcode in barcodes)
                        {
                            lstResult.Add(String.Format("Found barcode - Type: '{0}', Value: '{1}', Position: '{2}'", barcode.Type, barcode.Value, barcode.Rect.ToString()));
                        }
                    }
                }
                else
                {
                    lstResult.Add("Please upload document");
                }
            }
            catch (Exception ex)
            {
                lstResult = new List<string> { ex.Message };
            }

            return View("Result", lstResult);
        }

    }
}

web.config

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="BarcodeReaderFromDoc" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime maxRequestLength="1048576" targetFramework="4.5.2"/>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>


  Click here to get your Free Trial version of the SDK

Tutorials:

prev
next