Step-by-Step Guide for Integrating Anyline Web SDK into an ASP.NET Web Forms Project
The following Guide can be implemented in Visual Studio on Windows. Please make sure to have the latest version of Visual Studio for Windows before proceeding.
Development Tools
Should you not already have Visual Studio for Windows installed, Download here.
Setting Up a Web Application Project
Open the installed Visual Studio.
Click on the “File” menu, then “New”, and then “Project…”.
In the “New Project” dialog, select “ASP.NET Web Application (.NET Framework)” under the “Web” category.
On the next page, provide a Project Name and Solution Name. Other checkboxes are optional.
Choose “Web Forms” for the template.
Required Extensions and Packages
The Web SDK contains a bunch of TypeScript files that are required in order to successfully integrate the SDK and scan with it. Unlike .NET 6.0
. the .NET Framework 4.7.2
does not come with the extensions and packages required to read and run these files. Here is a list of the following Extensions/Packages that you would require before continuing to integrate the Web SDK:
Extensions
To handle TypeScript files in your ASP.NET Project, you should install the Microsoft.TypeScript.MSBuild
NuGet package. This package integrates TypeScript support into your project, allowing you to compile TypeScript files using MSBuild
.
It should look something like this:
Packages
Here is a summary of the commands you need to run:
npm install typescript --save-dev
npm install react react-dom --save
npm install @types/node --save-dev
npm install @types/jest --save-dev
npm install tsconfig-paths --save-dev
Downloading Anyline Assets
There are two options for downloading Anyline Web SDK:
Using
npm
:
Install the@anyline/anyline-js
npm package and get the content fromnode_modules/@anyline
. Use the following commands in an empty folder:npm init -y npm install @anyline/anyline-js npm install
Once done, it should look something like this:
PLEASE NOTE:
For the above example, I created an Empty Folder called “Web SDK” only for demonstrative purposes. Name your Empty Folder however you wish.
Downloading from GitHub:
Download the assets from the GitHub repository: anyline-ocr-anylinejs-module.
Placing Anyline Web SDK Content in the ASP.NET Web Forms Project
Place the Anyline Web SDK content inside the Scripts
folder in your project. For example, you can copy it to the Scripts
folder.
It should look something like this:
Required tsconfig.json
File
In order for all the syntax contained inside the Web SDK’s TypeScript files to be recognized and to avoid and Build Errors, create the following tsconfig.json
file inside your root
directory:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./Scripts",
"rootDir": "./TypeScript",
"strict": true,
"jsx": "react", // or "react-jsx" if you're using React 17+
"lib": [ "es2015", "es2015.iterable", "dom" ],
"types": [ "node", "jest" ],
"esModuleInterop": true
},
"include": [
"Scripts/anyline-js/types/**/*.ts",
"Scripts/anyline-js/types/**/*.tsx" // Include JSX/TSX files
],
"exclude": [
"Scripts/anyline-js/types/exclude-this-folder" // Adjust as needed
]
}
Calling Anyline Web SDK
Make the following changes in the Site.Master
file
Import the anyline.js
file:
<asp:ScriptReference Path="~/Scripts/anyline-js/anyline.js" />
PLEASE NOTE:
Please be sure to add the following inside your ScriptManager
inside the Site.Master
file.
The newly updated ScriptManager
should look like this:
<asp:ScriptManager runat="server">
<Scripts>
<%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
<%--Framework Scripts--%>
<asp:ScriptReference Path="~/Scripts/anyline-js/anyline.js" />
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
Make the following changes in the Default.aspx
file
Replace the default
<div>
element:<div id="root" style="height: 100vh"></div>
Important note: This
<div>
must have an id and a height specified in the style.Initialize the Anyline Web SDK:
<script> document.addEventListener('DOMContentLoaded', function () { const anylicense = '<LICENSE>'; const { init } = window.anylinejs; const anyline = init({ preset: 'all_barcode_formats', license: anylicense, element: document.getElementById('root'), }); anyline.onResult = result => { alert('GOT RESULT: ' + JSON.stringify(result.result)); }; anyline.onError = error => { console.error('GOT ERROR: ', error); }; anyline.startScanning().catch(error => { console.error('GOT ERROR: ', error); alert('GOT ERROR: ' + JSON.stringify(error)); }); }); </script>
The newly updated
Default.aspx
file should look like this:<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="<ProjectName>._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <script> document.addEventListener('DOMContentLoaded', function () { const anylicense = '<LICENSE>'; const { init } = window.anylinejs; const anyline = init({ preset: 'all_barcode_formats', license: anylicense, element: document.getElementById('root'), }); anyline.onResult = result => { alert('GOT RESULT: ' + JSON.stringify(result.result)); }; anyline.onError = error => { console.error('GOT ERROR: ', error); }; anyline.startScanning().catch(error => { console.error('GOT ERROR: ', error); alert('GOT ERROR: ' + JSON.stringify(error)); }); }); </script> <div id="root" style="height: 100vh"></div> </asp:Content>
IMPORTANT NOTE:
Replace
<ProjectName>
(on Line 1) with your actual Project Name.Replace
<LICENSE>
(on Line 7) with your actual license key.If you are using
all_barcode_formats
as the preset, opening browser development tools is not permitted. If browser development tools are opened, scanning will not start.
SUCCESS!
You can now run the project and see the result.
IMPORTANT!!!
The above sample serves Anyline trained data remotely. To serve it locally, you need to follow further steps.
Serving SDK Data Model Locally
Add the
anylinePath
config to theinit
function parameters:<script> document.addEventListener('DOMContentLoaded', function () { const anylicense = '<LICENSE>'; const { init } = window.anylinejs; const anyline = init({ anylinePath: '/Scripts/anyline-js/anylinejs', preset: 'all_barcode_formats', license: anylicense, element: document.getElementById('root'), }); anyline.onResult = result => { alert('GOT RESULT: ' + JSON.stringify(result.result)); }; anyline.onError = error => { console.error('GOT ERROR: ', error); }; anyline.startScanning().catch(error => { console.error('GOT ERROR: ', error); alert('GOT ERROR: ' + JSON.stringify(error)); }); }); </script>
The newly updated
Default.aspx
file should look like this:<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="<ProjectName>._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <script> document.addEventListener('DOMContentLoaded', function () { const anylicense = '<LICENSE>'; const { init } = window.anylinejs; const anyline = init({ anylinePath: '/Scripts/anyline-js/anylinejs', preset: 'all_barcode_formats', license: anylicense, element: document.getElementById('root'), }); anyline.onResult = result => { alert('GOT RESULT: ' + JSON.stringify(result.result)); }; anyline.onError = error => { console.error('GOT ERROR: ', error); }; anyline.startScanning().catch(error => { console.error('GOT ERROR: ', error); alert('GOT ERROR: ' + JSON.stringify(error)); }); }); </script> <div id="root" style="height: 100vh"></div> </asp:Content>
IMPORTANT NOTE:
Replace
<ProjectName>
(on Line 1) with your actual Project Name.Replace
<LICENSE>
(on Line 7) with your actual license key.
Update ASP.NET to serve unknown file types:
Add the following code in the
Web.config
file right inside the<configuration>
section:<system.webServer> <staticContent> <mimeMap fileExtension=".*" mimeType="application/octet-stream" /> </staticContent> </system.webServer>
SUCCESS!
You can now run the project and see the result.