2016-09-09 33 views
1


我需要在PDF中繪製代碼128字體的條形碼,所以我使用PdfSharp庫。條碼PDF圖中象下面這樣:PdfSharp在IIS上繪製條形碼128使符號空白

XFont BarcodeFont = new XFont("Code 128", 40, XFontStyle.Regular); 

tf.Alignment = XParagraphAlignment.Center; 
XRect barecodeLabel = new XRect(new XPoint(leftMargin, rectanglesPadding), new XSize(page.Width - rightMargin, BarcodeFont.Height + 10)); 
tf.DrawString(PrepareBarcode(item.ItemNo_), BarcodeFont, XBrushes.Black, barecodeLabel, XStringFormats.TopLeft); 
rectanglesPadding += barecodeLabel.Height - XUnit.FromPoint(5.0); 

準備數據條碼:

private string PrepareBarcode(string itemNo) 
    { 
     int sumControl = SumControl(itemNo); 
     string kod = ""; 
     kod += (char)209; 
     kod += itemNo; 

     kod += (char)sumControl; 

     kod += (char)211; 
     return kod; 
    } 

與和控制:

private int SumControl(string input) 
     { 
      int sum = 0; 

      for (int i = 0; i < input.Length; i++) 
      { 
       if (input[i] != ' ') 
       { 
        int znak = (int)input[i] - 32; 
        sum += (i + 1) * ((int)input[i] - 32); 
       } 
      } 
      sum += 104; 
      int rem = Convert.ToInt32(Decimal.Remainder(sum, 103)); 

      if (rem < 95) 
       return rem + 32; 
      else 
       return rem + 105; 
     } 

字體被包埋在PDF格式和PDF格式返回給aspx頁面作爲MemoryStream並刷新瀏覽器:

WCF:

MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document(); 
       MigraDoc.Rendering.DocumentRenderer renderer = new DocumentRenderer(doc); 
       MigraDoc.Rendering.PdfDocumentRenderer pdfRenderer = new MigraDoc.Rendering.PdfDocumentRenderer(true, PdfFontEmbedding.Always); 
       pdfRenderer.PdfDocument = document; 
       pdfRenderer.DocumentRenderer = renderer; 
       using (MemoryStream ms = new MemoryStream()) 
       { 
        pdfRenderer.Save(ms, false); 
        byte[] buffer = new byte[ms.Length]; 
        ms.Seek(0, SeekOrigin.Begin); 
        ms.Flush(); 
        ms.Read(buffer, 0, (int)ms.Length); 
        return ms; 
       } 

而且ASPX頁面:

try 
      { 
       zebexService = new SERVICES.NAV.WAREHOUSE.ZEBEX.ZebexClient(); 
       zebexService.Open(); 
       var wampirUserId = HttpContext.Current.GetWampirID(); 
       System.IO.MemoryStream stream = zebexService.PrintBarcodeLabels(wampirUserId, itemToPrintList); 
       zebexService.Close(); 

       HttpContext.Current.Response.Clear(); 
       HttpContext.Current.Response.ContentType = "application/pdf"; 
       HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; 
       HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Druk etykiet:" + DateTime.Now.ToString("yy/mm/dd-hh:mm") + "_" + wampirUserId + ".pdf"); 

       HttpContext.Current.Response.BinaryWrite(stream.ToArray()); 
       HttpContext.Current.Response.Flush(); 
       stream.Close(); 
       HttpContext.Current.Response.End(); 
       Response.BinaryWrite(stream.ToArray()); 
       Response.Flush(); 
      } 

當我畫到本地主機(窗口10,asp.net開發。服務器,語言。 PL)這是正確drawed:

[Correctly drawed barcode in pdf on localhost]]

,但是當我移動代碼(WCF)到IIS服務器與Windows Server 2012R2,郎。英語安裝了相同的字體(code128.ttf),我得到的條形碼,字符之間較大的差距:

[Not correctly drawed barcode]

我能做些什麼來解決這個問題?

+0

請檢查是否有任何控制檯錯誤? – Webruster

+0

僅限: 將資源解釋爲Document,但使用MIME類型application/pdf傳輸:「http:// localhost:59384/Warehouse/Buffer/PrintLabels.aspx」。 – Piotr

+0

將應用程序移至服務器時出現錯誤? – Webruster

回答

1

我只能通過使用PDFsharp來顯示我的代碼來生成條形碼(EAN 13)來幫助您,該代碼適用於開發和服務器。但它使用了不同的字體:ean13.ttf,所以也許這對你不起作用。

 using (PdfDocument document = new PdfDocument()) 
     { 
      //create pdf header 
      document.Info.Title = "My barcode"; 
      document.Info.Author = "Me"; 
      document.Info.Subject = "Barcode"; 
      document.Info.Keywords = "Barcode, Ean13"; 
      document.Info.CreationDate = DateTime.Now; 

      //create new pdf page 
      PdfPage page = document.AddPage(); 
      page.Width = XUnit.FromMillimeter(210); 
      page.Height = XUnit.FromMillimeter(297); 

      using (XGraphics gfx = XGraphics.FromPdfPage(page)) 
      { 
       //make sure the font is embedded 
       var options = new XPdfFontOptions(PdfFontEmbedding.Always); 

       //declare a font for drawing in the PDF 
       XFont fontEan = new XFont("Code EAN13", 75, XFontStyle.Regular, options); 
       XTextFormatter tf = new XTextFormatter(gfx); 

       //create the barcode from string 
       gfx.DrawString(barcodeText, fontEan, XBrushes.Black, new XRect(15, 40, page.Width, page.Height), XStringFormat.TopLeft);  
      } 
     } 
+0

謝謝,但我需要繪製像ABB302MOEL的索引號,所以ean13是不夠的。 – Piotr

+0

我知道,但也許指定頁面和高度將解決您的問題。 – VDWWD

+0

我已經這麼做了: 'PdfPage page = document.AddPage(); page.Width = XUnit.FromInch(2.76); page.Height = XUnit.FromInch(2.07); XGraphics gfx = XGraphics.FromPdfPage(page); gfx.MFEH = PdfFontEmbedding.Always; gfx.MUH = PdfFontEncoding.Unicode; XTextFormatter tf = new XTextFormatter(gfx);' – Piotr