Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code to print text "A" at location 100,200 (Point), but is doesn't print at the specified location.
VB
Imports System.IO
Imports System.Drawing.Printing


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

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

        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.OriginAtMargins = True


        prnDocument.Print()
    End Sub

    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)
        egraphics.DrawString("A", fReportFont, New SolidBrush(Color.Black), 100, 200)

    End Sub

End Class


if you want to see the actual printing, use the following link

http://social.msdn.microsoft.com/Forums/vstudio/en-US/7489fa8c-e50e-43b0-9491-17656cba789a/printing-doesnt-print-at-the-specified-points?forum=vbgeneral#aac3d27b-048e-4da7-9a43-fb60d9fefe04[^]
Posted
Updated 4-Dec-13 17:50pm
v3
Comments
TnTinMn 5-Dec-13 1:09am    
From your image, it appears to me that it printed 100 pts from the left edge and 200 pts from the top. Please explain why you believe it is incorrect.
kgmmurugesh 5-Dec-13 2:00am    
No, See the ruler line, It prints exactly at 114,180.
Mike Meinz 5-Dec-13 9:18am    
In the image found at the link in your question, it does look like it is printing properly. The image and the ruler lines are very small so it is difficult to see them. If the horizontal and vertical cell lines are at every 100 points, then it does look like the left edge of the "A" character is at 100 and the top edge is at 200. Don't forget that characters have a points of empty space around them. With larger fonts, there is more empty space around a character than with small fonts.
Mike Meinz 5-Dec-13 8:59am    
You're creating a Custom Paper Size. That may or may not be a causal factor. According to Wikipedia.org, A4 paper is 8.3 inches width and 11.7 inches height. For the PaperSize Class Constructor, that would be ("A4",830,1170). You are using ("A4",842,595) in your instantiation of the custom PaperSize class.

The "x" coordinate discrepancy is due to the the default formatting applied by the DrawString Method. If you apply "System.Drawing.StringFormat.GenericTypographic" as the format this will disappear. As for how you arrived at the "y" coordinate value being less than the requested 200 pts is beyond my comprehension. The Adobe Acrobat screen shot you referenced show the top of the letter at approximately 210; however as Mike Meinz has told you, the top of the letter is not the top of character cell. See this[^], for an explanation of the character cell layout.

egraphics.DrawString("A", fReportFont, New SolidBrush(Color.Black), 100, 200, System.Drawing.StringFormat.GenericTypographic)


I suggest that you change the draw point to (72, 72) so that you can see the layout better than trying to interpolate a grid position in Adobe Acrobat.
 
Share this answer
 
Comments
kgmmurugesh 10-Dec-13 23:13pm    
I want to add right alignment using the following code, it gives error.

Dim lAlign As New System.Drawing.StringFormat

lAlign.Alignment = StringAlignment.Far
lAlign = System.Drawing.StringFormat.GenericTypographic
TnTinMn 10-Dec-13 23:32pm    
It would help if you state what the error message is that you receive.

Also, I think your code fragment would be better if you did it like this:
Dim lAlign As System.Drawing.StringFormat = System.Drawing.StringFormat.GenericTypographic
lAlign.Alignment = StringAlignment.Far
kgmmurugesh 11-Dec-13 0:46am    
Thanks for your help. My mistake is, I don't substitute alignment property each and every time.
TnTinMn 11-Dec-13 9:17am    
You are welcome.

I also suggest that you add a lot of comments to your code about what worked (and didnot work) to get the effect you wanted. This is one of many areas that I find the documentation almost worthless and I find myself referring back to my old code quite often (or maybe it is that I am just getting old and forgetful :)).
kgmmurugesh 13-Dec-13 6:33am    
Hi i have another doubt, view the following link.

http://www.codeproject.com/Questions/696289/Text-Width-differs-for-printdocument-itextsharp
Since you completely ignored the questions ask on your other post about this topic, I'll post it again here.

It MAY be because the PrintPreview does not take into account the non-printable area of a page that most printers have.
 
Share this answer
 

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