从 C# 以双工模式打印 pdf [英] Print pdf in duplex mode From C#

查看:44
本文介绍了从 C# 以双工模式打印 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pdf 文件,我想从我的 C# 应用程序中打印它.我能够在代码中弄清楚打印机是否支持双面打印,但可以从我的代码中以双面打印格式打印 pdf.这是我的常规单面打印代码.我能够检查预设为双工的 pdf 打印对话框的元数据.但它不起作用.

I have a pdf file that I would like to print from my C# app. I was able to figure out in code if the printer is duplex capable but get the pdf to print in duplex from my code. Here is my code for regular simplex printing. I was able to check the metadata of the pdf's print dialog preset to duplex. but it does not work.

            string verbToUse = "PrintTo";
            startInfo.Verb = verbToUse;
            startInfo.Arguments = workCenterPrinterName.Value.ToString();
            Process p = Process.Start(startInfo);
            p.WaitForExit(5000);//random time after which process will be killed
            if (p.HasExited == false)
            {
                p.Kill();
            }

推荐答案

我不知道随着时间的推移发布答案是否正确,但我在这个问题上挣扎了很多,结果代码比我们更简单所有的想法:

I don't know if it's right to post an answer given time passed, but i struggled a lot with this question and turns out the code is simpler than we all thought:

public static void Printing(string printer, string fileLoc)
        {
            //Set Duplex Settings Session
            PrinterSettings set = new PrinterSettings();
            set.PrinterName = printer;
            set.Duplex = Duplex.Default;

            //Start Printing process
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.FileName = fileLoc;
            //Print and close program immediatly
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;

            Process P = new Process();
            P.StartInfo = info;
            P.Start();

            P.WaitForInputIdle();
            System.Threading.Thread.Sleep(3000);
            set.Duplex = Duplex.Simplex; //It seems setting back printer to normal wasn't thoroughly tested though 
            if (false == P.CloseMainWindow())
                P.Kill();
            //kill process
        }

所以基本上,当您创建 PrinterSettings 时,它会保存在内存中的某处并用于下次打印.它非常适合我.唯一需要注意的是,您必须有一个默认的应用程序来阅读 PDF(例如 Adob​​e Reader),这可以在控制面板-->默认应用程序中轻松完成.

So basically it seems as you create the PrinterSettings its saved somewhere in memory and used in the next printing. It worked perfectly for me. Only thing important to note is that you MUST have a default application to read PDF (Adobe Reader for example), this is done easily in Control Panel-->Default Applications.

这篇关于从 C# 以双工模式打印 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