Question

I have the following barcode image:

enter image description here

I'm using the following iTextSharp VB.NET script to generate a PDF document containing this barcode:

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

When this PDF is generated, however, the image is appearing larger than expected, and is distorted:

enter image description here

I can't see anywhere where I'm setting the image to be a specific size, so why would it distort like this? And how can I prevent it?

It's crucial that this image remains the size it is intended to be so that it can be read by barcode scanners.

Was it helpful?

Solution

There's a couple of things going on here and none of them actually have to do with iText/iTextSharp actually.

First, when dealing with images inside of a PDF always think in terms of inches or centimeters and never just pixels. A 300 pixel image at 300DPI is 1 inch wide and a 72 pixel image at 72DPI is also 1 inch wide. Both images will occupy 1 inch within the PDF, the former will just have more pixels to cram into that space and potentially look better. This is very important.

Second, using the above information, when you print a PDF with a 1" image and do not scale it (the default in Acrobat is to Fit which scales) you should be able to grab a ruler and measure exactly 1".

Third, I seriously don't know what Acrobat, Photoshop or any other program consider "100%". In Acrobat, if you go to Edit, Preferences, Page Display you can adjust the Resolution and that change's the definition of 100%. If I change this to 90% and hold my printout with a 1" object on it it seems to match the closest. YMMV.

This third thing I think is your problem, mainly what "100%" means. You and I have an idea of what it should mean but Adobe has a different idea apparently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top