带文本的条形码使用ItextSharp

时间:2011-02-07 03:13:58

标签: c# itextsharp barcode

我在我的应用程序中使用iTextSharp来生成条形码。虽然一切都按照我的意愿运行,但我需要在条形码下显示条形码的值,如附图中的一个showin。 Sample Barcode

以下是我使用的C#代码:

Barcode39 barcodeImg = new Barcode39();
barcodeImg.Code = barcodeValue.ToString();
barcodeImg.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White).Save(stream, ImageFormat.Png);

5 个答案:

答案 0 :(得分:6)

这是我在搜索网络时找到的代码。希望这能解决您的问题:

string prodCode = context.Request.QueryString.Get("code");
context.Response.ContentType = "image/gif";
if (prodCode.Length > 0)
{
  Barcode128 code128          = new Barcode128();
  code128.CodeType            = Barcode.CODE128;
  code128.ChecksumText        = true;
  code128.GenerateChecksum    = true;
  code128.StartStopText       = true;
  code128.Code                = prodCode;
  System.Drawing.Bitmap bm    = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
  bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);            
} 

答案 1 :(得分:4)

使用iTextSharp dll无法直接在条形码图像下方显示文字。我尝试了同样的方法,并有一个解决方法来显示文本。这不是一个直接的解决方案,但会提供类似于条形码图像预期的输出。

为了生成条形码图像,我在他的博客中使用了JP Hellemons共享的输入。谢谢JP Hellemons! http://www.jphellemons.nl/post/Make-a-code128-barcode-with-C-sharp-and-iTextSharp.aspx

我使用的代码:

Barcode128 code128 = new Barcode128();
code128.CodeType = iTextSharp.text.pdf.Barcode.CODE128;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.StartStopText = false;
code128.Code = <<Barcode value>>;

// Create a blank image 
System.Drawing.Bitmap bmpimg = new Bitmap(120,35); // provide width and height based on the barcode image to be generated. harcoded for sample purpose

Graphics bmpgraphics = Graphics.FromImage(bmpimg);
bmpgraphics.Clear(Color.White); // Provide this, else the background will be black by default

// generate the code128 barcode
bmpgraphics.DrawImage(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new Point(0, 0));

 //generate the text below the barcode image. If you want the placement to be dynamic, calculate the point based on size of the image
bmpgraphics.DrawString(<<Barcode value>>, new System.Drawing.Font("Arial", 8, FontStyle.Regular), SystemBrushes.WindowText, new Point(15, 24));

// Save the output stream as gif. You can also save it to external file
bmpimg.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

如果我错了,请纠正我。

如果您有任何直接或简单的解决方案,请分享..

答案 2 :(得分:2)

iTextSharp.text.pdf.Barcode128 bc = new Barcode128();
bc.TextAlignment = Element.ALIGN_CENTER;
bc.Code = "anything";
bc.StartStopText = false;
bc.CodeType = iTextSharp.text.pdf.Barcode128.CODE128;
bc.Extended = true;
//bc.Font = null;

iTextSharp.text.Image PatImage1 = bc.CreateImageWithBarcode(cb, iTextSharp.text.BaseColor.BLACK, iTextSharp.text.BaseColor.BLACK);
PatImage1.ScaleToFit(160, 20);

PdfPTable p_detail1 = new PdfPTable(1);
p_detail1.WidthPercentage = 100;

PdfPCell barcideimage = new PdfPCell(PatImage1);
//barcideimage.Colspan = 2;
barcideimage.HorizontalAlignment = 2;
barcideimage.Border = 0;
p_detail1.AddCell(barcideimage);

l1.Add(p_detail1);

答案 3 :(得分:0)

阿。你需要设置字体。我建议您使用FontFactory.registerDirectories()FontFactory.getFont( name )

registerDirectories()将枚举操作系统已知用于存储字体的所有目录。 Windows(无论什么味道)都没问题。完成后,您可以轻松地从工厂获取字体。我所知道的所有条形码符号都应该没有默认编码的问题,所以你真的只需要一个基本的字体名称,而不是知道路径。 FontFactory会为您跟踪所有路径信息。方便。

答案 4 :(得分:-1)

code128.Size=10;

code128.Baseline = code128.Size; enter image description here

EAN13 enter image description here

相关问题