By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,238 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,238 IT Pros & Developers. It's quick & easy.

printing graphics at the exact place and size

P: 1
I would like to print a 6.5 X 9 rectangle on 8.5 X 11 paper using vb.net. I need it to be exactly 1 inch from the top and 1 inch from the left (and by default 1 inch from the right and the bottom. When I print to my HP printer, The left edge is only .9 in while the right is .97 inches from the right and the rectangles dimensions are greater than those desired. The results are different for different printers but never accurate. At Times the errors have changed and I am careful to load the paper properly.

Here is an example of the code I use. Can someone please give me some guidance as to what I might be doing wrong?

CODE SAMPLE:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Drawing.Printing
  2. Public Class Form1
  3.     Private PrinterName As String
  4.     Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
  5.         With PrintDialog1
  6.             With PrintDialog1
  7.                 If .ShowDialog = Windows.Forms.DialogResult.OK Then
  8.  
  9.                     PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
  10.                     PrinterName = PrintDialog1.PrinterSettings.PrinterName
  11.                     PrintDocument1.Print()
  12.                 End If
  13.             End With
  14.         End With
  15.     End Sub
  16.  
  17.     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  18.         Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
  19.         Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
  20.         Dim Left As Single
  21.         Dim Top As Single
  22.         Dim PrinterNameString As String = PrinterName
  23.         Dim StringSize As SizeF
  24.         e.Graphics.PageScale = 1.0F
  25.         PrinterNameString += vbCrLf & Now.Date.ToString
  26.         With PrintDocument1.DefaultPageSettings
  27.             .Landscape = False
  28.             PrintAreaHeight = .PaperSize.Height
  29.             PrintAreaWidth = .PaperSize.Width
  30.             marginLeft = 0 ' .Margins.Left
  31.             marginTop = 0 '.Margins.Top
  32.         End With
  33.         Left = 200 '2 inch
  34.         Top = 200 '2 inch
  35.         Dim ulCorner As New PointF((Left), Top) ' 2 inches from left
  36.         Dim urCorner As New PointF(Left + 450, Top) ' 4.5 inches wide
  37.         Dim llCorner As New PointF((Left), ulCorner.Y + PrintAreaHeight - 2 * Top) ' 9 inches tall
  38.  
  39.         Dim destPara As PointF() = {ulCorner, urCorner, llCorner}
  40.         Dim s As String
  41.         Dim f As New Font("Arial", 32, FontStyle.Regular, GraphicsUnit.Document)
  42.  
  43.         e.Graphics.DrawLine(Pens.Blue, urCorner.X, urCorner.Y, ulCorner.X, ulCorner.Y)
  44.         e.Graphics.DrawLine(Pens.Blue, ulCorner.X, ulCorner.Y, llCorner.X, llCorner.Y)
  45.         e.Graphics.DrawLine(Pens.Blue, llCorner.X, llCorner.Y, urCorner.X, llCorner.Y)
  46.         e.Graphics.DrawLine(Pens.Blue, urCorner.X, llCorner.Y, urCorner.X, urCorner.Y)
  47.         s = "Left: " & (Left / 100).ToString & " inch"
  48.         e.Graphics.DrawString(s, f, Brushes.Black, CSng(Left + 100), CSng(8 + PrintAreaHeight / 2))
  49.         s = "Top: " & (Top / 100).ToString & " inch"
  50.         e.Graphics.DrawString(s, f, Brushes.Black, CSng(PrintAreaWidth / 2), (Top + 100))
  51.         s = "Width: " & 11 - 2 * Left & " inches"
  52.         e.Graphics.DrawString(s, f, Brushes.Black, CSng(PrintAreaWidth / 2), (Top + 300))
  53.  
  54.         s = PrinterNameString
  55.         StringSize = e.Graphics.MeasureString(s, f, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt)
  56.         e.Graphics.DrawString(s, f, Brushes.Black, (PrintAreaWidth - StringSize.Width) / 2, (PrintAreaHeight - StringSize.Height) / 2)
  57.     End Sub
  58.  
  59.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  60.         PrintDialog1.PrinterSettings.DefaultPageSettings.Landscape = False
  61.         PrintDialog1.PrinterSettings.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
  62.         PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize = New PaperSize("A4", 850, 1100)
  63.     End Sub
  64. End Class
Nov 16 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.