code_junkie

Print existing PDF (or other files) in C#

Nov 12th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. private void PrintFormPdfData(byte[] formPdfData)
  2. {
  3. string tempFile;
  4.  
  5. tempFile = Path.GetTempFileName();
  6.  
  7. using (FileStream fs = new FileStream(tempFile, FileMode.Create))
  8. {
  9. fs.Write(formPdfData, 0, formPdfData.Length);
  10. fs.Flush();
  11. }
  12.  
  13. try
  14. {
  15. string gsArguments;
  16. string gsLocation;
  17. ProcessStartInfo gsProcessInfo;
  18. Process gsProcess;
  19.  
  20. gsArguments = string.Format("-grey -noquery -printer "HP LaserJet 5M" "{0}"", tempFile);
  21. gsLocation = @"C:Program FilesGhostgumgsviewgsprint.exe";
  22.  
  23. gsProcessInfo = new ProcessStartInfo();
  24. gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
  25. gsProcessInfo.FileName = gsLocation;
  26. gsProcessInfo.Arguments = gsArguments;
  27.  
  28. gsProcess = Process.Start(gsProcessInfo);
  29. gsProcess.WaitForExit();
  30. }
  31. finally
  32. {
  33. File.Delete(tempFile);
  34. }
  35. }
  36.  
  37. ''' <summary>
  38. ''' Start Adobe Process to print document
  39. ''' </summary>
  40. ''' <param name="p"></param>
  41. ''' <remarks></remarks>
  42. Private Function printDoc(ByVal p As PrintObj) As PrintObj
  43. Dim myProcess As New Process()
  44. Dim myProcessStartInfo As New ProcessStartInfo(adobePath)
  45. Dim errMsg As String = String.Empty
  46. Dim outFile As String = String.Empty
  47. myProcessStartInfo.UseShellExecute = False
  48. myProcessStartInfo.RedirectStandardOutput = True
  49. myProcessStartInfo.RedirectStandardError = True
  50.  
  51. Try
  52.  
  53. If canIprintFile(p.sourceFolder & p.sourceFileName) Then
  54. isAdobeRunning(p)'Make sure Adobe is not running; wait till it's done
  55. Try
  56. myProcessStartInfo.Arguments = " /t " & """" & p.sourceFolder & p.sourceFileName & """" & " " & """" & p.destination & """"
  57. myProcess.StartInfo = myProcessStartInfo
  58. myProcess.Start()
  59. myProcess.CloseMainWindow()
  60. isAdobeRunning(p)
  61. myProcess.Dispose()
  62. Catch ex As Exception
  63. End Try
  64. p.result = "OK"
  65. Else
  66. p.result = "The file that the Document Printer is tryng to print is missing."
  67. sendMailNotification("The file that the Document Printer is tryng to print" & vbCrLf & _
  68. "is missing. The file in question is: " & vbCrLf & _
  69. p.sourceFolder & p.sourceFileName, p)
  70. End If
  71. Catch ex As Exception
  72. p.result = ex.Message
  73. sendMailNotification(ex.Message, p)
  74. Finally
  75. myProcess.Dispose()
  76. End Try
  77. Return p
  78. End Function
Add Comment
Please, Sign In to add comment