How to upload PDF document in ASP.NET application and then read barcodes from PDF using Bytescout BarCode Reader SDK for ASP.NET

  • Home
  • /
  • Articles
  • /
  • How to upload PDF document in ASP.NET application and then read barcodes from PDF using Bytescout BarCode Reader SDK for ASP.NET

How to upload PDF in ASP.NET and then read barcodes from uploaded PDF using Bytescout BarCode Reader SDK for .NET and ASP.NET

You can find full source code of this sample in ExamplesAdvanced ExamplesASP.NET folder of Bytescout BarCode Reader SDK installation.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTestSharp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Web Barcode Reader Tester (C#)</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Click browse button to upload a PDF document<br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button> 
        <br />
       <br />
        <asp:Label id="UploadStatusLabel" Text="" runat="server"></asp:Label> 
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Visible="False"></asp:ListBox><br />
        <br />
        </div>
    </form>
</body>
</html>

Visual Basic in ASP.NET (Default.aspx.vb)

Imports System.Drawing

Imports Bytescout.BarCodeReader


Public Partial Class _Default
	Inherits System.Web.UI.Page
	Protected Sub UploadButton_Click(sender As Object, e As EventArgs)
		Dim savePath As [String] = "uploads"

		If FileUpload1.HasFile Then
			Dim fileName As [String] = FileUpload1.FileName
			savePath += fileName
			FileUpload1.SaveAs(Server.MapPath(savePath))

            Dim barcodeReader As New Reader()

            			' reader.MediumTrustLevelCompatible = true ' uncomment this line to enable Medium Trust compatible mode (slows down the recognition process as direct image data access is disabled in Medium Trust mode)
			

            UploadStatusLabel.Visible = False
            ListBox1.Items.Clear()
            ListBox1.Visible = True

            ListBox1.Items.Add("Reading barcode(s) from PDF")

            Dim barcodes As FoundBarcode() = barcodeReader.ReadFrom(Server.MapPath(savePath))

            If barcodes.Length = 0 Then
                ListBox1.Items.Add("    No barcodes found")
            Else
                For Each barcode As FoundBarcode In barcodes
                    ListBox1.Items.Add([String].Format("    Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value))
                Next
            End If


        Else
            ' Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload."
        End If
	End Sub
End Class

Visual C# in ASP.NET (Default.aspx.cs)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing.Imaging;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Collections.Generic;

using Bytescout.BarCodeReader;

namespace WebTestSharp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String savePath = @"uploads";

            if (FileUpload1.HasFile)
            {
                String fileName = FileUpload1.FileName;
                savePath += fileName;
                FileUpload1.SaveAs(Server.MapPath(savePath));

				Reader barcodeReader = new Reader();

            				// reader.MediumTrustLevelCompatible = true; // uncomment this line to enable Medium Trust compatible mode (slows down the recognition process as direct image data access is disabled in Medium Trust mode)
					UploadStatusLabel.Visible = false;

                    ListBox1.Items.Clear();
                    ListBox1.Visible = true;
						ListBox1.Items.Add("Reading barcode(s) from image #" + imgNum);
						
						FoundBarcode[] barcodes = barcodeReader.ReadFrom(savePath);

						if (barcodes.Length == 0)
						{
							ListBox1.Items.Add("    No barcodes found");
						}
						else
						{
							foreach (FoundBarcode barcode in barcodes)
								ListBox1.Items.Add(String.Format("Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value));
						}

            }
            else
            {
                // Notify the user that a file was not uploaded.
                UploadStatusLabel.Text = "You did not specify a file to upload.";
            }
        }
    }
}


next