打开并打印PDF文件

时间:2013-10-16 09:19:09

标签: c# pdf printing

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

2 个答案:

答案 0 :(得分:0)

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

您可以在此处找到如何将PDF文档发送到打印机的示例:How to print PDF on default network printer using GhostScript (gswin32c.exe) shell command

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

答案 1 :(得分:-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);
            }
        }