g.copies always 1

I am having trouble printing multiple copies of an output where g.copies is always 1.

I went to the examples and made the following change for g.nextpage but as copies is always 1 the loop doesn’t work.

I’m using - 2108 R2

Dim g As Graphics
Dim p As PrinterSetup

p = New PrinterSetup
If p.PageSetupDialog Then
g = OpenPrinterDialog§
If g <> Nil Then
for i as integer = 1 to g.Copies
Dim hDPI As Double = p.HorizontalResolution
Dim vDPI As Double = p.VerticalResolution

  Dim pageWidth As Double = p.PageWidth
  Dim pageHeight As Double = p.PageHeight
  
  g.TextFont = "Arial"
  g.TextSize = 9
  
  // Width and height of label in inches
  Dim labelWidth As Double = WidthField.Text.Val
  Dim labelHeight As Double = HeightField.Text.Val
  
  // Width and height of label in page points
  Dim labelPageWidth As Double = hDPI * labelWidth
  Dim labelPageHeight As Double = vDPI * labelHeight
 
  // Draw as many labels as fit into the size of the page
  For x As Integer = 1 To pageWidth Step labelPageWidth
    For y As Integer = 1 To pageHeight Step labelPageHeight
      If (y + labelPageHeight) < pageHeight And _
        (x + labelPageWidth) < pageWidth Then
        g.DrawString(LabelArea.Text, x, y)
      End If
    Next
  Next
  if g.Copies > 1 then
    g.NextPage
  end if
next i

End If
End If

Simpler example same result

If ps.PageSetupDialog Then
Dim g As Graphics
g = OpenPrinterDialog(ps, Nil)
If g <> Nil Then
for i as integer = 1 to g.Copies
// if the report runs successfully
If rpt.Run(orders, ps) Then
rpt.Document.Print(g)
End If
if g.copies > 1 then
g.NextPage
end if
next i
End If

End If


 For i as Integer = 0 to g.Copies -1
    
  //YOUR PRINTING METHOD
    
    
    If i < g.Copies -1 Then
      
      // kicks out a page and starts a new one
      g.nextpage   
      
      
    End If
    
  Next

//Make g Nill to ensure no blank pages 
g = Nil 

The issue is that no matter what the user enters on the print dialog, g.Copies is always reporting 1. I am seeing this and can confirm the behavior happens with Xojo 2018r4, macOS 10.14.2

Ah ok it is a 2018R4 problem?

I’m still on 2018R3

Thanks for the report Tim

Martin is reporting it on 2018r2 as well. The test is simple to do:

dim ps as new PrinterSetup
dim g as Graphics = OpenPrinterDialog(ps)
if g <> nil then
  dim iCopies as Integer = g.Copies
  break

end

Change copies in the print dialog to anything other than 1.

Ah my old eyes missed that part of the OP’s post about being on 2018r2 :confused:

I seemed to recall that Copies has NEVER worked?

[quote]
I seemed to recall that Copies has NEVER worked?[/quote]
Me too. Been a problem for so long I gave up even testing it.
Ditto page ranges

Consider this an amen from the congregation, but I ran into this with 2014, R2.1. I remember others at the time lamenting the same issue. So, it’s at least that old.

Isn’t this because (on macOS at least), the system takes care of printing multiple copies?

I think that is more precieved than actual… I would think that any app (Apple or otherwise) would basically do the same as what Brian illustrated… And if it were baked into macOS directly, than it should work automagically for Xojo too.

I ran the latest version of my main app with Xojo 2018.4 breaking on
g.copies
It was always 1 as reported by others. Also tried with Xojo 2017.3. Same result
Ran the compiled version of my app which is currently on the web site. Can’t remember whether it was compiled with 2018.4 or 2018.3, but it was one of the latest versions.
When I chose PRINT and selected 2 pages, 2 pages were printed. IOW, this seems to not display correctly in the debugger, but functions correctly in a compiled app.

So it sounds like Paul was correct then. This is good news.

See <https://xojo.com/issue/50117> (Closed By Design): OpenPrinterDialog / always one copy

[quote]Joe Ranieri on 30 Mar 2018
This case has been closed because the behavior described is not a bug. macOS will take care of respecting the number of copies the user requested and the application only needs to provide a single copy.[/quote]

So do we still need to provide a loop

for i As integer = 0 To g.copies-1 //print Next
Or is that all useless code?

That loop will take care of platforms where the system doesn’t take care of multiple copies. However, I’m not sure what the situation is with Windows and flavors of Linux.

It might also depend on the particular printer driver.

Thanks for the feedback.
If I try and print from the compiled version multiple copies to pdf or Preview only a single copy appears in the file.

Would that be the expected behaviour or should it show multiple copies in the file?

Printing in a file, then make duplicates (of this file) at will.

Printed to paper is a different beast.