Barcode Scanning Needed for nopCommerce

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi  

We use a barcode scanner to scan a barcode into a field on form - plug a scanner into the computer and it looks just like a keyboard puting the data in the current field. Then we use the value to read and display product data.

Here are the main bits of code from 1.7 PDFhelper to print a barcode on the PDF Invoice and as such can be used to print product or SKU barcodes for scanning back into your field on the form.

namespace NopSolutions.NopCommerce.BusinessLogic.Utils
{
    /// <summary>
    /// Represents a PDF helper
    /// </summary>
    public class PDFHelper
    {
        #region Methods

        static void AddTicketLogoHeader(Document document, Order order, Product Product, int languageId, string filePath)
        {

            Section section = document.LastSection;

            var table = section.AddTable();
            table.Borders.Visible = false;
            table.AddColumn(Unit.FromCentimeter(9));
            table.AddColumn(Unit.FromCentimeter(9));

            var ordRow = table.AddRow();

            if (File.Exists(LogoFilePath))
                ordRow[0].AddImage(LogoFilePath);

            string barCodeImgPath = filePath + ".jpg";
            GenerateBarCode(String.Format("{0:00}{1:00000}", Product.EventVenueId, order.OrderId), barCodeImgPath);
            ordRow[1].AddImage(barCodeImgPath);
            ordRow[1].AddParagraph();
            ordRow[1].AddParagraph();

            var sup = ordRow[1].AddParagraph("     " + SettingManager.StoreUrl.Trim(new[] { '/' }));
            sup.AddHyperlink(SettingManager.StoreUrl, HyperlinkType.Url);
            sup.Format.Font.Bold = true;
            ordRow[1].AddParagraph("     " + LocalizationManager.GetLocaleResourceString("PDFInvoice.ABN", languageId)).Format.Font.Bold = true;
            ordRow[1].AddParagraph("     " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.OrderDate", languageId), DateTimeHelper.ConvertToUserTime(order.CreatedOn, DateTimeKind.Utc).ToString("D"))).Format.Font.Bold = true;
            ordRow[1].AddParagraph();
            ordRow[1].AddParagraph("     " + String.Format(LocalizationManager.GetLocaleResourceString("PDFTicketInvoice.Order#", languageId), order.OrderId)).Format.Font.Bold = true;
        }


        /// <summary>
        /// Print a ticket
        /// </summary>
        /// <param name="order">Order</param>
        /// <param name="languageId">Language identifier</param>
        /// <param name="filePath">File path</param>          
        public static void PrintTicket(Order order, int languageId, string filePath)
        {
            if (order == null)
                throw new ArgumentNullException("order");

            if (String.IsNullOrEmpty(filePath))
                throw new ArgumentNullException("filePath");

            var language = LanguageManager.GetLanguageById(languageId);
            if (language == null)
                throw new NopException("Language could not be loaded");

            var document = new Document();
            var section = document.AddSection();
            var pageSetup = DefineTicketPageSetup(section);
            section.PageSetup = pageSetup;

            DefineTicketStyles(document);

            int n = 0;

            AddTicketLogoHeader(document, order, Product, languageId, filePath);
            
            // add the rest od the PDF order...................


            var renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always)
            {
                Document = document
            };

            renderer.RenderDocument();
            renderer.PdfDocument.Save(filePath);
        }


        #endregion

        #region Properties

        /// <summary>
        /// Gets a file path to PDF logo
        /// </summary>
        public static string LogoFilePath
        {
            get
            {
                return HttpRuntime.AppDomainAppPath + "images/pdflogo.png";
            }
        }

        public static string BarCodeFontPath
        {
            get
            {
                return HttpRuntime.AppDomainAppPath + "files/";
            }
        }

        public static string BarCodeFont
        {
            get
            {
                string font = SettingManager.GetSettingValue("Print.Ticket.BarCodeFontName");
                return font;
            }
        }

        #endregion

        #region Utils

        private static void GenerateBarCode(string barCode, string filePath)
        {
            var pfc = new PrivateFontCollection();
            string font = BarCodeFontPath + SettingManager.GetSettingValue("Print.Ticket.BarCodeFontName") + ".ttf";
            pfc.AddFontFile(font);
            var code39FontFamily = new FontFamily(BarCodeFont, pfc);
            int size = SettingManager.GetSettingValueInteger("Print.Ticket.BarCodeFontSize");
            var code39Font = new System.Drawing.Font(code39FontFamily, size);
            barCode = "*" + barCode + "*";

            using (var fStream = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                var barCodeSize = SizeF.Empty;

                using (var bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
                using (var g = Graphics.FromImage(bmp))
                    barCodeSize = g.MeasureString(barCode, code39Font);
                using (var bmp = new Bitmap((int)barCodeSize.Width, (int)barCodeSize.Height, PixelFormat.Format32bppArgb))
                {
                    using (var objGraphics = Graphics.FromImage(bmp))
                    {
                        objGraphics.FillRectangle(new SolidBrush(System.Drawing.Color.White), new Rectangle(0, 0, bmp.Width, bmp.Height));
                        objGraphics.DrawString(barCode, code39Font, new SolidBrush(System.Drawing.Color.Black), 0, 0);
                    }
                    bmp.Save(fStream, ImageFormat.Jpeg);
                }
            }
        }

        #endregion
    }
}
11 years ago
Maybe you can consider to cooperate with us - Nova Software.

Nova is Microsoft gold certified partner and nopCommerce partner.  Nova has been dedicated in software outsourcing industry for over 7 years, providing web development and mobile development services.  Nova has accumulated abundant experience in NopCommerce.

You can visit our company's nopCommerce page to see more information.  In our page, you can see our services, demo stores, open source modules.
11 years ago
hi
like the others i am searching for a solution to tie up nopcom with POS and bar codes ? I have an online shop and physical shop in 2 different locations
Richard [email protected]
10 years ago
garrie007 wrote:
Hi,

I also would like the barcode scanning option.

can this barcode scanning help?
10 years ago
That didn't come out as a link
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.