Results 1 to 24 of 24

Thread: Print to printer without dialog and print preview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Print to printer without dialog and print preview

    I have a program I created to print labels where is has a print preview form and an rdlc for interaction so users can see what they print before they print it. Now new desire is to have it auto print from an external source. I have created arguments to get the data, printer, copies, and what layouts I need to make the selection automatically, The issue I I don't know how to have the program use the existing code and have it print directly to the printer without print preview. Existing code is attached

    Code:
    With PrintPreview114.ReportViewer114
                    .PrinterSettings.PrinterName = "\\GVL02\114 Label Printer (Assembly)" 'Intermec Printer PM4i
                    temstr = .PrinterSettings.PrinterName
                    goodprinter = TestPrinter(temstr)
                    If goodprinter = "" Then
                        ' MessageBox.Show("Printed is not installed. Contact your Administrator.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    Else
                        If temstr <> goodprinter Then
                            .PrinterSettings.PrinterName = goodprinter
                        End If
                    End If
                    .ProcessingMode = ProcessingMode.Local
                    .PrinterSettings.Copies = lcnumoflabels
                    .LocalReport.DataSources.Clear()
                    .LocalReport.DataSources.Add(RDS)
                    .SetDisplayMode(DisplayMode.PrintLayout)
                    'Set the paper
                    Try
                        .PrinterSettings.DefaultPageSettings.PaperSize = .PrinterSettings.PaperSizes(2)
                    Catch
                        MessageBox.Show("Printer needs to be setup. Check Paper sizes.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End Try
                    .LocalReport.SetParameters(param1)
                    .LocalReport.SetParameters(param2)
                    .LocalReport.SetParameters(param3)
                    .LocalReport.SetParameters(param4)
                    .LocalReport.Refresh()
                End With

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    You need to show the code that opens the PrintPreviewDialog. It’s probably a simple fix…

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    With PrintPreview114??? Ok I get it now. What other properties does Your reportviewer have?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    after the End With in the normal program I have
    PrintPreview114.Show()
    There is only Me.ReportViewer114.RefreshReport() in the PrintPreview114_load.

    Thanks for helping.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    So there is no Print command instead of PrintPreview114.Show?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    apparently, I am a novice to VB so I could have overlooked it. What would be the syntax? Because there is no syntax for the PrintPreview114 or Reportviewer114 for print?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    .paul., what do I need to do to get this working? Your help is greatly appreciated.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Try this...

    Code:
    With PrintPreview114.ReportViewer114
        .PrinterSettings.PrinterName = "\\GVL02\114 Label Printer (Assembly)" 'Intermec Printer PM4i
        temstr = .PrinterSettings.PrinterName
        goodprinter = TestPrinter(temstr)
        If goodprinter = "" Then
            ' MessageBox.Show("Printed is not installed. Contact your Administrator.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        Else
            If temstr <> goodprinter Then
                .PrinterSettings.PrinterName = goodprinter
            End If
        End If
        .ProcessingMode = ProcessingMode.Local
        .PrinterSettings.Copies = lcnumoflabels
        .LocalReport.DataSources.Clear()
        .LocalReport.DataSources.Add(RDS)
        .SetDisplayMode(DisplayMode.PrintLayout)
        'Set the paper
        Try
            .PrinterSettings.DefaultPageSettings.PaperSize = .PrinterSettings.PaperSizes(2)
        Catch
            MessageBox.Show("Printer needs to be setup. Check Paper sizes.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        .LocalReport.SetParameters(param1)
        .LocalReport.SetParameters(param2)
        .LocalReport.SetParameters(param3)
        .LocalReport.SetParameters(param4)
        .LocalReport.Refresh()
        .LocalReport.PrintToPrinter()
    End With

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    I get 'PrintToPrinter' is not a member of Microsoft.Reportin.Winforms.LocalReport?

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Ok. This page from Microsoft explains how…

    https://docs.microsoft.com/en-us/pre...ectedfrom=MSDN

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    Yea, I saw that. I was trying to avoid it if possible. Guess not. Thanks

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    OK, I have that implemented but it only prints 1 copy. When I add printdoc.PrinterSettings.Copies = 2 it still prints 1 copy. Wam I missing something?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Can you show me your PrintPage code? You need to implement a loop to print your number of copies... You'd set PrinterSettings.Copies = 2, then run PrintPage twice.

    Edit: or any number in PrinterSettings.Copies

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintDocument1.PrinterSettings.Copies = 2
        PrintDocument1.Print
    End Sub
    
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        ' your page print code
    
        PrintDocument1.PrinterSettings.Copies -= CShort(1)
    
        If PrintDocument1.PrinterSettings.Copies > 0 Then
            e.HasMorePages = True
        End If
    End Sub

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    The printpage code looke like this:
    Code:
     Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
            Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
    
            ' Adjust rectangular area with printer margins.
            Dim adjustedRect As New Rectangle(ev.PageBounds.Left - CInt(ev.PageSettings.HardMarginX),
                                              ev.PageBounds.Top - CInt(ev.PageSettings.HardMarginY), _
                                              ev.PageBounds.Width, _
                                              ev.PageBounds.Height)
    
            ' Draw a white background for the report
            ev.Graphics.FillRectangle(Brushes.White, adjustedRect)
    
            ' Draw the report content
            ev.Graphics.DrawImage(pageImage, adjustedRect)
    
            ' Prepare for the next page. Make sure we haven't hit the end.
            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
        End Sub
    So I would set the copies then in the sub print Which looks like this
    Code:
        Private Sub Print()
            If m_streams Is Nothing OrElse m_streams.Count = 0 Then
                Throw New Exception("Error: no stream to print.")
            End If
            AddHandler printdoc.PrintPage, AddressOf PrintPage
            m_currentPageIndex = 0
            printdoc.Print()
        End Sub

    Add a loop for the AddHanler line?

    Why is this so complicated? Why couldn't Microsoft Create code to send a rdlc direct to a printer? like localreport.print()? Years ago in Foxpro, I think we could do it. BTW, thanks again for all your help.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    No. See my code in post #14
    It’s more complicated as your printing spans over more than one page. You can print more copies by setting your page range counter back to 0 and then setting hasmorepages to true, then it’ll print another copy of your complete pages in your range

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    OK, I set my printdoc.printersettings.copies to 2 then added the
    Code:
     printdoc.PrinterSettings.Copies -= CShort(1)
     If printdoc.PrinterSettings.Copies > 0 Then
        ev.HasMorePages = True
    End If
    I see how it is working but when it hits the printpage sub again because hasmorepages is now true, in the Dim pageimage line it errors out unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll. BT, it does show a messagebox stating what page it is printing. Is there any way to get rid of that too?

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Code:
    Private Sub Print()
        If m_streams Is Nothing OrElse m_streams.Count = 0 Then
            Throw New Exception("Error: no stream to print.")
        End If
        AddHandler printdoc.PrintPage, AddressOf PrintPage
        m_currentPageIndex = 0
        printdoc.PrinterSettings.Copies = 2
        printdoc.Print()
    End Sub
    Code:
    Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
    
        ' Adjust rectangular area with printer margins.
        Dim adjustedRect As New Rectangle(ev.PageBounds.Left - CInt(ev.PageSettings.HardMarginX),
                                              ev.PageBounds.Top - CInt(ev.PageSettings.HardMarginY), _
                                              ev.PageBounds.Width, _
                                              ev.PageBounds.Height)
    
        ' Draw a white background for the report
        ev.Graphics.FillRectangle(Brushes.White, adjustedRect)
    
        ' Draw the report content
        ev.Graphics.DrawImage(pageImage, adjustedRect)
    
        ' Prepare for the next page. Make sure we haven't hit the end.
        m_currentPageIndex += 1
        if (m_currentPageIndex < m_streams.Count) Then
            ev.HasMorePages = True
        Else
            printdoc.PrinterSettings.Copies -= CShort(1)
            m_currentPageIndex = 0
            If printdoc.PrinterSettings.Copies > 0 Then
                ev.HasMorePages = True
            End If
        End If
    End Sub

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    I stepped away from it for a while and went back to and and go it. Definitely had to do a loop like you stated, but the loop has to be here. Start the loop then do the new code for the Export and Print. Finally. Again, Thanks for all your help.


    Code:
     For i = 1 To 2
         printdoc = New PrintDocument
         Export(PrintPreview114.ReportViewer114.LocalReport, "114")
         Print()
    Next

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Or you could do it without a loop, as I showed you in post #18

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    For some reason, I got an unhandled exception error on that code when it went to print page 2.

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    On which line?

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Print to printer without dialog and print preview

    When it sets the ev.hasmorepages to true, at the end of the Sub, it goes back to the beginning of the Sub and errors out on
    Dim pageImage As New Metafile(m_streams(m_currentPageIndex)). If I let it print the page (document) and then exit the sub, I can print copy with the loop.

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Print to printer without dialog and print preview

    Try this...

    Code:
    Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
    
        ' Adjust rectangular area with printer margins.
        Dim adjustedRect As New Rectangle(ev.PageBounds.Left - CInt(ev.PageSettings.HardMarginX),
                                              ev.PageBounds.Top - CInt(ev.PageSettings.HardMarginY), _
                                              ev.PageBounds.Width, _
                                              ev.PageBounds.Height)
    
        ' Draw a white background for the report
        ev.Graphics.FillRectangle(Brushes.White, adjustedRect)
    
        ' Draw the report content
        ev.Graphics.DrawImage(pageImage, adjustedRect)
    
        pageImage.Dispose
    
        ' Prepare for the next page. Make sure we haven't hit the end.
        m_currentPageIndex += 1
        if (m_currentPageIndex < m_streams.Count) Then
            ev.HasMorePages = True
        Else
            printdoc.PrinterSettings.Copies -= CShort(1)
            m_currentPageIndex = 0
            If printdoc.PrinterSettings.Copies > 0 Then
                ev.HasMorePages = True
            End If
        End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width