PDF 中的图像显示大于实际尺寸

发布于 2024-12-16 20:40:13 字数 1549 浏览 0 评论 0原文

我有以下条形码图像:

在此处输入图像描述

我正在使用以下 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 时,图像看起来比预期大,并且扭曲:

我看不到我将图像设置为特定尺寸的任何地方,那么为什么它会像这样扭曲呢?我该如何预防呢?

至关重要的是,该图像保持其预期大小,以便条形码扫描仪可以读取。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末蓝 2024-12-23 20:40:13

这里发生了一些事情,但实际上它们都与 iText/iTextSharp 无关。

首先,在处理 PDF 中的图像时,始终以英寸或厘米为单位,而不仅仅是像素。 300DPI 的 300 像素图像宽度为 1 英寸,72DPI 的 72 像素图像也是 1 英寸宽。两个图像都将在 PDF 中占据 1 英寸,前者只会有更多像素填充到该空间中,并且可能看起来更好。这非常重要。

其次,使用上述信息,当您打印包含 1 英寸图像的 PDF 并且不缩放(Acrobat 中的默认设置是适合其比例)时,您应该能够拿起尺子进行测量正好 1"。

第三,我真的不知道Acrobat、Photoshop或任何其他程序认为“100%”是什么。在 Acrobat 中,如果您转到“编辑”、“首选项”、“页面显示”,您可以调整分辨率,该更改就是 100% 的定义。如果我将其更改为 90% 并在打印输出上放置一个 1" 物体,它似乎与最接近的匹配。YMMV。

我认为第三件事是你的问题,主要是“100%”的含义。你和我有一个想法Adobe 显然有不同的想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文