VintaSoft Barcode .NET SDK 14.3: Documentation for Web developer
In This Topic
    Generate barcode image in ASP.NET Core application with Angular
    In This Topic
    This tutorial shows how to create a blank ASP.NET Core Web application in Visual Studio .NET 2022 and generate barcode image in ASP.NET Core application with Angular.

    Here are steps, which must be done:
    1. Create a blank ASP.NET Core Web application.

      Open Visual Studio .NET 2022 and create a new project, of ASP.NET Core with Angular application type:
      Open Visual Studio .NET 2022 and create a new project, of ASP.NET Core with Angular Application type
      Configure the project to use .NET 7.0:
      Configure the project to use .NET 7.0
    2. Server side: Add references to the Vintasoft assemblies to ASP.NET Core Web application.

      Add references to the Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll, Vintasoft.Shared.dll, Vintasoft.Shared.Web.dll, Vintasoft.Barcode.Web.Services.dll and Vintasoft.Barcode.AspNetCore.ApiControllers.dll assemblies from "<InstallPath>\VintaSoft Barcode .NET v14.3\Bin\DotNet7\AnyCPU\" folder in ASP.NET Core Web application.
      Comment: Reference to Vintasoft.Barcode.SkiaSharp.dll assembly is necessary only if SDK should draw text value of barcode on barcode image. Vintasoft.Barcode.ImageSharp.dll can be used instead of Vintasoft.Barcode.SkiaSharp.dll assembly.

      Add references to the Vintasoft assemblies
    3. Server side: Add Web API controller that allows to generate barcode image.

      • Press the right mouse button on the "Controllers" folder and select the "Add => Controller..." menu from context menu
        Select 'Add => Controller' context menu for 'Controllers' folder
      • Select Empty API controller template, set the controller name to the "MyVintasoftBarcodeApiController" and press the "Add" button
        Select Empty API controller template, set the controller name to the MyVintasoftBarcodeApiController and press the Add button
      • Specify that MyVintasoftBarcodeApiController class is derived from Vintasoft.Barcode.AspNetCore.ApiControllers.VintasoftBarcodeApiController class
        Source codes of Barcode API controller
        Here are source codes of MyVintasoftBarcodeApiController class:
        using Microsoft.AspNetCore.Hosting;
        using Microsoft.AspNetCore.Mvc;
        using Vintasoft.Barcode.AspNetCore.ApiControllers;
        
        namespace Project1.Controllers
        {
            public class MyVintasoftBarcodeApiController : VintasoftBarcodeApiController
            {
        
                public MyVintasoftBarcodeApiController(IWebHostEnvironment hostingEnvironment)
                    : base(hostingEnvironment)
                {
                }
        
            }
        }
        
    4. Compile the project for restoring dependencies using 'npm'.

    5. Client side: Delete files, which are not necessary in this demo.

      Delete directories "ClientApp\src\app\counter\", "ClientApp\src\app\fetch-data\", "ClientApp\src\app\home\", "ClientApp\src\app\nav-menu\" - these Angular components are not necessary in this demo.

      Delete files "WeatherForecast.cs" and "Controllers\WeatherForecastController.cs" - the WeatherForecast Web API controller is not necessary in this demo.

      Delete unnecessary files
      Open file "ClientApp\src\app\app.component.html" and delete line "<app-nav-menu></app-nav-menu>" from HTML markup - this demo does not need navigation menu.

      Open file "ClientApp\src\app\app.module.ts" and delete code that uses the following Angular components: NavMenuComponent, HomeComponent, CounterComponent, FetchDataComponent - these Angular components are not necessary in this demo.
      Here are source codes of "app.module.ts" file after update:
      import { BrowserModule } from '@angular/platform-browser';
      import { NgModule } from '@angular/core';
      import { FormsModule } from '@angular/forms';
      import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
      import { RouterModule } from '@angular/router';
      
      import { AppComponent } from './app.component';
      
      @NgModule({
        declarations: [
          AppComponent,
        ],
        imports: [
          BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
          HttpClientModule,
          FormsModule,
          RouterModule.forRoot([
          ])
        ],
        providers: [],
        bootstrap: [AppComponent]
      })
      export class AppModule { }
      
      
    6. Client side: Add JavaScript files and TypeScript modules to the ASP.NET Core Web application.

      Add the "assets" folder to the "ClientApp\src\app\" folder.

      Copy Vintasoft.Shared.js, Vintasoft.Shared.d.ts, Vintasoft.Barcode.js and Vintasoft.Barcode.d.ts files from "<InstallPath>\VintaSoft Barcode .NET v14.3\Bin\JavaScript\" folder to the "ClientApp\src\app\assets\" folder.
      Add references to Vintasoft JavaScript and TypeScript files
      Set "Build Action" to the "TypeScript file" for Vintasoft.Shared.d.ts and Vintasoft.Barcode.d.ts files:
      Set 'Build Action' to the 'TypeScript file' for Vintasoft.Shared.d.ts and Vintasoft.Barcode.d.ts files

      Add references to Vintasoft JavaScript files to the "projects => Demo => architect => build => options => scripts" section in "ClientApp\angular.json" file:
      ...
      "scripts": [
        "src/app/assets/Vintasoft.Shared.js",
        "src/app/assets/Vintasoft.Barcode.js"
      ]
      ...
      

      Add references to Vintasoft JavaScript files to the Scripts section in angular.json file
    7. Client side: Create Angular component that generates and displays barcode image.

      • Create folder "ClientApp\src\app\barcode-generator-demo\".

        Create "barcode-generator-demo.component.html" file that will contain HTML markup of Angular component:
        • Select "Add => New Item..." context menu for folder "ClientApp\src\app\barcode-generator-demo\" => "Add new item" dialog will appear
        • Select "HTML Page" type for new item
        • Set "barcode-generator-demo.component.html" as element name
        • Click the "Add" button => dialog will be closed and file "barcode-generator-demo.component.html" will be added into folder "ClientApp\src\app\barcode-generator-demo\"

        Add HTML markup (demo header and image element that will display generated barcode image) to the "barcode-generator-demo.component.html" file:
        <h1>Angular Barcode Generator Demo</h1>
        
        <img id="barcodeImage" src="" />
        
        
        Add HTML markup to barcode-generate-demo.component.html file

        Create "barcode-generator-demo.component.ts" file that will contain code of Angular component:
        • Select "Add => New Item..." context menu for folder "ClientApp\src\app\barcode-generator-demo\" => "Add new item" dialog will appear
        • Select "TypeScript File" type for new item
        • Set "barcode-generator-demo.component.ts" as element name
        • Click the "Add" button => dialog will be closed and file "barcode-generator-demo.component.ts" will be added into folder "ClientApp\src\app\barcode-generator-demo\"

        Add TypeScript code to the "barcode-generator-demo.component.cs" file:
        import { Component } from '@angular/core';
        
        @Component({
          selector: 'barcode-generator-demo',
          templateUrl: './barcode-generator-demo.component.html'
        })
        export class BarcodeGeneratorDemoComponent {
        
          ngOnInit() {
            // generate image of QR barcode with value "12345"
            this.generate2dBarcodeImage("QR", "12345");
          }
        
          /**
           * Generates 1D barcode image.
           * @param barcodeType Barcode type.
           * @param barcodeValue Barcode value.
          */
          public generate1dBarcodeImage(barcodeType: string, barcodeValue: string) {
            // set the session identifier
            Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
        
            // create web service that allows to generate barcode
            let barcodeService: Vintasoft.Shared.WebServiceJS = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
        
            // create the barcode writer
            let barcodeWriter: Vintasoft.Barcode.WebBarcodeWriterJS = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
        
            // create the barcode writer settings for generating 2D barcode
            let barcodeWriterSettings: Vintasoft.Barcode.Web1DBarcodeWriterSettingsJS = new Vintasoft.Barcode.Web1DBarcodeWriterSettingsJS();
            // specify that barcode writer must generate QR barcode image
            barcodeWriterSettings.set_BarcodeType(new Vintasoft.Barcode.WebBarcodeTypeEnumJS(barcodeType));
            // specify the QR barcode value
            barcodeWriterSettings.set_Value(barcodeValue);
        
            // specify settings for barcode writer
            barcodeWriter.set_Settings(barcodeWriterSettings);
        
            // send an asynchronous request for getting barcode image as Base64 string
            barcodeWriter.getBarcodeAsBase64Image(this.__writeBarcode_success, this.__writeBarcode_failed);
          }
        
          /**
           * Generates 2D barcode image.
           * @param barcodeType Barcode type.
           * @param barcodeValue Barcode value.
          */
          public generate2dBarcodeImage(barcodeType: string, barcodeValue: string) {
            // set the session identifier
            Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
        
            // create web service that allows to generate barcode
            let barcodeService: Vintasoft.Shared.WebServiceJS = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
        
            // create the barcode writer
            let barcodeWriter: Vintasoft.Barcode.WebBarcodeWriterJS = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
        
            // create the barcode writer settings for generating 2D barcode
            let barcodeWriterSettings: Vintasoft.Barcode.Web2DBarcodeWriterSettingsJS = new Vintasoft.Barcode.Web2DBarcodeWriterSettingsJS();
            // specify that barcode writer must generate QR barcode image
            barcodeWriterSettings.set_BarcodeType(new Vintasoft.Barcode.WebBarcodeTypeEnumJS(barcodeType));
            // specify the QR barcode value
            barcodeWriterSettings.set_Value(barcodeValue);
        
            // specify settings for barcode writer
            barcodeWriter.set_Settings(barcodeWriterSettings);
        
            // send an asynchronous request for getting barcode image as Base64 string
            barcodeWriter.getBarcodeAsBase64Image(this.__writeBarcode_success, this.__writeBarcode_failed);
          }
        
          /**
           * Barcode is generated successfully.
           * @param data Object that stores response from barcode service.
           */
          private __writeBarcode_success(data: Vintasoft.Barcode.WebBarcodeWriteResponseParamsJS) {
            if (data.success) {
              let barcodeImage: string = data.barcodeImage;
              let barcodeImageElement: HTMLImageElement = document.getElementById("barcodeImage") as HTMLImageElement;
              barcodeImageElement.src = barcodeImage;
            }
            else {
              alert(data.errorMessage);
            }
          }
        
          /**
           * Barcode generation is failed.
           * @param data Object with information about error.
           */
          private __writeBarcode_failed(data) {
            // show information about error
            alert(data.errorMessage);
          }
        
        }
        
        
        Add TypeScript code to the barcode-reader-demo.component.cs file

      • Add created Angular component to the Angular application module - "ClientApp\src\app\app.module.ts" file.
        Add created Angular component to the Angular application module

        Here are source codes of "app.module.ts" file after update:
        import { BrowserModule } from '@angular/platform-browser';
        import { NgModule } from '@angular/core';
        import { FormsModule } from '@angular/forms';
        import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
        import { RouterModule } from '@angular/router';
        
        import { AppComponent } from './app.component';
        import { BarcodeGeneratorDemoComponent } from './barcode-generator-demo/barcode-generator-demo.component';
        
        @NgModule({
          declarations: [
            AppComponent,
            BarcodeGeneratorDemoComponent
          ],
          imports: [
            BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
            HttpClientModule,
            FormsModule,
            RouterModule.forRoot([
              { path: '', component: BarcodeGeneratorDemoComponent, pathMatch: 'full' },
            ])
          ],
          providers: [],
          bootstrap: [AppComponent]
        })
        export class AppModule { }
        
        
    8. Run the ASP.NET Core Web application with Angular and see the result.

      Barcode generation result in ASP.NET Core Web application with Angular