473,462 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

printing graphics at the exact place and size

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
0 1551

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

Similar topics

0
by: Mark Davison | last post by:
Hi, I hope someone can help, this has been driving me mad! I am trying to generate a graphic on the fly. This is then shown in the browser in a "print preview" type window. The user can click a...
2
by: Philippe | last post by:
I'm writing a vb.net application that prints jpeg pictires, and although I set the defaultpageSettings.printerResolution of my PrintDocument object to the maximum available printer resolution...
3
by: D Witherspoon | last post by:
No matter what I do the default paper size is always either A3 or 11 by 8.5 .. Here is the code. Dim dlg As DialogResult pd.DocumentName = "Weld Image" Dim pkPaperSize As New...
0
by: KevinM | last post by:
Hello all! I am trying to set up my app to print with a custom paper size, but it keeps failing with the following error: PaperSize cannot be changed unless the Kind property is set to Custom. On...
10
by: gigi24 | last post by:
I have set up a custom paper size on a Windows XP SP2 machine. I have followed all of the directions that I can find. It does appear in the print server properties, but when I go to the advanced...
4
by: Jarry | last post by:
What are the rules on how VB.Net 2005 packages graphics? If I use the same graphic in two different picture boxes, does both the images count towards the final size? What if I start with image...
0
by: musicandstamps | last post by:
Hi Hopefully just a simple problem. All i am trying to do is print a rectangle or other graphic at a specified point on the paper measured in millimeters. The start is to get the hard margins...
1
by: priyavarsh | last post by:
Hi How to print graphics output in DOS mode
2
by: abdulsami2 | last post by:
Here is the problem. I want to scan image of a bank cheque, show it to the user the user than selects (clicking at the appropriate spots on the cheque image) the places at which he wants to print the...
2
by: DaveRook | last post by:
Hi I am coding a windows app in C#. The user enters a part number, clicks the button and information about the part number is displayed on the page, including text and graphics (pictures of...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.