Click here to Skip to main content
15,902,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi - when measure the width of a string in printdocument and itextsharp are differ

the code is below

VB
Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
       Dim fReportFont As System.Drawing.Font

       egraphics = e.Graphics
       egraphics.PageUnit = GraphicsUnit.Point
       fReportFont = New System.Drawing.Font(Trim("Helvetica"), 60, FontStyle.Regular)
       MsgBox(egraphics.MeasureString("Abcdghijyzw", fReportFont).Width)
       egraphics.DrawString("Abcdghijyzw", fReportFont, New SolidBrush(Color.Black), 100, 100, System.Drawing.StringFormat.GenericTypographic)

   End Sub


VB
Private Sub ITextSharpPrint()

        Using fs As System.IO.FileStream = New FileStream("C:\Kgm.pdf", FileMode.Create)

            Dim document As New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
            Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
            Dim lstrString As String
            Dim tFont As iTextSharp.text.Font
            Dim ltemp As Double
            Dim l1 As Double
            Dim l2 As Double

            document.Open()
            Dim cb As PdfContentByte = writer.DirectContent

            cb.BeginText()

            lstrString = "Abcdghijyzw"

            tFont = FontFactory.GetFont("Helvetica", 60, 0)

            Dim lbase As BaseFont
            lbase = tFont.BaseFont
            l1 = lbase.GetAscentPoint(lstrString, tFont.Size)
            l2 = Math.Abs(lbase.GetDescentPoint(lstrString, tFont.Size))
            ltemp = l1 + l2


            MsgBox(lbase.GetWidthPoint(lstrString, 60))


            cb.SetFontAndSize(tFont.BaseFont, 60)
            cb.SetTextMatrix(100, document.PageSize.Height - (100 + ltemp))
            cb.ShowText(lstrString)

            cb.EndText()
            document.Close()
            writer.Close()
            fs.Close()

        End Using
    End Sub


in the above code the 2 msgboxes shown different values - ie. 363.5 and 333.42 what went wrong?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900