Having scoured the internet to find out how to migrate from VB6 to VB.Net a Printer.TextWidth() call to calculate the length of a piece of text for the current printer, we couldn't find any good examples.
The examples around are either form-based, or involve being in the middle of actually printing a document. Sometimes you want to work out text lengths without printing. In our instance we are adding watermarks of Postscript files, from which we then generate PDFs.
As ever, it turns out that this is simpler to do than it at first seems.
A VB6 function of the form:
Function vbGetTextWidth(s as string) As Single
Printer.Font.Name = "arial"
Printer.Font.Size = 12
Printer.ScaleMode = vbInches
vbGetTextWidth = Printer.TextWidth(s)
End Function
becomes something like
Function netGetTextWidth(ByVal s As String) As Integer
Dim g As Graphics
Dim p As New Printing.PrintDocument
Dim f As New Font("arial", 12, FontStyle.Regular, GraphicsUnit.Point, 0)
On Error Resume Next
g = p.PrinterSettings.CreateMeasurementGraphics
g.PageUnit = GraphicsUnit.Inch
netGetTextWidth = g.MeasureString(s, f).Width
g.Dispose()
f.Dispose()
End Function
Hope this helps someone in a similar fix. By the look of the forums are quite a few.