i rewrote the Example of iText (Java)
http://itextpdf.com/examples/iia.php?id=297
in iTextSharp (C#) like this..
var document = new Document(PageSize.A4);
var writer = PdfWriter.GetInstance(document,
new FileStream(Path, FileMode.Create, FileAccess.Write,
FileShare.None));
document.Open();
var cb = writer.DirectContent;
document.Add(new Paragraph("Barcode EAN.UCC-13"));
var codeEan = new BarcodeEAN {Code = "230482304"};
document.Add(new Paragraph("default:"));
document.Add(codeEan.CreateImageWithBarcode(cb, BaseColor.BLACK, BaseColor.WHITE));
codeEan.GuardBars = false;
document.Add(new Paragraph("without guard bars:"));
Image i = codeEan.CreateImageWithBarcode(cb, null, null);
document.Add(i);
codeEan.Baseline = -1f;
codeEan.GuardBars = true;
document.Add(new Paragraph("text above:"));
document.Add(codeEan.CreateImageWithBarcode(cb, null, null));
codeEan.Baseline = codeEan.Size;
document.Close();
but getting following Exception
Index was outside the bounds of the array.
at iTextSharp.text.pdf.BarcodeEAN.GetBarsEAN13(String _code)
at iTextSharp.text.pdf.BarcodeEAN.PlaceBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor)
at iTextSharp.text.pdf.Barcode.CreateTemplateWithBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor)
at iTextSharp.text.pdf.Barcode.CreateImageWithBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor)
Where is my mistake? Its 1:1 the Example on there Page… i dont found something for C#, but a C# Port and no Doku is a little bit… nothing.
The problem here is that you’re providing an invalid EAN barcode value to your
BarcodeEAN‘sCodeproperty. EAN barcode values must have a specific format including a minimum character requirement and with the last digit being a checksum. You can find more about this format here.There are plenty of resources available for validating EAN barcode values. Over at codeproject.com there’s an article with C# code that will validate an EAN13 barcode and will also calculate the checksum digit for a given 12 digit EAN13 barcode value.