我需要在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:
,但是當我移動代碼(WCF)到IIS服務器與Windows Server 2012R2,郎。英語安裝了相同的字體(code128.ttf),我得到的條形碼,字符之間較大的差距:
我能做些什麼來解決這個問題?
請檢查是否有任何控制檯錯誤? – Webruster
僅限: 將資源解釋爲Document,但使用MIME類型application/pdf傳輸:「http:// localhost:59384/Warehouse/Buffer/PrintLabels.aspx」。 – Piotr
將應用程序移至服務器時出現錯誤? – Webruster