Split PDF by Barcode | ASP.NET C#ByteScout Barcode Reader SDK

Split PDF by Barcode | ASP.NET C#

Default.aspx:

VB
<%@ 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 with a barcode inside so this PDF will be splitted by the page with barcode found<br />
        Choose sample "barcodes.pdf" file:
        <br />
        <asp:Label ID="LabelInputFile" runat="server" Text="LabelInputFile"></asp:Label><br />  
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button id="UploadButton" 
           Text="Upload And Split 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>

Default.aspx.cs:

C#
using System;
using System.IO;
using Bytescout.BarCodeReader;

namespace WebTestSharp
{
    /*
    IF YOU SEE TEMPORARY FOLDER ACCESS ERRORS: 

    Temporary folder access is required for web application when you use ByteScout SDK in it.
    If you are getting errors related to the access to temporary folder like "Access to the path 'C:\Windows\TEMP\... is denied" then you need to add permission for this temporary folder to make ByteScout SDK working on that machine and IIS configuration because ByteScout SDK requires access to temp folder to cache some of its data for more efficient work.

    SOLUTION:

    If your IIS Application Pool has "Load User Profile" option enabled the IIS provides access to user's temp folder. Check user's temporary folder

    If you are running Web Application under an impersonated account or IIS_IUSRS group, IIS may redirect all requests into separate temp folder like "c:\temp\".

    In this case
    - check the User or User Group your web application is running under
    - then add permissions for this User or User Group to read and write into that temp folder (c:\temp or c:\windows\temp\ folder)
    - restart your web application and try again

    */

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LabelInputFile.Text = MapPath("barcodes.pdf");
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String savePath = @"\uploads\";

            if (FileUpload1.HasFile)
            {
                String fileName = FileUpload1.FileName;
                fileName = savePath + fileName;
                // remove if already exists
                if (File.Exists(fileName))
                    File.Delete(fileName);

                FileUpload1.SaveAs(Server.MapPath(fileName));

                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();

                // 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("Searching for barcode(s) from PDF file \"" + fileName + "\"");

                FoundBarcode[] barcodes = barcodeReader.ReadFrom(Server.MapPath(fileName));

                if (barcodes.Length == 0)
                {
                    ListBox1.Items.Add("No barcodes found");
                }
                else
                {
                    // saving filename of the pdf file 
                    string lastFilename = fileName;

                    int partIndex = 0;

                    foreach (FoundBarcode barcode in barcodes)
                    {
                        // Split PDF document in two parts by found barcode
                        partIndex++;
                        barcodeReader.SplitDocument(
                            Server.MapPath(lastFilename), 
                            Server.MapPath(savePath+@"part-" + partIndex + ".pdf"),
                            Server.MapPath(savePath + @"part-last.pdf"), 
                            barcode.Page + 1);

                        ListBox1.Items.Add(String.Format("Splitting " + fileName + " file into 2 by barcode with type '{0}' and value '{1}' on page {2} at {3} ", barcode.Type, barcode.Value, barcode.Page, barcode.Rect.ToString()));
                        ListBox1.Items.Add("Saved:" + savePath + @"part-" + partIndex + ".pdf");

                        lastFilename = savePath + @"part-last.pdf";

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

Web.config:

<?xml version="1.0"?>

<configuration>
  
    <appSettings/>
    <connectionStrings/>
  
    <system.web>
        <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
        <compilation debug="true" />
        <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
        <authentication mode="Windows" />
        <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
</configuration>