2009-07-08 10 views
6

Próbuję wydrukować plik PDF za pomocą obiektu Process. I do pewnego stopnia mogłem go wydrukować z powodzeniem. Ale teraz chcę ustawić właściwości drukarki .. jak żadne kopie, rozmiar papieru itp. Ale nie widzę żadnej właściwości, aby ustawić te wartości. Używam następujący kod do drukowania plików PDFJak ustawić ustawienia drukarki podczas drukowania PDF

string fileName = ""; 
string arguments = ""; 
string verbToUse = ""; 
int i = 0; 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

openFileDialog1.InitialDirectory = "c:\\"; 
openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*"; 
openFileDialog1.FilterIndex = 2; 
openFileDialog1.RestoreDirectory = true; 

if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    if ((fileName = openFileDialog1.FileName) != null) 
    { 
     startInfo = new ProcessStartInfo(fileName); 

     if (File.Exists(fileName)) 
     { 
      i = 0; 
      foreach (String verb in startInfo.Verbs) 
      { 
       // Display the possible verbs. 
       MessageBox.Show(i.ToString() + ". " + verb); 
       i++; 
      } 
     } 
    } 

    //Console.WriteLine("Select the index of the verb."); 
    string index = "2"; 
    if (Convert.ToInt32(index) < i) 
     verbToUse = startInfo.Verbs[Convert.ToInt32(index)]; 
    else 
     return; 

    startInfo.Verb = verbToUse; 
    if (verbToUse.ToLower().IndexOf("printto") >= 0) 
    { 
     //Printer Name 
     arguments = @"\\hydfsvt02\HPLaserJ"; 
     startInfo.Arguments = arguments; 
    } 

    Process newProcess = new Process(); 
    newProcess.StartInfo = startInfo; 

    try 
    { 
     newProcess.Start(); 

     MessageBox.Show(newProcess.ProcessName + " for file " + fileName + " started successfully with verb " + startInfo.Verb); 
    } 
    catch (System.ComponentModel.Win32Exception ex) 
    { 
     MessageBox.Show(" Win32Exception caught!"); 
     MessageBox.Show(" Win32 error = " + ex.Message); 
    } 
    catch (System.InvalidOperationException) 
    { 
     MessageBox.Show("File " + fileName + " started with verb " + verbToUse); 
    } 
} 

Odpowiedz

0

Napisałem aplikację, która robi drukowanie wsadowe plików PDF.

Nie można określić ustawień drukarki, których chcesz użyć. Nie jest to nawet możliwe, jeśli używasz interfejsu COM z wersjami Adobe Standard/Pro.

Twoje opcje są albo:

  1. Kup licencję do trzeciej PDF renderujący, który można wykorzystać do konwersji plików PDF do bitmapy i używać PrintDocument kontrolować PrinterSettings
  2. użyć coś jak GhostScript, aby konwertować pliki PDF na pliki BMP, a następnie użyć klasy PrintDocument do wydrukowania plików BMP. Następnie możesz kontrolować ustawienia PrinterSettings.
0
private void startPrintingButton_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    if (DialogResult.OK == ofd.ShowDialog(this)) 
    { 
     PrintDocument pdoc = new PrintDocument(); 

     pdoc.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GK420d"; 
     pdoc.DefaultPageSettings.Landscape = true; 
     pdoc.DefaultPageSettings.PaperSize.Height = 140; 
     pdoc.DefaultPageSettings.PaperSize.Width = 104; 

     Print(pdoc.PrinterSettings.PrinterName, ofd.FileName); 
    } 
} 

private void Print(string printerName, string fileName) 
{ 
    try 
    { 
     ProcessStartInfo gsProcessInfo; 
     Process gsProcess; 

     gsProcessInfo = new ProcessStartInfo(); 
     gsProcessInfo.Verb = "PrintTo"; 
     gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     gsProcessInfo.FileName = fileName; 
     gsProcessInfo.Arguments = "\"" + printerName + "\""; 
     gsProcess = Process.Start(gsProcessInfo); 
     if (gsProcess.HasExited == false) 
     { 
      gsProcess.Kill(); 
     } 
     gsProcess.EnableRaisingEvents = true; 

     gsProcess.Close(); 
    } 
    catch (Exception) 
    { 
    } 
} 

Ten kod będzie drukować pliki PDF, jak również dostosować ustawienia drukowania.

Powiązane problemy