2011-02-07 146 views
7

我在我的應用程序中使用iTextSharp生成條形碼。雖然一切都按照我想要的方式工作,但我需要在條形碼下顯示條形碼的值,如附圖所示。 Sample Barcode使用ItextSharp的文本條形碼

下面是C#代碼我使用:

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

可以幫助嗎?[this](http://stackoverflow.com/questions/3215522/itextsharp-adding-text-plus-barcode-in-a-single-cell)有幫助嗎? – 2011-02-07 03:28:15

回答

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

對不起,文字仍未出現在條形碼下方。感謝您的幫助。 – acadia 2011-02-07 13:25:17

+4

哈哈!該片段看起來就像我的博客上的片段! http://www.jphellemons.nl/post/Make-a-code128-barcode-with-C-sharp-and-iTextSharp.aspx – 2011-10-19 13:19:51

0

啊。你需要設置字體。我推薦使用FontFactory.registerDirectories()FontFactory.getFont(name)

registerDirectories()將枚舉所有目錄已知OS用於存儲字體。 Windows(無論什麼味道)都沒有問題。一旦完成,您可以輕鬆從工廠獲取字體。所有我知道的條形碼符號應該沒有默認編碼的問題,所以您只需要詢問基本字體名稱而不知道路徑。 FontFactory會跟蹤您的所有路徑信息。便利。

+0

標記其不是字體。我正在尋找顯示條形碼下的值,如附圖中的一個顯示。謝謝 – acadia 2011-03-03 21:51:57

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

糾正我,如果我錯了..

如果您有任何的直接或簡單的解決方案,請分享..

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