Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to measure the width of a string in printdocument & itextSharp, it shows different width,
Printdocument shows 363.5 points & Itextsharp shows 333.42 points, what is wrong with my code.

VB
Imports System.IO
Imports System.Drawing.Printing
Imports iTextSharp.text.pdf
Imports iTextSharp.text



Public Class Form1
    Public egraphics As Graphics
    Public prnDocument As New System.Drawing.Printing.PrintDocument

    Private TMPbmp As New Bitmap(1, 1)
    Private TMPgfx As Graphics = Graphics.FromImage(TMPbmp)
    Private TxtSize As SizeF
    Private lstrstring As String



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim psize As New Printing.PaperSize("Custom", 595, 842)

        lstrstring = "Abcdghijyzw"

        AddHandler prnDocument.PrintPage, AddressOf PrintPage

        prnDocument.DefaultPageSettings.PaperSize = psize
        prnDocument.DefaultPageSettings.Margins.Top = 0
        prnDocument.DefaultPageSettings.Margins.Left = 0
        prnDocument.DefaultPageSettings.Margins.Bottom = 0
        prnDocument.DefaultPageSettings.Margins.Right = 0
        prnDocument.Print()
        ITextSharpPrint()
    End Sub

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

        lstringformat = System.Drawing.StringFormat.GenericTypographic

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

    End Sub


    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 tFont As iTextSharp.text.Font

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

            cb.BeginText()

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

            Dim lbase As BaseFont
            lbase = tFont.BaseFont

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

            MsgBox(lbase.GetWidthPoint(lstrstring, 60))


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

        End Using
    End Sub
End Class
Posted
Updated 13-Dec-13 8:51am
v3
Comments
Sergey Alexandrovich Kryukov 13-Dec-13 15:11pm    
I don't see a problem. First, MeasureString is notoriously inaccurate. Also, I think the width can slightly differ for different media.
—SA

1 solution

Please see my comment to the question. This CodeProject article demonstrates the improved version of the method of measuring the string: Bypass Graphics.MeasureString limitations[^].

—SA
 
Share this answer
 
Comments
kgmmurugesh 14-Dec-13 1:21am    
When i convert your code to vb.net, it shows the following error.

An error occured converting your code, probably due to a syntax error:
-- line 1 col 15: invalid TypeDecl
Sergey Alexandrovich Kryukov 14-Dec-13 2:14am    
I don't know what kind of translator did you use. You can use open-source ILSpy. Build C# project, load assembly in ILSpy, save as VB.NET. The result is very accurate.
http://en.wikipedia.org/wiki/.NET_Reflector,
http://www.ilspy.net.
—SA
kgmmurugesh 16-Dec-13 0:17am    
I use the following link to convert from c# to vb.net.

http://www.developerfusion.com/tools/convert/csharp-to-vb/?batchId=7795037d-af46-4486-9629-b1f1469740f7
Sergey Alexandrovich Kryukov 16-Dec-13 0:54am    
This one is not bad, but I would not trust if some online translator gives you a problem. I told you the ultimate way to translate: open-source ILSpy.
—SA
kgmmurugesh 16-Dec-13 2:07am    
your code also shows different width. your code shows 355 points, I want same width in printdocument & Itextsharp. Is it possible?

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