我有以下条形码图像:
我正在使用以下 iTextSharp VB.NET 脚本生成包含此条形码的 PDF 文档:
Dim pdfDocument As iTextSharp.text.Document = Nothing
Dim filename As String = HttpContext.Current.Server.MapPath("barcode.pdf")
pdfDocument = New iTextSharp.text.Document()
Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, New FileStream(filename, FileMode.Create))
pdfDocument.Open()
Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
pdfDocument.NewPage()
Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("barcode.jpg"))
pdfDocument.Add(img)
pdfDocument.Close()
Dim fInfo As New FileInfo(filename)
Dim numBytes As Long = fInfo.Length
Dim fStream As New FileStream(filename, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fStream)
Dim data As Byte() = br.ReadBytes(CInt(numBytes))
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("Content-Type", "binary/octet-stream")
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=barcode.pdf;size ={0}", data.Length.ToString()))
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.BinaryWrite(data)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
但是,当生成此 PDF 时,图像看起来比预期的大,并且失真:
我在将图像设置为特定尺寸的任何地方都看不到,为什么它会像这样扭曲?我该如何预防呢?
至关重要的是,此图像保持其预期大小,以便条形码扫描仪可以读取。