打开并打印 PDF 文件

本文关键字:PDF 文件 打印 | 更新日期: 2023-09-27 18:35:45

我想打开并打印位于给定文件夹中的所有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] + "'"";

打开并打印 PDF 文件

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

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

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

 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);
            }
        }