3

我想打开并打印位于给定文件夹中的所有 PDF 文件。这些文件根据以下模式命名:

NameOfThePrinter_Timestamp.pdf

现在我想使用相应的打印机打印这些文件:

static void Main(string[] args)
{
    string pdf = @"C:\PathToFolder";
    if (Directory.GetFiles(pdf).Length > 0)
    {
        string[] files = Directory.GetFiles(pdf);
        var adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("App Paths").OpenSubKey("AcroRd32.exe");
        var path = adobe.GetValue("");
        string acrobat = path.ToString();
        for (int i = 0; i < files.Length; i++)
        {
            Process process = new Process();
            process.StartInfo.FileName = acrobat;
            process.StartInfo.Verb = "printto";
            process.StartInfo.Arguments = "/p /s /h \"" + files[i] + "\"";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();

            DateTime start = DateTime.Now;
            IntPtr handle = IntPtr.Zero;

            while (handle == IntPtr.Zero && DateTime.Now - start <= TimeSpan.FromSeconds(2))
            {
                try
                {
                    System.Threading.Thread.Sleep(50);
                    handle = process.MainWindowHandle;
                } catch (Exception) { }
            }
            foreach (String verb in process.StartInfo.Verbs)
            {
                // Display the possible verbs.
                Console.WriteLine("  {0}. {1}", i.ToString(), verb);
                i++;
            }
            System.Threading.Thread.Sleep(10000);
            Console.Out.WriteLine("File: " + files[i] + " is printing!");
            process.Kill();
        }

        foreach (string str in files)
        {
            File.Delete(str);
        }
        Console.Out.WriteLine("Files are deleted!");
    }
}

我的问题是:如何将打印机名称作为参数传递?

在这里我尝试了一些东西,但它要么抛出错误,要么打印到默认打印机:

process.StartInfo.Arguments = "/p /s /h \"" + files[i] + "\"";
4

2 回答 2

0

您可以使用Ghostscript将 PDF 文档发送到打印机。

在这里您可以找到如何将 PDF 文档发送到打印机的示例:如何使用 GhostScript (gswin32c.exe) shell 命令在默认网络打印机上打印 PDF

如果您想直接控制 Ghostscript 而无需调用 .exe 文件,您可以在这里找到适用于 .NET 的 Ghostscript 包装器:http: //ghostscriptnet.codeplex.com

于 2013-10-16T09:58:32.227 回答
-1
 function printDisclosureDocument() {
            var doc = document.getElementById('pdfDocument');
            if (doc == 'undefined' || doc == null) {
                var pdfbox = document.createElement('embed');
                pdfbox.type = 'application/pdf';
                pdfbox.src = 'ShowPDF.aspx?refid=' + $('#MainContent_hdnRefId').val();
                pdfbox.width = '1';
                pdfbox.height = '1';
                pdfbox.id = 'pdfDocument';
                document.body.appendChild(pdfbox);
            }
            if (doc != null && doc != 'undefined') {
                //Wait until PDF is ready to print    
                if (typeof doc.print === 'undefined') {
                    setTimeout(function () { printDisclosureDocument(); }, 500);
                } else {

                    doc.print();
                }
            }
            else {
                setTimeout(function () { printDisclosureDocument(); }, 500);
            }
        }
于 2016-06-15T15:52:57.563 回答