0

我一直在用 Visual c# 开发一个应用程序,它需要按下一个按钮来打印一个现有的 pdf 文件。

我一直在使用该System.Diagnostics.Process方法进行打印,但它在 Windows 8 环境中无法正常工作,因为“Printto”命令似乎在那里不起作用。

我想使用替代方法,例如可能System.Drawing.Printing.PrintDocument有一个适用于文本文件的示例,但是当我尝试使用 pdf 时,它正在打印随机字符。

我的谷歌搜索似乎是空的,或者我没有输入正确的关键字,但我需要一个解决方案,它不仅可以将 pdf 打印到目标打印机,而且可以确定打印机是否准备好、脱机或缺纸以及错误捕获。

请告知,因为我也愿意查看任何可推荐的 SDK 或第三方路线。

编辑:添加了我目前正在使用的代码片段:

        string defFile = (Path.Combine(GlobalVars.pdfPath, tkt_no + "_DEF.pdf"));
        string rwPrinter = "";
        if (GlobalVars.useDefaultPrinter == false) 
        { 
            foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                if (strPrinter.StartsWith("ZDesigner"))
                {
                    rwPrinter = strPrinter;
                    break;
                }
            }
        }
        if (jobdo.Equals("print"))
        {
            Process process = new Process();
            //process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.FileName = defFile;
            if (rwPrinter.Length > 0)
            {
                process.StartInfo.Verb = "printto";
                //process.StartInfo.Verb = (Path.Combine(System.Windows.Forms.Application.StartupPath, "printto.exe"));
                process.StartInfo.Arguments = "\"" + rwPrinter + "\"";
            }
            else
            {
                process.StartInfo.Verb = "print";
            }

            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            process.WaitForInputIdle();
            //while (process.MainWindowHandle == IntPtr.Zero)
            //{
            //    Thread.Sleep(20);
            //    process.Refresh();
            //}
            Thread.Sleep(7000);
            try
            {
                process.Kill();
            }
            catch { }
            // close any occurrences of Adobe Reader that may not close through a citrix environment regardless
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
            {
                if (p.ProcessName.Equals("AcroRd32"))
                {
                    ObjectQuery sq = new ObjectQuery
                        ("Select * from Win32_Process where ProcessID = '" + p.Id + "'");
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
                    foreach (ManagementObject oReturn in searcher.Get())
                    {
                        string[] o = new string[2];
                        oReturn.InvokeMethod("GetOwner", (object[])o);
                        if(o[0] != null)
                            if(o[0].Equals(System.Environment.UserName))
                                p.Kill();
                    }

                }
            }

            if (rwPrinter == "") 
            {
                rwPrinter = "the default printer";
            }
            else
                MessageBox.Show("Ticket " + tkt_no + " was printed to " + rwPrinter + ".");
        }
4

1 回答 1

-2

请看这个,使用 iTextSharp 库您可以轻松创建 PDF 文件:http: //sourceforge.net/projects/itextsharp/

于 2013-10-31T13:17:29.427 回答