2009-07-08 48 views
6

Tôi đang cố gắng in tệp PDF bằng cách sử dụng đối tượng Quy trình. Và đến một mức độ nhất định tôi có thể in nó thành công. Nhưng bây giờ tôi muốn thiết lập thuộc tính máy in .. như không có bản sao, kích thước giấy vv Nhưng tôi không thấy bất kỳ tài sản để thiết lập các giá trị này. Tôi đang sử dụng mã sau để in các tệp PDFCách đặt Cài đặt Máy in trong khi in 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); 
    } 
} 

Trả lời

0

Tôi đã viết một ứng dụng thực hiện in hàng loạt tệp PDF.

Không thể chỉ định cài đặt máy in mà bạn muốn sử dụng. Nó thậm chí không thể nếu bạn sử dụng giao diện COM với các phiên bản Adobe Standard/Pro.

lựa chọn của bạn là để một trong hai:

  1. Mua một giấy phép để một renderer PDF của bên thứ ba mà bạn có thể sử dụng để chuyển đổi PDF sang file ảnh và sử dụng PrintDocument để kiểm soát PrinterSettings
  2. Sử dụng một cái gì đó như GhostScript để chuyển đổi các tập tin PDF thành các tập tin BMP và sau đó sử dụng lớp PrintDocument để in các tập tin BMP. Sau đó bạn có thể kiểm soát 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) 
    { 
    } 
} 

Mã này sẽ in tệp PDF cũng như điều chỉnh cài đặt in.

Các vấn đề liên quan