2011-11-17 31 views
2

大,我有以下條碼圖像:圖片看起來比實際大小的PDF

enter image description here

我使用以下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,然而,圖像中出現大於預期,和失真:

enter image description here

我看不到任何地方,我將圖像設置爲特定尺寸,爲什麼它會像這樣變形?我該如何預防它?

這個圖像的大小保持原來的大小非常重要,以便條形碼掃描儀可以讀取它。

回答

0

這裏有一些事情正在進行,實際上他們中沒有一個實際上與iText/iTextSharp有關。

首先,處理PDF內部的圖像時總是考慮以英寸或釐米爲單位,而不是像素。 300DPI的300像素圖像爲1英寸寬,72DPI的72像素圖像也爲1英寸寬。這兩種圖像在PDF中佔據1英寸,前者只會有更多的像素塞進該空間,並可能看起來更好。這個非常重要。其次,使用上述信息,當您打印1「圖像和的PDF不會縮放(Acrobat中的默認設置爲適合哪些比例),您應該可以抓住標尺並精確測量1" 。

第三,我真的不知道Acrobat,Photoshop或其他程序認爲「100%」是什麼。在Acrobat中,如果您轉到「編輯」,「首選項」,「頁面顯示」,您可以調整分辨率,並且更改的定義爲100%。如果我將其改爲90%,並用1「對象將其打印出來,它似乎與最接近的YMMV相匹配。我有一個想法,它應該應該的意思,但Adobe有明顯不同的想法。