Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. openFileDialog1.InitialDirectory = "c:\";
  2. openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
  3. openFileDialog1.FilterIndex = 2;
  4. openFileDialog1.RestoreDirectory = true;
  5.  
  6. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  7. {
  8. if ((fileName = openFileDialog1.FileName) != null)
  9. {
  10. startInfo = new ProcessStartInfo(fileName);
  11.  
  12. if (File.Exists(fileName))
  13. {
  14. i = 0;
  15. foreach (String verb in startInfo.Verbs)
  16. {
  17. // Display the possible verbs.
  18. MessageBox.Show(i.ToString() + ". " + verb);
  19. i++;
  20. }
  21. }
  22. }
  23.  
  24. //Console.WriteLine("Select the index of the verb.");
  25. string index = "2";
  26. if (Convert.ToInt32(index) < i)
  27. verbToUse = startInfo.Verbs[Convert.ToInt32(index)];
  28. else
  29. return;
  30.  
  31. startInfo.Verb = verbToUse;
  32. if (verbToUse.ToLower().IndexOf("printto") >= 0)
  33. {
  34. //Printer Name
  35. arguments = @"\hydfsvt02HPLaserJ";
  36. startInfo.Arguments = arguments;
  37. }
  38.  
  39. Process newProcess = new Process();
  40. newProcess.StartInfo = startInfo;
  41.  
  42. try
  43. {
  44. newProcess.Start();
  45.  
  46. MessageBox.Show(newProcess.ProcessName + " for file " + fileName + " started successfully with verb " + startInfo.Verb);
  47. }
  48. catch (System.ComponentModel.Win32Exception ex)
  49. {
  50. MessageBox.Show(" Win32Exception caught!");
  51. MessageBox.Show(" Win32 error = " + ex.Message);
  52. }
  53. catch (System.InvalidOperationException)
  54. {
  55. MessageBox.Show("File " + fileName + " started with verb " + verbToUse);
  56. }
  57. }
  58.  
  59. private void startPrintingButton_Click(object sender, EventArgs e)
  60. {
  61. OpenFileDialog ofd = new OpenFileDialog();
  62. if (DialogResult.OK == ofd.ShowDialog(this))
  63. {
  64. PrintDocument pdoc = new PrintDocument();
  65.  
  66. pdoc.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GK420d";
  67. pdoc.DefaultPageSettings.Landscape = true;
  68. pdoc.DefaultPageSettings.PaperSize.Height = 140;
  69. pdoc.DefaultPageSettings.PaperSize.Width = 104;
  70.  
  71. Print(pdoc.PrinterSettings.PrinterName, ofd.FileName);
  72. }
  73. }
  74.  
  75. private void Print(string printerName, string fileName)
  76. {
  77. try
  78. {
  79. ProcessStartInfo gsProcessInfo;
  80. Process gsProcess;
  81.  
  82. gsProcessInfo = new ProcessStartInfo();
  83. gsProcessInfo.Verb = "PrintTo";
  84. gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
  85. gsProcessInfo.FileName = fileName;
  86. gsProcessInfo.Arguments = """ + printerName + """;
  87. gsProcess = Process.Start(gsProcessInfo);
  88. if (gsProcess.HasExited == false)
  89. {
  90. gsProcess.Kill();
  91. }
  92. gsProcess.EnableRaisingEvents = true;
  93.  
  94. gsProcess.Close();
  95. }
  96. catch (Exception)
  97. {
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement