vb.net acurate printer output

I'm having trouble printing accurately by inches, to a form.

I'm trying to print text starting at 1/16" left, 1 1/8 inches down
I even printed a rectangle around it to see where the layout rectangle is.
I'm getting 1/4" over and 1 1/4" down.

This makes the form not line up. I can never know what printer this app will run on, but it needs to print correctly on the form each time.  Any thoughts?
Private Function InchesToPrinterUnits(ByVal InchValue As Single) As Single
        'printer units are 1/100 of an inch

        Return InchValue * 100
 End Function

  Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles MyDoc.PrintPage
        'get printer dpi

        MyDoc.OriginAtMargins = False

        '1/16in,1 1/8in, 2 7/8in 1/16in
        Dim rctfResidence As New RectangleF(InchesToPrinterUnits(0.0625), 
InchesToPrinterUnits(1.125), InchesToPrinterUnits(2.875), InchesToPrinterUnits(0.3125))
        Dim sf As New System.Drawing.StringFormat()
        sf.Alignment = StringAlignment.Near

        With e.Graphics
            .DrawString(txtResidence.Text, fntData, Brushes.Black, rctfResidence, sf)
            .DrawRectangle(Pens.Red, rctfResidence.X, rctfResidence.Y, rctfResidence.Width, rctfResidence.Height)
            '.DrawString("", fntData, Brushes.Black, 0, 0)
        End With

        e.HasMorePages = False
    End Sub

Open in new window

LVL 25
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Since you are dealing with different printers, you cannot assume that printer units are 1/100 of an inch. Most modern printers are 1/600 or 1/1200.

The resolution can change from one printer to another, and a printer can have many resolutions. Some have resolutions that are different horizontally and vertically.

Here is the code I use to get the default resolution of my own printer:
Dim settings As New System.Drawing.Printing.PrinterSettings
settings.PrinterName = "HP LaserJet 3015"
Debug.WriteLine(settings.DefaultPageSettings.PrinterResolution.X.ToString, settings.DefaultPageSettings.PrinterResolution.Y.ToString)

Open in new window

The result is 600: 600

If I run it with an older InkJet printer, I get 300:300.
SStoryAuthor Commented:
Well, I thought that returned DPI, and the docs I read said printing in dotnet was done on a unit that was in 1/100 inch no matter what. So which is true?  I am wondering if my only real problem is the hard margin that the printer may have. I have found an article related to this :

I haven't tested it yet, and don't know if this is still an issue since the article was for an old version of Dotnet.  Do you have any experience with this to know?  The article suggests getting the hard margins and offsetting. I don't know why that would be necessary, but my print is only off 1/4" in both directions which could suggest the hard margin issue or minimum margin.  I just assumed that when I set
MyDoc.OriginAtMargins = False

That it would calculate from absolute 0,0 and not 0,0 at the hard margin. Maybe that is not correct.
Jacques Bourgeois (James Burger)PresidentCommented:
I have not worked with that stuff in real life for years, I mostly go through Word, Excel and Access when I want to print from my applications nowadays. They take care of all that stuff.

But by reading the documentation in details, I think I might have found the solution. I am not at my development computer right now, so I cannot test.

If you set OriginAtMargins to False, the coordinate system is relative to the "printable page". It thus takes into account the hard margins, that is the portion of the sheet on which the printer won't print to prevent ink or toner overflow in the printer mechanism.

If you set it to True, then the coordinate system is at the page margins. The page margins are not set at 0,0 by default. I just tested on my current printer, and they are set at 100,100. This means 1/6", which is very close to the gap you get between 1/16" and 1/4".

Try setting the margins to what you need, something like the following before setting the OriginAtMargins to False. Adjust the value as needed. With 600 dots per inch, 1/4" would theoretically require a margin of 150:

MyDoc.DefaultPageSettings.Margins.Left = 150
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

