有没有办法在启动进程时指定一个“打印到”打印机?

13

我的现状

我正在编写一个程序,它可以对指定的文件执行一些操作。目前,它会打开文件,或将其附加到电子邮件并发送到指定的地址。

该文件可以是以下格式之一:Excel、Excel报告、Word或PDF。

我目前正在使用文件路径生成进程,并启动该进程;但是,我还尝试修复一个问题(已添加到启动信息中的PrintTo动词),具体取决于指定的设置。

我的需求

我的任务是想将文档打开,并打印到程序内部指定的打印机上,然后文件应自动关闭。

如果没有通用的方法来实现这个功能,我们可以考虑为每种不同的文件类型制定一种方法。

你需要准备的工具

这里是我正在使用的代码:

ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = FilePath;

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
       // TODO: Add default printer.
    }

    pStartInfo.Verb = "PrintTo";
}

// Open the report file unless only set to be emailed.
if ((!Email && !Print) || Print)
{
    Process p = Process.Start(pStartInfo);
}

我的进展如何...

仍然被卡住了...可能会像微软一样说,'这是有意设计的'。


可能是重复问题:如何在所选打印机中打印任何文档 - Rowland Shaw
3个回答

29

以下内容已经被我验证过了(测试了*.doc和*.docx文件)

使用"System.Windows.Forms.PrintDialog"可以出现Windows的打印对话框,并且使用"System.Diagnostics.ProcessStartInfo"只需选择所选打印机即可 :)

请将FILENAME替换为您的Office文件的完整名称(路径+名称)。我认为这对其他文件也有效...

// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(**FILENAME**);
        info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
        info.CreateNoWindow = true;
        info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        info.UseShellExecute = true;
        info.Verb = "PrintTo";
        System.Diagnostics.Process.Start(info);
    }
}

2
对我来说也适用于PDF文件!谢谢 - 帮了我很多 - pila
我们可以从打印对话框中传递整个打印设置吗? - huMpty duMpty
@huMpty duMpty:你说的“传递整个打印设置”是什么意思?你已经通过使用“yourPrintDialog1.PrinterSettings”从打印对话框中接收到了所有的打印设置。 - dataCore
@AvneeshSrivastava:没有测试打印机是否可用。也许Windows打印对话框会处理它... - dataCore
@DataCore 如果我使用进程和PrintDialog这样打印,打印机会忽略我在对话框中选择的设置,例如纸张来源或方向。有什么想法吗? - Mong Zhu
显示剩余3条评论

5

理论上,根据MSDN上的一篇文章(链接),你应该可以更改它为以下内容:

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
        pStartInfo.Arguments = "\"" + PrinterName + "\"";
    }

    pStartInfo.CreateNoWindow = true;
    pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    pStartInfo.UseShellExecute = true;
    pStartInfo.WorkingDirectory = sDocPath;

    pStartInfo.Verb = "PrintTo";
}

1
我们可以将整个打印设置从打印对话框中作为参数传递吗? - huMpty duMpty
@huMptyduMpty 不行,ShellExecute 只支持发送文档和打印机名称。 - Rowland Shaw
有没有办法可以传递打印机设置? - huMpty duMpty
@huMptyduMpty 处理从打印机设置对话框中获取的详细信息并进行打印的唯一方法是自己处理打印(即页面布局等)。没有标准的方法可以将设置传递给任意客户端应用程序。 - Rowland Shaw

1

来自 Rowland Shaw:

               ProcessStartInfo startInfo = new ProcessStartInfo(Url)
                {
                    Verb = "PrintTo",
                    FileName = FilePath,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true,
                    Arguments = "\"" + PrinterName+ "\"",
                };
                Process.Start(startInfo);

文件路径看起来像是'D:\EECSystem\AttachedFilesUS\53976793.pdf'

打印机名称是你的打印机名称

复制代码,它将起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接