Skip to main content

Add barcode on pdf

using System;

using System.IO;

using System.Drawing;

using ZXing;

using iTextSharp.text;

using iTextSharp.text.pdf;

using System.Drawing;

using System.Web.UI.WebControls;


namespace PDFBarcode

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        byte[] byteImage;

        protected void Page_Load(object sender, EventArgs e)

        {


        }


        protected void btnDownload_Click(object sender, EventArgs e)

        {

         

            Random r = new Random();

            string fileName = r.Next().ToString();

            string filenameext = fileName + ".pdf";


            GenerateBarcode(fileName);

            string Sourcepath = Server.MapPath("~/SavedFolder/data1.pdf");

            string Barcodepath = Server.MapPath("~/Barcode/"+ fileName+".jpg");

            string pathtosave = Server.MapPath("~/PdfBar/");

            

            using (Stream inputPdfStream = new FileStream(Sourcepath, FileMode.Open, FileAccess.Read, FileShare.Read))

            using (Stream outputPdfStream = new FileStream(pathtosave + filenameext, FileMode.Create, FileAccess.Write, FileShare.None))

            {

                var reader = new PdfReader(inputPdfStream);

                int cntPage = reader.NumberOfPages;

                var stamper = new PdfStamper(reader, outputPdfStream);


                for (int init = 1; init <= cntPage; init++)

                {

                    Stream inputImage = new FileStream(Barcodepath, FileMode.Open, FileAccess.Read);

                    var pdfContentByte = stamper.GetOverContent(init);

                    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImage);

                    image.SetAbsolutePosition(30, 30);

                    image.ScaleToFit(100f, 100f);

                    image.Border = iTextSharp.text.Rectangle.BOX;

                    image.BorderColor = iTextSharp.text.BaseColor.BLACK;

                    pdfContentByte.AddImage(image, false);

                 

                }

                

                stamper.Close();

           }


            Response.ContentType = "application/octet-stream";

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + filenameext);

            Response.TransmitFile(pathtosave + filenameext);

            Response.End();


        }






        private byte[] GenerateBarcode(string CodexPassno)

        {

            string barcodetext = CodexPassno;

            var writer = new BarcodeWriter

            {

                Format = BarcodeFormat.CODE_128

            };

            var bitmap = writer.Write(barcodetext);

            string folderLocation = Server.MapPath("~/Barcode/");

            bool exists = System.IO.Directory.Exists(folderLocation);

            if (!exists)

            {

                System.IO.Directory.CreateDirectory(folderLocation);

            }


            bitmap.Save(Server.MapPath("~/Barcode/" + barcodetext + ".jpg"));

            byteImage = ImageToByte2(bitmap);

            return byteImage;


        }


        private byte[] GenerateImage(string ImgFileName)

        {

            byte[] bytes = null;

            string FileName = ImgFileName;

            string folderLocation = Server.MapPath("~/Barcode/");

            bool exists = System.IO.Directory.Exists(folderLocation);

            if (!exists)

            {

                System.IO.Directory.CreateDirectory(folderLocation);

            }

            string strPath = Server.MapPath("~/Barcode/" + FileName);

            // string SaveCargoFileName = strPath.Substring(strPath.LastIndexOf('\\') + 1);

            bool existsImage = System.IO.File.Exists(strPath);

            if (existsImage)

            {

                using (FileStream file = new FileStream(strPath, FileMode.Open, FileAccess.Read))

                {

                    bytes = new byte[file.Length];

                    file.Read(bytes, 0, (int)file.Length);

                }

            }


            return bytes;


        }


        public static byte[] ImageToByte2(Bitmap img)

        {

            byte[] byteArray = new byte[0];

            using (MemoryStream stream = new MemoryStream())

            {

                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                stream.Close();

                byteArray = stream.ToArray();

            }

            return byteArray;

        }

    }

}





        private byte[] GenerateBarcode(string CodexPassno)

        {

            string barcodetext = CodexPassno;

            var writer = new BarcodeWriter

            {

                Format = BarcodeFormat.CODE_128

            };

            var bitmap = writer.Write(barcodetext);

            string folderLocation = Server.MapPath("~/Barcode/");

            bool exists = System.IO.Directory.Exists(folderLocation);

            if (!exists)

            {

                System.IO.Directory.CreateDirectory(folderLocation);

            }


            bitmap.Save(Server.MapPath("~/Barcode/" + barcodetext + ".jpg"));

            byteImage = ImageToByte2(bitmap);

            return byteImage;


        }


        private byte[] GenerateImage(string ImgFileName)

        {

            byte[] bytes = null;

            string FileName = ImgFileName;

            string folderLocation = Server.MapPath("~/Barcode/");

            bool exists = System.IO.Directory.Exists(folderLocation);

            if (!exists)

            {

                System.IO.Directory.CreateDirectory(folderLocation);

            }

            string strPath = Server.MapPath("~/Barcode/" + FileName);

            // string SaveCargoFileName = strPath.Substring(strPath.LastIndexOf('\\') + 1);

            bool existsImage = System.IO.File.Exists(strPath);

            if (existsImage)

            {

                using (FileStream file = new FileStream(strPath, FileMode.Open, FileAccess.Read))

                {

                    bytes = new byte[file.Length];

                    file.Read(bytes, 0, (int)file.Length);

                }

            }


            return bytes;


        }


        public static byte[] ImageToByte2(Bitmap img)

        {

            byte[] byteArray = new byte[0];

            using (MemoryStream stream = new MemoryStream())

            {

                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                stream.Close();

                byteArray = stream.ToArray();

            }

            return byteArray;

        }

    }

}



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PDFBarcode.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Button runat="server"  ID="btnDownload"  Text="Download" OnClick="btnDownload_Click" />

    </div>

    </form>

</body>

</html>


Popular posts from this blog

Dynamic Menu in asp.net using Listview

 1.    public DataTable LoadMenu()         {             MenuBal MenuBal = new MenuBal();             DataSet ds = this.GetData(0);             PopulateMenu(ds);             return ds.Tables[2];         }     private DataSet GetData(int parentMenuId)         {             MenuBal objmenu = new MenuBal();             string Mode = "";             if (Session["Mode"] != null)             {                 Mode = Session["Mode"].ToString();             }             DataSet ds = objmenu.GetData(parentMenuId, Convert.ToInt32(SessionHelper.CurrentUserId), Mode, SessionHelper...