Generate barcode (PDF417) programmatically using iTextSharp and Microsoft .NET

  • Post author:
  • Post category:Blog
  • Post comments:0 Comments
  • Reading time:1 mins read

To generate barcode PDF417 from text, we use following code

private void btnGenerate_Click(object sender, EventArgs e)
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("out.pdf", FileMode.Create));
        document.Open();
        BarcodePDF417 pdf417 = new BarcodePDF417();
        pdf417.Options = BarcodePDF417.PDF417_USE_MACRO;           
 
        pdf417.MacroFileId = "0";
        pdf417.MacroSegmentCount = 20;
        pdf417.MacroSegmentId = 0;
        pdf417.SetText(txtText.Text);
 
        iTextSharp.text.Image img = pdf417.GetImage();         
        document.Add(img);
        document.Close();       
        MessageBox.Show("out.pdf generated!");
         
        //get  System.Drawing.Image from BarcodePDF417 object
        System.Drawing.Image imgOut = pdf417.CreateDrawingImage(Color.Black, Color.White);
        //Save to file
        imgOut.Save("out.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
        //Display on Picture box
        pictureOutput.Image = imgOut;
    }

Hope this help!

Leave a Reply