SStoryAuthor Commented:
Thanks! I will try that.
Yes, well, this is a "simple" app, and I didn't want to involve Word, etc. as the person buying it would have to then buy those.
Jacques Bourgeois (James Burger)PresidentCommented:
I like the double quotes around "simple". Those simple applications always end up being more complex than they appear at the start.
SStoryAuthor Commented:
So if I say print 1/16" over would that be

left margin + 1/16" or 1/16" from the edge? The form I am trying to print two really has no margin.
SStoryAuthor Commented:
I'm starting to think this isn't going to be possible on a laser printer as the form needs to have no margins.
Maybe on a dot matrix or something where you can offset the form until it is properly aligned or something.
Jacques Bourgeois (James Burger)PresidentCommented:
There is always a margin on modern printers, laser and ink. Otherwise, if you were printing right on the border of the sheet, some ink or toner could flow a little outside of the border and cause a buildup that would eventually cause problems.

This is what they call the hard margins. It changes with each printer / driver combination. I have 3 printers, and they go from 12/100 to 25/100. On a given printer, the margin will also change depending on the driver used. If I try to print at 1/16 on any of them, that is 6.25/100, I am too close and the printer driver will simply crop the extra portion.

I did not understand that when you said you wanted to print at 1/16" left, that was relative to the paper, otherwise I would have told you from the start that it is impossible. Usually, when you specify a distance, it is the distance from the margin.

Even with a dot matrix, unless you have one that can take paper larger than the one you want to print to, you might not be able to do what you want. It's been years since I used one, but I remember having a customer who wanted a border around the whole surface of the sheet and we were never able to do it on a regular (8 1/2" width) printer. The printer head simply did not travelled the whole width. So if we adjusted the paper so that the left line was just on the border of the paper, there was a gap of 1/4" or so at the right side. He had to buy a printer that accepted wider sheets in order to be able to cover the whole 8 1/2 width.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SStoryAuthor Commented:
Well, actually some photo printers, print right to the edge.  I am looking for a printer that will do 1/16" or less and after talking with HP it appears there may be some portable printers capable of this.

I agree it is a pain. I will let you know how it turns out.
Jacques Bourgeois (James Burger)PresidentCommented:
You are right, I forgot about that because I almost never print photos for my personal use and never had to do it on photo paper from my applications.

I happen to be able to do that on two HP inkjet color printers, and they are standard inkjet printers, not of the portable type.

Just checked on my DeskJet 6940. If I set it for Bordeless Printing, hard margins are set to 0.0, 0.0 and I can print up to the edges.

But for your information, there are limitations that might not be acceptable for your use. Some of those might be specific to my model of printer however.

First of all, this setting is defined in a special property of the printer drivers that needs to be set manually in the printer's preferences. I see no way of setting it through code, since this is not a feature that is exposed through the PrinterSettings class.

Printing is slower than normal in this mode, even when set for draft printing.

The paper track in those printers is not precise. There might be a slight difference in alignment between 2 sheets. This is usually not visible if you print with a margin or a thick border, but if the border is thin, it is very evident. In some sheets, part of the printout ends up outside of sheet so a thin line could not show on some pages.

When set to to Borderless printing, the only choice of sizes is a series of "Borderless ..." papers, such as "Borderless 8 1/2 x 11". There is photo paper sold in those sizes, but this paper is thicker than standard paper though. I wonder if there could be a problem such as ink buildup on the rollers in the long run with standard thinner paper.

If your need is to have a border around the page, you might consider having the border printed by a printing shop and use that paper when needed. A regular printer could then be used without problem.

Hoping these help you a bit.
SStoryAuthor Commented:
Well, the form to be printed on is more like card stock paper anyway--thicker than normal paper at least.
SStoryAuthor Commented:
I've talked to many manufacturers of printers and not found a one that sales a printer that will do this.  It is amazing to me.  For that reason I've not been able to complete the program.
SStoryAuthor Commented:
I am awarding points because the question asked for "any thoughts," and the answer is apparently that it is not doable.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.