Real Software Forums

Printer Graphics creator & destructor prints a half-page
Page 1 of 1

Author:  judgedoug [ Wed Aug 29, 2012 1:27 pm ]
Post subject:  Printer Graphics creator & destructor prints a half-page

Hi all,

so I've got a printer setup for Windows 7:

dim g as Graphics

dim choosePrinterPS as PrinterSetup

choosePrinterPS = new PrinterSetup
choosePrinterPS.MaxHorizontalResolution = 300
choosePrinterPS.MaxVerticalResolution = 300

g = OpenPrinterDialog(choosePrinterPS)

if g<>nil then

labelPrinter = new PrinterSetup // global variable containing label printer data
labelPrinter.SetupString = choosePrinterPS.SetupString

if labelPrinter.PageSetupDialog then
SaveLabelPrinterConfiguration // saves base64 setupstring to file
end if

end if

After a printer is chosen, it prints a page. Why?

But another strange thing also happens.

Whenever the destructor for the graphics object is called - either by me setting it to nil or the the method where the printing is occurring - it prints a half page.

For example, this simple method that prints numbers:

g  = OpenPrinter(LabelPrinter) //printer graphics object

Right=LabelPrinter.Pagewidth-(ABS(LabelPrinter.pageLeft))//pixel right for the right edge of printable area
Bottom=LabelPrinter.PageHeight-(ABS(LabelPrinter.pagetop))//pixel value for the bottom edge of printable area


if G <> Nil then
For i=maxNum downto minNum


g.DrawString trim(str(i)),fromleft,fromtop



The code works fine and prints one number per label, and then advancing the paper to the next page after each.

When the end of the method occurs, it prints a half page. If I call g=nil instead of letting the method end, then it prints a half page. This is terrible because now the label printer, on a continuous label spool, will then print it's next job starting with a half-sheet (so the next job is not printed correctly).

So I added a final G.NextPage after the loop. the G.NextPage prints a half page, and then the method exiting (g destructor) then prints another half-page. That's a weird workaround... so my questions are: why does it print a page when it's selected, and why would it print a halfpage when it's destroyed/set to nil in the first place?

Author:  harriew [ Wed Aug 29, 2012 2:25 pm ]
Post subject:  Re: Printer Graphics destructor prints a half-page

Once you open a printer object it creates the graphics object for a page to be printed. If you then close that printer object the page is sent to the printer, even if you never issued any DrawString or other commands to place something on the page.

In the second part with the loop, you do a NextPage to send the last page to the printer. Once it is sent the next page is automatically created and ready to have stuff placed on it. But, you are done printing, leave the method and the graphics object is closed and so that last, blank page, is sent to the printer. In the loop change the g.NextPage line to the following:
If i > minNum then g.NextPage

This way, when the last page has been printed, you are not issuing a g.NextPage command which loads in that blank page at the end. The actual last page gets sent to the printer when the printer object is closed.

Author:  judgedoug [ Wed Aug 29, 2012 2:43 pm ]
Post subject:  Re: Printer Graphics destructor prints a half-page

Alrighty, that alleviates the problem when printing to the printer.

But then I'm wondering how to effectively, in Win 7, allow the user to select a printer then select the page setup without having it print the page?

Author:  judgedoug [ Wed Aug 29, 2012 3:02 pm ]
Post subject:  Re: Printer Graphics creator & destructor prints a half-page

I'm also wondering why g.NextPage on a blank object would only send a half-page to print on the label printer. Academic at this point, but that doesn't make sense to me.

Author:  harriew [ Wed Aug 29, 2012 5:59 pm ]
Post subject:  Re: Printer Graphics creator & destructor prints a half-page

Absolutely no way to answer that with nothing but a statement that it happens. I have never seen such a thing myself; but, that doesn't mean that it can't happen. Just never seen it so have no idea what may be going on.

Author:  judgedoug [ Thu Aug 30, 2012 8:50 am ]
Post subject:  Re: Printer Graphics creator & destructor prints a half-page

Do you mind if I ask how you allow a user to select a printer, then a page setup, in Windows 7?

