C# 使用ItextSharp时显示文本的条形码

C# 使用ItextSharp时显示文本的条形码,c#,itextsharp,barcode,C#,Itextsharp,Barcode,我正在应用程序中使用iTextSharp生成条形码。虽然一切都按我所希望的那样工作,但我需要在条形码下显示条形码的值,就像附件图像中显示的一样。 下面是我使用的C代码: Barcode39 barcodeImg = new Barcode39(); barcodeImg.Code = barcodeValue.ToString(); barcodeImg.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.Wh

我正在应用程序中使用iTextSharp生成条形码。虽然一切都按我所希望的那样工作,但我需要在条形码下显示条形码的值,就像附件图像中显示的一样。

下面是我使用的C代码:

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

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

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

啊。你需要设置字体。我建议使用
FontFactory.registerDirectories()
FontFactory.getFont(名称)


registerDirectories()
将枚举操作系统已知用于存储字体的所有目录。Windows(无论什么风格)都没有问题。完成后,您可以很容易地从工厂获得字体。我所知道的所有条形码符号都应该与默认编码没有问题,因此您只需要询问基本的字体名称,而不需要知道路径。FontFactory为您跟踪所有路径信息。方便。

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

为了生成条形码图像,我使用了JP Hellemons在其博客中共享的输入。谢谢JP Hellemons!

我使用的代码是:

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);
Barcode128=newbarcode128();
code128.CodeType=iTextSharp.text.pdf.Barcode.code128;
code128.ChecksumText=true;
code128.GenerateChecksum=true;
code128.StartStopText=false;
代码128.Code=;
//创建空白图像
System.Drawing.Bitmap bmpimg=新位图(120,35);//根据要生成的条形码图像提供宽度和高度。为样本目的进行编码
Graphics bmpgraphics=Graphics.FromImage(bmpimg);
bmp图像。清晰(颜色。白色);//提供此选项,否则背景默认为黑色
//生成代码128条形码
bmpgraphics.DrawImage(代码128.CreateDrawingImage(System.Drawing.Color.Black,System.Drawing.Color.White),新点(0,0));
//生成条形码图像下方的文本。如果希望放置是动态的,请根据图像的大小计算该点
bmpgraphics.DrawString(,new System.Drawing.Font(“Arial”,8,FontStyle.Regular),SystemBrush.WindowText,new Point(15,24));
//将输出流另存为gif。您还可以将其保存到外部文件
bmpimg.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
如果我错了,请纠正我

如果你们有直接或更简单的解决方案,请分享。

code128.Size=10

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);
code128.Baseline=code128.Size;


EAN13

有帮助吗?很抱歉,条形码下仍没有显示文本。谢谢你的帮助。哈哈!那个片段看起来就像我博客上的那个!标记它不是字体。我正在寻找显示的条码下的价值一样,在附加的图像显示。谢谢你说的对。要让条形码显示其文本,需要设置字体。就在for
Barcode.setFont()
中这样说。奇怪的语法,但想法是存在的。奇怪的是,Barcode128的默认构造函数将字体初始化为Helvetica/WinAnsienceODing。你正在使用什么版本的iText,你想显示什么文本?我认为这是不可行的。。。我已经尝试了我能想到的每一个选择,但没有得到想要的结果。。。如果有人真的能做到这一点,一个真正的答案将是伟大的。。。(@markstorer-不是iText,而是iTextSharp-一个到.NET的端口)它们实际上是可以互换的。“get”经常但不总是成为属性,所有函数名都以大写字母开头。我重复一遍,您使用的是什么版本的iTextSHARP,以及您试图显示的文本?请不要只提供代码答案,还要解释您的解决方案如何以及为什么能够解决问题。