Solved

Programmatically printing from Report Viewer c#

Posted on 2011-03-16
11
2,612 Views
Last Modified: 2013-12-17
I have for example a ReportViewer. I have a loop which will go through the report populating it then exporting to pdf and doing the next. I would like to know if anyone knows a way of forcing it to print without a dialogue box?

Cheers
0
Comment
Question by:KazooSoft
  • 7
  • 4
11 Comments
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35146958
this code will open the pdf print and close
Dim MyProcess As New Process
        MyProcess.StartInfo.CreateNoWindow = False
        MyProcess.StartInfo.Verb = "print"
        MyProcess.StartInfo.FileName = "C:\file.pdf "
        MyProcess.Start()
        MyProcess.WaitForExit(10000)
        MyProcess.CloseMainWindow()
        MyProcess.Close()

Open in new window

0
 
LVL 4

Author Comment

by:KazooSoft
ID: 35147340
Do you have that in C#? I think I will have to do it that way. Export them all to PDF then print them. Then delete them.
0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35147382

Process MyProcess = new Process();
MyProcess.StartInfo.CreateNoWindow = false;
MyProcess.StartInfo.Verb = "print";
MyProcess.StartInfo.FileName = "C:\\file.pdf ";
MyProcess.Start();
MyProcess.WaitForExit(10000);
MyProcess.CloseMainWindow();
MyProcess.Close();

Open in new window

0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35147388
and always use this link to convert:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35147415
use this code in the report viewer form, it will solve your issue
Private Sub frm_ReportViewer_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Dim warnings As Warning() = Nothing
        Dim streamids As String() = Nothing
        Dim mimeType As String = Nothing
        Dim encoding As String = Nothing
        Dim extension As String = Nothing
        Dim bytes As Byte()
        Dim FileName As String

       



        bytes = ReportViewer1.LocalReport.Render("Pdf", _
          Nothing, mimeType, _
            encoding, extension, streamids, warnings)
        FileName = "PrintOut" & Format(Now, "yyyyMMddhhmmss") & ".pdf"
        Dim fs As New FileStream("C:\file.pdf", FileMode.Create)
        fs.Write(bytes, 0, bytes.Length)
        fs.Close()

        Dim MyProcess As New Process
        MyProcess.StartInfo.CreateNoWindow = False
        MyProcess.StartInfo.Verb = "print"
        MyProcess.StartInfo.FileName = "C:\file.pdf"
        MyProcess.Start()
        MyProcess.WaitForExit(10000)
        MyProcess.CloseMainWindow()
        MyProcess.Close()


	 Try

            Dim strFileSize As String = ""
            Dim di As New IO.DirectoryInfo("c:\")
            Dim aryFi As IO.FileInfo() = di.GetFiles("file*.pdf")
            Dim fi As IO.FileInfo


            For Each fi In aryFi
                fi.Delete()
            Next
        Catch
        End Try

        Me.Close()
    End Sub

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:LeDaouk
ID: 35147425
as you can see filename in the code is to generate a file everytime you can use it instead of file.pdf
so it wil create printout....pdf and redelete it
0
 
LVL 4

Author Comment

by:KazooSoft
ID: 35147559
Ok I have converted this to C# but how to i suppress the PDF window?

Cheers
0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35147602
Try

            Dim strFileSize As String = ""
            Dim di As New IO.DirectoryInfo("c:\")
            Dim aryFi As IO.FileInfo() = di.GetFiles("file*.pdf")
            Dim fi As IO.FileInfo


            For Each fi In aryFi
                fi.Delete()
            Next
        Catch
        End Try
 is deleting the file at the end of the sub
0
 
LVL 4

Author Comment

by:KazooSoft
ID: 35147638
That works perfectly. But it keeps spamming the user with a lot of PDF windows until we wait for them to close. Is there a way of getting it to run the chosen PDF reading in the background? out of sight?
0
 
LVL 4

Accepted Solution

by:
LeDaouk earned 500 total points
ID: 35147814
MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
and hide the form
0
 
LVL 4

Author Closing Comment

by:KazooSoft
ID: 35148331
Awesome results thank you for all your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now