ShowTextAligned未准确输出文本

时间:2014-10-07 19:34:03

标签: itextsharp

我正在使用下面的代码段。我希望文本恰好相距一英寸,但事实并非如此。我做错了什么?

Dim outputFile As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf")
    Using fs As New FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)
        Using doc As New Document(PageSize.LETTER)
            Using writer As PdfWriter = PdfWriter.GetInstance(doc, fs)
                doc.Open()

                'This creates two lines of text using the iTextSharp abstractions
                '   doc.Add(New Paragraph("This is Paragraph 1"))
                '  doc.Add(New Paragraph("This is Paragraph 2"))

                'This does the same as above but line spacing needs to be calculated manually
                Dim cb As PdfContentByte = writer.DirectContent
                cb.SaveState()
                cb.SetColorFill(BaseColor.BLACK)
                cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12.0F)
                cb.BeginText()
                ' rlm - dimensions are from the bottom
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb1", 20, 20, 0)
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb2", 20, 92, 0)
                cb.EndText()
                cb.RestoreState()
                doc.Close()
            End Using
        End Using
    End Using

1 个答案:

答案 0 :(得分:0)

我不确定你是如何测量东西的,但我只是以实际分辨率打印你的样品并将它与尺子进行比较,它们的基线确实相隔一英寸。

如果您在计算机屏幕上进行测量,则会遇到问题。基于Windows的机器(虽然这种情况正在发生变化)通常以96 DPI运行,因此如果您在计算机屏幕上以100%的速度查看测试并尝试测量它,您将获得大约4/3英寸(96 / 72)。所谓的" hi-DPI"设置和设备变得越来越普遍,因此无法保证屏幕实际测量的内容。

enter image description here