2012-06-29 27 views
1

我改寫的iText的(Java)的實施例 http://itextpdf.com/examples/iia.php?id=297 在iTextSharp的(C#)這樣的..iTextSharp的IndexOutOfRange異常時添加一個條形碼等文檔

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

但要下列例外

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) 

我的錯誤在哪裏?它的1:1在那裏的例子頁...我沒有找到C#的東西,但一個C#端口,沒有Doku是一點點......沒有。

回答

1

這裏的問題是您要爲您的BarcodeEANCode屬性提供無效的EAN條形碼值。 EAN條形碼值必須具有特定的格式,包括最小字符要求和最後一個數字作爲校驗和。你可以找到更多關於這種格式here

有大量資源可用於驗證EAN條形碼值。在codeproject.com上有an article與C#代碼將驗證EAN13條形碼,並且還將計算給定的12位EAN13條形碼值的校驗和數字。

+0

謝謝,有時我覺得很複雜:)現在工作很好 – slopsucker