KazooSoft
asked on
Programmatically printing from Report Viewer c#
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
Cheers
ASKER
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.
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();
and always use this link to convert:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://www.developerfusion.com/tools/convert/vb-to-csharp/
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
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
so it wil create printout....pdf and redelete it
ASKER
Ok I have converted this to C# but how to i suppress the PDF window?
Cheers
Cheers
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
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
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome results thank you for all your help!
Open in new window