My code allows them to do this (although the button says "Print" instead of "Okay" when selecting the printer) and then after they do select everything, it prints a blank page because the graphics object is being destroyed at the end of the method.

Author:  harriew [ Thu Aug 30, 2012 11:13 am ]
Post subject:  Re: Printer Graphics creator & destructor prints a half-page

Here is some code from a program that I used to use that produces a large number of different reports. These reports are all done with drawString commands.

First to define some of the properties that you will see that are not dimmed within the methods shown.

pg is a window property which points to the printers graphics object upon which the reports will be produced.
gPS is a global printersetup object
gPSSettings is a printersetup string which is stored in, and retrieved from, the programs preference file.

At the start of each of the reports the following lines will be found:
if preparePrinter then
end if

The preparePrinter method is a method of the window as all reports a printed from this window and it contains the following:
Private Function preparePrinter() As Boolean
if gPS = nil then // is printersetup established?
if not doPageSetup then // no, present page setup dialog
return true
end if
end if
if pg <> nil then
pg = nil
end if
pg = openPrinterDialog(gPS)
if pg = nil then
return true
end if
return false
End Function

Note the one oddity above, I return true if there is a problem, or the user cancels the printing. Just the way my mind was working when I wrote that code.

You can see at the beginning of that code, if there is no current printersetup string available, the doPageSetup method is called, another method of the window. It looks like the following:
Private Function doPageSetup() As Boolean
Dim ok As Boolean
Dim s As String

gPS = new PrinterSetup
if gPS = nil then
s = "Unable to create printerSetup class in doPageSetup method in the wStart window. " _
+"The program must quit."
msgBox s
end if
if gPSSettings <> "" then
gPS.setupString = gPSSettings
end if
gPS.maxVerticalResolution = -1 // allow maximum resolution of printer
gPS.maxHorizontalResolution = -1
ok = gPS.PageSetupDialog
if ok then
gPSSettings = gPS.setupString
gxSF = gPS.horizontalResolution \ 72 // horizontal scale factor
gySF = gPS.verticalResolution \ 72 // vertical scale factor
updatePref("printersetup", gPSSettings)
end if
return ok
End Function

There is also a "Page Setup..." option on the File menu and the menu handler for it contains the following:
Function FilePageSetup() As Boolean
Dim ok As Boolean

ok = doPageSetup
Return True

End Function

So, in this code the PageSetup information is processed first, so that it can be passed to the OpenPrinterDialog.

The OpenPrinterDialog is presented last so that if the user cancels the printing, the printer will never have been opened and consequently no blank page will be sent to the printer. Also note the calculations for gxSF and gySF near the end of the doPageSetup method. These are the scaling factors to be used when producing the reports. Everything having a dimension of some sort going to the report gets multiplied by the appropriate scaling factor. I hope this helps.

Author:  harriew [ Thu Aug 30, 2012 11:28 am ]
Post subject:  Re: Printer Graphics creator & destructor prints a half-page

I should add, this is all code done on a Mac; however, seems to me that the same order of processing should be good on Windows as well but I certainly will not bet my retirement check on that one!!

Author:  judgedoug [ Thu Aug 30, 2012 2:58 pm ]
Post subject:  Re: Printer Graphics creator & destructor prints a half-page

I think that's the problem. In Windows 7, you cannot select a printer from PageSetup. If you don't call the OpenPrinterDialog first, then the PageSetup doesn't know what size paper is selected from the available pages from the Printer.

This is very important when you have multiple printers that an application prints. For instance, one label printer loaded with 1"x2" labels, another label printer loaded with 2"x3" labels, a normal printer loaded with Statement sheets (8.5"x5.5") and another printer with normal Letter sheets.

In Win 7, if you call PageSetup first, it utilizes the settings of the Default Printer. So if we were setting up the 1x2 label printer and the letter printer was default, it would have 8.5x11 dimensions. So the OpenPrinterDialog MUST be called first, in order for the printer to be chosen, then the PageSetupDialog, which will have the print sizes available for the selected printer (in this case, the correct 1x2 label).

There's GOT to be a better way for Win 7.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group